【源码】LinkedHashMap源码剖析

来源:互联网 发布:二叉树的前序遍历java 编辑:程序博客网 时间:2024/04/20 01:10

注:以下源码基于jdk1.7.0_11

之前的两篇文章通过源码分析了两种常见的Map集合,HashMap和Hashtable。本文将继续介绍另一种Map集合——LinkedHashMap。
顾名思义,LinkedHashMap除了是一个HashMap之外,还带有LinkedList的特点,也就是说能够保持遍历的顺序和插入的顺序一致,那么它是怎么做到的呢?
下面我们开始分析。
首先看构造器。
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class LinkedHashMap<K,V>  
  2.     extends HashMap<K,V>  
  3.     implements Map<K,V>  

LinkedHashMap直接继承自HashMap,所以拥有HashMap的大部分特性,
比如支持null键和值,默认容量为16,装载因子为0.75,非线程安全等等。
但是LinkedHashMap还有很多个性的地方,
下面来看成员变量:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. private transient Entry<K,V> header;//内部双向链表的头结点  
  2.    /** 
  3.     *代表这个链表的排序方式,true代表按照访问顺序,
  4.     *false代表按照插入顺序。 
  5.     */  
  6. private final boolean accessOrder;  

LinkedHashMap比HashMap多了两个成员变量,
其中header代表内部双向链表的头结点,
后面我们就会发现,LinkedHashMap除了有个桶数组容纳所有Entry之外,还有一个双向链表保存所有Entry引用。
历的时候,并不是去遍历桶数组,而是直接遍历双向链表,
所以LinkedHashMap的遍历时间不受桶容量的限制,这是它和HashMap的重要区别之一。
而这个accessOrder代表的是是否按照访问顺序,true代表是,默认是插入顺序。
所以我们可以将accessOrder置为true来实现LRU算法,这可以用来做缓存。
再看构造器:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public LinkedHashMap(int initialCapacity, float loadFactor) {  
  2.         super(initialCapacity, loadFactor);  
  3.         accessOrder = false;  
  4.     }  
  5.     public LinkedHashMap(int initialCapacity) {  
  6.         super(initialCapacity);  
  7.         accessOrder = false;  
  8.     }  
  9.     public LinkedHashMap() {  
  10.         super();  
  11.         accessOrder = false;  
  12.     }  
  13.     public LinkedHashMap(Map<? extends K, ? extends V> m) {  
  14.         super(m);  
  15.         accessOrder = false;  
  16.     }  
  17.    public LinkedHashMap(int initialCapacity,  
  18.                          float loadFactor,  
  19.                          boolean accessOrder) {  
  20.         super(initialCapacity, loadFactor);  
  21.         this.accessOrder = accessOrder;  
  22.     }  

构造器首先都会调用父类也就是HashMap的构造器来初始化桶数组,
而accessOrder之后会被初始化,
除了最后面的一个构造器允许指定accessOrder外,
其他构造器都默认将accessOrder置为了false
读者可能很奇怪,不是还有个header么,这个双向链表为啥不在构造器中初始化呢?
这得回到HashMap中查看hashMap的构造器了:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public HashMap(int initialCapacity, float loadFactor) {  
  2.         if (initialCapacity < 0)  
  3.             throw new IllegalArgumentException("Illegal initial capacity: " +  
  4.                                                initialCapacity);  
  5.        ... ...  
  6.         init();  
  7.     }  

HashMap构造器最后一步调用了一个init方法,而这个init方法在HashMap中是个空实现,没有任何代码。
这其实就是所谓的“钩子”,
具体代码由子类实现,
如果子类希望每次构造的时候都去做一些特定的初始化操作,
可以选择复写init方法。
我们看到LinkedHashMap中确实复写了init:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Override  
  2.    void init() {  
  3.        header = new Entry<>(-1nullnullnull);//初始化双向链表  
  4.        header.before = header.after = header;//不光是双向链表,还是循环链表  
  5.    }  

在init方法中,果然初始化了双向链表,而且我们还发现,这不光是个双向链表,还是个循环链表。
HashMap内部的Entry类并没有before和after指针,
也就是说LinkedHashMap自己重写了一个Entry类
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. private static class Entry<K,V> extends HashMap.Entry<K,V> {  
  2.         // These fields comprise the doubly linked list used for iteration.  
  3.         Entry<K,V> before, after;//前驱、后继指针  
  4.         Entry(int hash, K key, V value, HashMap.Entry<K,V> next) {  
  5.             super(hash, key, value, next);  
  6.         }  
  7.         /** 
  8.          * Removes this entry from the linked list. 
  9.          */  
  10.         private void remove() {  
  11.             before.after = after;  
  12.             after.before = before;  
  13.         }  
  14.         /** 
  15.          * Inserts this entry before the specified existing entry in the list. 
  16.          */  
  17.         private void addBefore(Entry<K,V> existingEntry) {  
  18.             after  = existingEntry;  
  19.             before = existingEntry.before;  
  20.             before.after = this;  
  21.             after.before = this;  
  22.         }  
  23.         /** 
  24.          * This method is invoked by the superclass whenever the value 
  25.          * of a pre-existing entry is read by Map.get or modified by Map.set. 
  26.          * If the enclosing Map is access-ordered, it moves the entry 
  27.          * to the end of the list; otherwise, it does nothing. 
  28.          */  
  29.         void recordAccess(HashMap<K,V> m) {  
  30.             LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m;  
  31.             if (lm.accessOrder) {  
  32.                 lm.modCount++;  
  33.                 remove();  
  34.                 addBefore(lm.header);  
  35.             }  
  36.         }  
  37.         void recordRemoval(HashMap<K,V> m) {  
  38.             remove();  
  39.         }  
  40.     }  
