1、简述

    LinkedHashMap是HashMap的子类,他们最大的不同是,HashMap内部维护的是一个单向的链表数组,而LinkedHashMap内部维护的是一个双向的链表数组。HashMap是无序的,LinkedHashMap可以根据访问顺序或者插入顺序进行排序(默认是根据插入顺序的,当设置accessOrder为true时会按照访问顺序排序),当按照访问顺序排序的时候,每次get或put操作时,都会将该节点放到链表的末尾

2、实现

  1、数据结构:

    LinkedHashMap是HashMap的子类,它的内部同样是使用了hashMap定义的链表数组存储的数据。只不过在HashMap中使用的是单想链表Node<k,v>作为每个节点,而LinkedHashMap使用了集成了HashMap.Node<k,v>的双向链表Entry<k,v>来作为每个节点,Entry<k,v>在Node<k,v>的基础上增加了Entry<K,V> before, after两个属性,将数组中的前后元素关联起来,这样的实现也为LinkedHashMap根据插入顺序或访问顺序提供了可能

  2、构造方法:

    LinkedHashMap是HashMap的子类,它提供了五种构造方法,

 public LinkedHashMap(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
accessOrder = false;
}
public LinkedHashMap(int initialCapacity) {
super(initialCapacity);
accessOrder = false;
}
public LinkedHashMap() {
super();
accessOrder = false;
}
public LinkedHashMap(Map<? extends K, ? extends V> m) {
super();
accessOrder = false;
putMapEntries(m, false);
}
public LinkedHashMap(int initialCapacity,
float loadFactor,
boolean accessOrder) {
super(initialCapacity, loadFactor);
this.accessOrder = accessOrder;
}

    从以上我们可以看出,前面四中构造方法都是默认的accessOrder=false,即按找插入顺序排序,第五种构造方法我们可以自己定义accessOrder的值,当为true时,说明是按照访问顺序排序的,每次put或get操作的时候都会将元素移到链表的末尾

  3、LinkedHashMap操作

      由于LinkedHashMap继承了HashMap类,而且并没有重写put,get等方法,所以在这里对于LinkedHashMap的基础操作的源码就不再分析,感兴趣的可以参考上一篇关于HashMap的学习笔记。在这里重点看一下LinkedHashMap提供的removeEldestEntry(Map.Entry<K,V> eldest)方法

    

/**
* Returns <tt>true</tt> if this map should remove its eldest entry.
* This method is invoked by <tt>put</tt> and <tt>putAll</tt> after
* inserting a new entry into the map. It provides the implementor
* with the opportunity to remove the eldest entry each time a new one
* is added. This is useful if the map represents a cache: it allows
* the map to reduce memory consumption by deleting stale entries.
*
* <p>Sample use: this override will allow the map to grow up to 100
* entries and then delete the eldest entry each time a new entry is
* added, maintaining a steady state of 100 entries.
* <pre>
* private static final int MAX_ENTRIES = 100;
*
* protected boolean removeEldestEntry(Map.Entry eldest) {
* return size() &gt; MAX_ENTRIES;
* }
* </pre>
*
* <p>This method typically does not modify the map in any way,
* instead allowing the map to modify itself as directed by its
* return value. It <i>is</i> permitted for this method to modify
* the map directly, but if it does so, it <i>must</i> return
* <tt>false</tt> (indicating that the map should not attempt any
* further modification). The effects of returning <tt>true</tt>
* after modifying the map from within this method are unspecified.
*
* <p>This implementation merely returns <tt>false</tt> (so that this
* map acts like a normal map - the eldest element is never removed).
*
* @param eldest The least recently inserted entry in the map, or if
* this is an access-ordered map, the least recently accessed
* entry. This is the entry that will be removed it this
* method returns <tt>true</tt>. If the map was empty prior
* to the <tt>put</tt> or <tt>putAll</tt> invocation resulting
* in this invocation, this will be the entry that was just
* inserted; in other words, if the map contains a single
* entry, the eldest entry is also the newest.
* @return <tt>true</tt> if the eldest entry should be removed
* from the map; <tt>false</tt> if it should be retained.
*/
protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
return false;
}

    这个方法就是判断是否需要删除LinkedHashMap中最旧的元素,默认是false的。当我们需要利用hashmap做一些数据的缓存,可能有的数据是使用了一两次之后就失去了价值,此外我们也不想看到缓存的map无限的增长又不想自己手动的去维护这个map。此时我们就可以使用LinkedHashMap来完成这个功能;

public class Demo {
static class MyMap<k,v> extends LinkedHashMap<k,v>{ //定义一个继承LinkedHashMap的类,并重写removeEldestEntry方法
int size;
public MyMap (int size){ //只提供一个有参的构造方法,用来设置map的大小
/*
* 这里调用的是LinkedHashMap的LinkedHashMap(int initialCapacity,boolean accessOrder)
* 构造方法,设置按访问顺序排序
*/
super(16,0.75f,true);
this.size = size;
}
@Override
protected boolean removeEldestEntry(Entry<k, v> eldest) {
// TODO Auto-generated method stub
return this.size<size(); //设置当map里面存放的元素超过MyMap设置的size时就删除最老的元素
} }
public static void main(String[] args) {
Map<String,String> map = new MyMap<String,String>(5);
for (int i = 0; i < 10; i++) {
map.put(i+"", i+"");
System.out.print(i+"="+map.size()+"\t");
}
System.out.println();
//遍历map看看里面的元素
map.forEach((k,v)->{
System.out.println("k="+k);
}); //此时访问一下里面的元素,再观察遍历后的顺序
map.get("6");map.get("8");
System.out.println("========================");
map.forEach((k,v)->{
System.out.println("k="+k);
});
}
} 打印的结果:
  

