咱們都知道HashMap是無序的Map,TreeMap是有序的Map。而LinkedHashMap繼承於HashMap,也是一個有序的Map,這彷佛違背了Hash的理論。node
咱們來看這麼一個例子數組
public class LinkedHashMapTest { public static void main(String[] args) { Map<String,String> test = new HashMap<>(); test.put("化學","93"); test.put("數學","98"); test.put("生物","92"); test.put("英語","97"); test.put("物理","94"); test.put("歷史","96"); test.put("語文","99"); test.put("地理","95"); test.entrySet().stream().forEach(entry -> System.out.println(entry.getKey() + ":" + entry.getValue())); System.out.println("----------------"); Map<String,String> test1 = new LinkedHashMap<>(); test1.put("化學","93"); test1.put("數學","98"); test1.put("生物","92"); test1.put("英語","97"); test1.put("物理","94"); test1.put("歷史","96"); test1.put("語文","99"); test1.put("地理","95"); test1.entrySet().stream().forEach(entry -> System.out.println(entry.getKey() + ":" + entry.getValue())); } }
運行結果數據結構
物理:94
生物:92
歷史:96
化學:93
數學:98
語文:99
英語:97
地理:95
----------------
化學:93
數學:98
生物:92
英語:97
物理:94
歷史:96
語文:99
地理:95app
咱們來看一下LinkedHashMap的源碼,首先它是繼承於HashMapthis
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>
而後它的節點是一個雙向鏈表spa
static class Entry<K,V> extends HashMap.Node<K,V> { Entry<K,V> before, after; Entry(int hash, K key, V value, Node<K,V> next) { super(hash, key, value, next); } }
不只節點是雙向鏈表,它自己的數據結構跟HashMap最大的不一樣,HashMap是由一個可擴容的動態數組來構架的,而LinkedHashMap倒是由雙向鏈表來構架主體數據結構的繼承
在HashMap中rem
transient Node<K,V>[] table;
在LinkedHashMap中 get
transient LinkedHashMap.Entry<K,V> head; transient LinkedHashMap.Entry<K,V> tail;
因爲繼承於HashMap,LinkedHashMap沒有重寫put()方法源碼
在HashMap中
public V put(K key, V value) { return putVal(hash(key), key, value, false, true); }
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; //若是表爲空,rehash全表 if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; //若是表的該hash位置爲空,新建節點 if ((p = tab[i = (n - 1) & hash]) == null) tab[i] = newNode(hash, key, value, null); else { Node<K,V> e; K k; //若是Hash衝突,且插入的key與節點key相同,將新節點p賦給e if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; //若是p爲紅黑樹節點,將紅黑樹節點p賦給e else if (p instanceof TreeNode) e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); //不然即爲鏈表節點,進行相應處理 else { for (int binCount = 0; ; ++binCount) { if ((e = p.next) == null) { p.next = newNode(hash, key, value, null); if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st treeifyBin(tab, hash); break; } if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } if (e != null) { // existing mapping for key V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; } } ++modCount; if (++size > threshold) resize(); afterNodeInsertion(evict); return null; }
咱們在putVal()方法中能夠看到有一個newNode()的方法,則LinkedHashMap重寫了該方法newNode()
在LinkedHashMap中
Node<K,V> newNode(int hash, K key, V value, Node<K,V> e) { //新增節點 LinkedHashMap.Entry<K,V> p = new LinkedHashMap.Entry<K,V>(hash, key, value, e); //將新節點插入到雙向鏈表的尾部 linkNodeLast(p); return p; }
private void linkNodeLast(LinkedHashMap.Entry<K,V> p) { LinkedHashMap.Entry<K,V> last = tail; tail = p; if (last == null) head = p; else { p.before = last; last.after = p; } }
咱們在HashMap的put()方法中看到有兩個方法afterNodeAccess(e),afterNodeInsertion(evict);
void afterNodeAccess(Node<K,V> p) { } void afterNodeInsertion(boolean evict) { }
它們在HashMap中的方法體爲空。
而在LinkedHashMap中也重寫了這兩個方法
void afterNodeAccess(Node<K,V> e) { // move node to last LinkedHashMap.Entry<K,V> last; if (accessOrder && (last = tail) != e) { LinkedHashMap.Entry<K,V> p = (LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after; p.after = null; if (b == null) head = a; else b.after = a; if (a != null) a.before = b; else last = b; if (last == null) head = p; else { p.before = last; last.after = p; } tail = p; ++modCount; } }
void afterNodeInsertion(boolean evict) { // possibly remove eldest LinkedHashMap.Entry<K,V> first; if (evict && (first = head) != null && removeEldestEntry(first)) { K key = first.key; removeNode(hash(key), key, null, false, true); } }
protected boolean removeEldestEntry(Map.Entry<K,V> eldest) { return false; }