当前位置: 首页 > news >正文

建设阅读网站的研究意义桂林论坛网app

建设阅读网站的研究意义,桂林论坛网app,费县做网站,家居品牌网站设计论文目录 一.LinkedHashSet和LinkedHashMap 1.基本介绍 2.与HashSet和HashMap的区别 3.LinkedHashSet和LinkedHashMap具体的方法 1.LinkedHashSet 2.LinkedHashMap 二.模拟代码实现LinkedHashMap 三.具体应用 一.LinkedHashSet和LinkedHashMap 1.基本介绍 顾名思义,根据名…

目录

一.LinkedHashSet和LinkedHashMap

1.基本介绍

2.与HashSet和HashMap的区别

3.LinkedHashSet和LinkedHashMap具体的方法

1.LinkedHashSet

2.LinkedHashMap

二.模拟代码实现LinkedHashMap

三.具体应用


一.LinkedHashSet和LinkedHashMap

1.基本介绍

顾名思义,根据名字我们可以知道LinkedHastSet和LinkedHashMap底层是用链表实现的,具体是使用双向链表实现的,下面是继承树,LinkedHashMap继承了父类HashMap,因此HashMap中的方法,在LinkedHashMap中都是可以使用的.(LinkedHashSet一样)

2.与HashSet和HashMap的区别

我们都知道HashSet和HashMap是根据HashCode存储到具体位置,例如是求余7存储到具体的索引,那么当我们遍历时候,存储的顺序和添加的顺序是不一样的.例如如下代码执行的结果,toString打印出来的和添加的顺序不一样

HashSet<Integer> set = new HashSet<>();
set.add(1);
set.add(5);
set.add(4);
set.add(3);
System.out.println(set);//[1, 3, 4, 5]

 而LinkedHashSet底层使用链表实现的,每次添加都是尾插法进行添加,所以添加的顺序一定是和遍历的顺序是一样的(这里采用迭代器遍历)

LinkedHashSet<Integer> set1 = new LinkedHashSet<>();
set1.add(1);
set1.add(5);
set1.add(4);
set1.add(3);
Iterator<Integer> iterator = set1.iterator();
while (iterator.hasNext()){System.out.print(iterator.next()+"  ");//1  5  4  3 
}

3.LinkedHashSet和LinkedHashMap具体的方法

1.LinkedHashSet

具体就是以下方法,LinkedHashSet和HashSet一样,不保存值相同的元素

LinkedHashSet<Integer> set = new LinkedHashSet<>();
set.add(1);
set.add(2);
set.add(3);
set.add(1);
System.out.println(set);//[1, 2, 3]
set.remove(1);//删除成功返回true,删除失败返回false
System.out.println(set);//[2, 3]
System.out.println(set.size());//2
System.out.println(set.isEmpty());//false
System.out.println(set.contains(2));//true

2.LinkedHashMap

LinkedHashMap<Integer,String> map=new LinkedHashMap<>();
map.put(1,"1");
map.put(2,"2");
map.put(3,"3");
System.out.println(map);//{1=1, 2=2, 3=3}
map.remove(1);
System.out.println(map);//{2=2, 3=3}
System.out.println(map.size());//2
map.put(1,"10");
System.out.println(map);//{2=2, 3=3, 1=10}
System.out.println(map.containsKey(1));//true
System.out.println(map.containsValue("10"));//true

二.模拟代码实现LinkedHashMap

这里我们为了方便,就没有采用泛型来定义.用了HashMap来存储键和对应的Node结点,这样根据键就可以找到对应的Node在LinkedHashMap中的位置,并且添加的时候和遍历的顺序也是一致的,这是一种比较好理解的LinkedHashMap的实现方式.链表这里我们采用了双向链表的存储方式,结点存储key和value,并且结点的key和HashMap的key是一一对应的.

public class LinkedHashMap extends LinkedList {//HashMap的键(key)和LinkedList的Node的键相互对应HashMap<Integer, Node> map;LinkedList list;public LinkedHashMap() {map = new HashMap<>();list = new LinkedList();}public void addLast(int key, int val) {Node x = new Node(key, val);list.addLast(x);map.put(key, x);}public void remove(int key) {Node x = map.get(key);list.remove(x);map.remove(x);}public int size() {return list.size();}public boolean containsKey(int key){return map.containsKey(key);}}class LinkedList {class Node {public int key, val;public Node next, prev;public Node(int key, int val) {this.key = key;this.val = val;}}//头尾虚拟节点private Node head, tail;//链表元素数private int size;public LinkedList() {head = new Node(0, 0);tail = new Node(0, 0);head.next = tail;tail.prev = head;size = 0;}public void addLast(Node x) {x.prev = tail.prev;x.next = tail;tail.prev.next = x;tail.prev = x;size++;}public void remove(Node x) {x.prev.next = x.next;x.next.prev = x.prev;size--;}public Node removeFirst() {//空链表if (head.next == tail) {return null;}Node first = head.next;remove(first);return first;}public int size() {return size;}
}

三.具体应用

具体在实现LRU缓存淘汰算法上有应用,只要就是应用的LinkedHashMap的存储的顺序和添加的顺序是一样的特性,我们对于不经常使用的数据进行淘汰处理,这个时候我们需要确保存储的顺序和添加的顺序保持一致性,也就是当内存满了之后,先添加的进行淘汰,具体的讲解明天进行博客的更新!!!!!!!!!

http://www.yayakq.cn/news/914827/

相关文章:

  • 网站后台seo设置英文网站建设需要准备什么
  • 做相片网站海淀教育互动平台网站建设
  • 网站建设网页设计江苏海通建设有限公司网站
  • 网站页面多大合适网站建设策划书目录
  • 通信科技网站设计wordpress cat
  • 网站策划方法中国建设网站银行
  • 定制型网站设计服装网站开发方案
  • 免费无广告建站科技创新绘画作品
  • 苏州网站建设方案策划设计师兼职平台
  • 手机自助建站平台免费网站每年续费给谁
  • 网站字体排版技巧在线照片编辑工具
  • 网站建设是干什么中国建筑网站平台有哪些
  • 彩票网站建设古大学商洛建设公司网站
  • 深圳设计网站哪个好应用公园收费标准
  • 泰安企业网站seo广州网站设计开发招聘
  • 私做网站名电子章设计画册设计
  • 外贸网站建设怎么建设太原企业自助建站
  • 个人网站设计的参考文献网站引流怎么做
  • 优秀app网站设计建设部网站示范文本
  • 网站运营意义大德通众包做网站怎么样
  • 健康饮食网站设计论文高端网站开发建设
  • 小程序开发北京华网天下首选优化防控措施
  • 免费商城网站建设平台网站制作论文题目
  • 做网站设计的长宽一般是多少钱网站建设需要包含什么
  • 多导航织梦网站模板下载地址手机导航下载2022新版
  • 网站开发需要哪些人怎么分工做国际网站多少钱
  • 无锡网站建设哪里好自己做服务器网站
  • 做暧暧暖网站欧美汕头网站快速排名优化
  • 做网站一般费用多少怀化网站建设怎么收费
  • 佛山小企业网站建设网站开发google