0=1 1=2 2=3 3=4 4=5 5=5 6=5 7=5 8=5 9=5
k=5
k=6
k=7
k=8
k=9
========================
k=5
k=7
k=9
k=6
k=8

 

  

总结:

    在学完LinkedHashMap和HashMap之后我们会发现,这两种集合类是非常相似的,二者都是通过内部数组去保存数据的,不同的是HashMap数组中的每个元素都是一个单向的链表,指向了产生hash冲突时和它本身具有相同hash值的元素,所以HashMap只能是无序的。LinkedHashMap中数组里面的每一个元素都是一个双向链表,它不仅指向了产生hash冲突时下一个跟它本身具有相同hash值的位置,还指定了它的上一个和下一个元素(可以是插入顺序的上一个下一个,也可以试访问顺序的上一个下一个),这样为LinkedHashMap实现有序提供了可能

java集合类学习笔记之LinkedHashMap的更多相关文章

  1. java集合类学习笔记之HashMap

    1.简述 HashMap是java语言中非常典型的数据结构,也是我们平常用的最多的的集合类之一.它的底层是通过一个单向链表(Node<k,v>)数组(也称之为桶bucket,数组的长度也叫 ...

  2. java集合类学习笔记之ArrayList

    1.简述 ArrayList底层的实现是使用了数组保存所有的数据,所有的操作本质上是对数组的操作,每一个ArrayList实例都有一个默认的容量(数组的大小,默认是10),随着 对ArrayList不 ...

  3. Java集合类学习笔记2

    二,具体的集合 集合类型 描述 ArrayList 一种可以动态增长和缩减的索引序列 LinkedList 一种可以在任何位置进行高效地插入和删除操作的有序序列 ArrayDeque 一种用循环数组实 ...

  4. java集合类学习笔记1

    一.集合的接口 java集合类库也将接口与实现相分离.首先看一下大家都熟悉的数据结构-队列是如何分离的.队列接口指出可以在队列的尾部添加元素,在队列的头部删除元素,并且可以查找队列中元素的个数.当需要 ...

  5. Java集合类学习笔记(各种Map实现类的性能分析)

    HashMap和Hashtable的实现机制几乎一样,但由于Hashtable是一个古老的.线程安全的集合,因此HashMap通常比Hashtable要快. TreeMap比HashMap和Hasht ...

  6. Java集合类学习笔记(Map集合)

    Map用于保存具有映射关系的数据,因此Map集合里保存着两组数据,一组用于保存Map的key,一组用于保存key所对应的value. Map的key不允许重复. HashMap和Hashtable都是 ...

  7. Java集合类学习笔记(Set集合)

    Set集合不允许包含相同的元素,如果试图把两个相同的元素加入同一个Set集合中,则添加操作失败,add()方法返回false,且新元素不会被加入. HashSet类的特点: 不能保证元素的排列顺序,顺 ...

  8. Java集合类学习笔记(各种线性表性能分析)

    ArrayList.LinkedList是线性表的两种典型实现:基于数组的线性表和基于链的线性表. Queue代表了队列,Deque代表了双端队列. 一般来说,由于数组以一块连续内存区来保存所有的数组 ...

  9. Java集合类学习笔记(Queue集合)

    Queue集合用于模拟队列(先进先出:FIFO)这种数据类型. Queue有一个Deque接口,代表一个"双端队列",双端队列可以同时从两端来添加.删除元素,因此Deque的实现类 ...

随机推荐

  1. spring bean单例注入与用单例模式通过class.getinstance()区别?

    1.action的某个方法中,用以下代码获得redis单例实例 RedisDelegate redisDelegate = RedisDelegate.getInstance(); redisDele ...

  2. C#在控制台输出异常所在的行数

    对于异常,我们经常用try-catch语句来处理,一种常见的方式是在catch语句块用MessageBox.Show("异常")这种弹窗的方式来报告异常.但是有些时候,有些异常发生 ...

  3. Zookeeper使用--Java API

    1  创建节点 创建节点有异步和同步两种方式.无论是异步或者同步,Zookeeper都不支持递归调用,即无法在父节点不存在的情况下创建一个子节点,如在/zk-ephemeral节点不存在的情况下创建/ ...

  4. Openssl oscp命令

    一.简介 ocsp,在线证书状态命,能够执行很多OCSP的任务,可以被用于打印请求文件和响应文件, 二.语法 openssl ocsp [-out file] [-issuer file] [-cer ...

  5. C#中把任意类型的泛型集合转换成SQLXML数据格式的小例子

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  6. MySQL 存储过程 -光标的使用

    #四.光标的使用 #声明光标 语法:DECLARE 光标名字 CURSOR FOR sql语句 #打开光标 OPEN 光标名称 #使用光标 FETCH 光标名称 into ... #关闭光标 CLOS ...

  7. [GO]单向channel和应用

    var ch1 chan int  //ch1是一个正常的channel,不是单向的 var ch2 chan <- float64   //ch2是一个单向的channel,只用于写float ...

  8. NET Framework V4.0.30319

    http://www.microsoft.com/zh-cn/download/details.aspx?id=17718

  9. 一句话说说java设计模式

    设计模式 看到标题,大家是不是觉得不可思议,java的23种设计模式那么‘复杂’,那么‘难懂’,用一句话怎么说的明白呢? 首先,各位看官不要误解,近来在回头看设计模式,之前都看过,但时间是把‘杀猪刀’ ...

  10. 编写高质量代码改善C#程序的157个建议——建议73:避免锁定不恰当的同步对象

    建议73:避免锁定不恰当的同步对象 在C#中,让线程同步的另一种编码方式就是使用线程锁.线程锁的原理,就是锁住一个资源,使得应用程序在此刻只有一个线程访问该资源.通俗地讲,就是让多线程变成单线程.在C ...