这里的Entry选择继承父类的Entry类,
也就是说LinkedHashMap中的Entry拥有三个指针,
除了前驱后继指针外用于双向链表的连接外,
还有一个next指针用于解决hash冲突(引用链)。
除此之外,Entry新增了几个方法,remove和addbefore用来操作双向链表不用多说。
而recordAccess方法比较特殊,这个方法在HashMap中也是空实现,并在put方法中会调用此方法:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public V put(K key, V value) {//HashMap的put方法  
  2.        if (key == null)  
  3.            return putForNullKey(value);  
  4.        int hash = hash(key);  
  5.        int i = indexFor(hash, table.length);  
  6.        for (Entry<K,V> e = table[i]; e != null; e = e.next) {  
  7.            Object k;  
  8.            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {  
  9.                V oldValue = e.value;  
  10.                e.value = value;  
  11.                e.recordAccess(this);//发生覆盖操作时,会调用此方法  
  12.                return oldValue;  
  13.            }  
  14.        }  
  15.      ... ...  
  16.    }  

此外,在LinkedHashMap的get方法中,也会调用此方法:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public V get(Object key) {  
  2.        Entry<K,V> e = (Entry<K,V>)getEntry(key);  
  3.        if (e == null)  
  4.            return null;  
  5.        e.recordAccess(this);  
  6.        return e.value;  
  7.    }  

也就是说,只要涉及到访问结点,那么就会调用这个方法。
观察该方法的逻辑:
如果accessOrder为true,
那么会调用addBefore方法将当前Entry放到双向链表的尾部,
最终在我们遍历链表的时候就会发现最近最少使用的结点的都集中在链表头部(从近期访问最少到近期访问最多的顺序),
这就是LRU。

LinkedHashMap并没有复写put方法,
但是却复写了addEntry和createEntry方法,
之前分析HashMap的时候我们就知道了,
put方法会调用addEntry将键值对挂到桶的某个合适位置,
而addEntry又会调用createEntry方法创建一个键值对对象。
因而,LinkedHashMap其实是间接更改了put方法,
想想也很容易理解,LinkedHashMap除了要向桶中添加键值对外,
还需向链表中增加键值对,所以必须得修改put方法。
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. void addEntry(int hash, K key, V value, int bucketIndex) {  
  2.         super.addEntry(hash, key, value, bucketIndex);  
  3.         // Remove eldest entry if instructed  
  4.         Entry<K,V> eldest = header.after;//标记最少访问的对象  
  5.         if (removeEldestEntry(eldest)) {//判断是否需要删除这个对象---->可由子类实现来提供缓存功能  
  6.             removeEntryForKey(eldest.key);  
  7.         }  
  8.     }  
  9.     void createEntry(int hash, K key, V value, int bucketIndex) {  
  10.         HashMap.Entry<K,V> old = table[bucketIndex];  
  11.         Entry<K,V> e = new Entry<>(hash, key, value, old);  
  12.         table[bucketIndex] = e;  
  13.         e.addBefore(header);//添加到链表尾部  
  14.         size++;  
  15.     }  

createEntry方法会将键值对分别挂到桶数组和双向链表中。
比较有意思的是addEntry方法,它提供了一个可选的操作,
我们可以通过继承LinkedHashMap并复写removeEldestEntry方法
让该子类可以自动地删除最近最少访问的键值对——这可以用来做缓存!!

LinkedHashMap自定义了迭代器以及迭代规则,
LinkedHashMap是通过内部的双向链表来完成迭代的,
遍历时间与键值对总数成正比,而HashMap遍历时间与容量成正比,
所以通常情况下,LinkedHashMap遍历性能是优于HashMap的,
但是因为需要额外维护链表,所以折中来看,两者性能相差无几。
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. private abstract class LinkedHashIterator<T> implements Iterator<T> {  
  2.        Entry<K,V> nextEntry    = header.after;//指向链表首部  
  3.        Entry<K,V> lastReturned = null;  
  4.        int expectedModCount = modCount;  
  5.        public boolean hasNext() {  
  6.            return nextEntry != header;  
  7.        }  
  8.        public void remove() {  
  9.            if (lastReturned == null)  
  10.                throw new IllegalStateException();  
  11.            if (modCount != expectedModCount)  
  12.                throw new ConcurrentModificationException();  
  13.            LinkedHashMap.this.remove(lastReturned.key);  
  14.            lastReturned = null;  
  15.            expectedModCount = modCount;  
  16.        }  
  17.        Entry<K,V> nextEntry() {  
  18.            if (modCount != expectedModCount)  
  19.                throw new ConcurrentModificationException();  
  20.            if (nextEntry == header)  
  21.                throw new NoSuchElementException();  
  22.            Entry<K,V> e = lastReturned = nextEntry;  
  23.            nextEntry = e.after;  
  24.            return e;  
  25.        }  
  26.    }  
总结:
1.LinkedHashMap继承自HashMap,具有HashMap的大部分特性,
比如支持null键和值,默认容量为16,装载因子为0.75,非线程安全等等;
2.LinkedHashMap通过设置accessOrder控制遍历顺序是按照插入顺序还是按照访问顺序。
当accessOrder为true时,可以利用其完成LRU缓存的功能;
3.LinkedHashMap内部维护了一个双向循环链表,并且其迭代操作时通过链表完成的,而不是去遍历hash表。

原文地址:http://blog.csdn.net/chdjj/article/details/38677315
0 0
原创粉丝点击