Java源碼解析之LinkedHashMap
先來(lái)看看存儲(chǔ)元素的結(jié)構(gòu)吧:
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); }}
這個(gè)Entry在HashMap中被引用過(guò),主要是為了能讓LinkedHashMap也支持樹(shù)化。在這里則是用來(lái)存儲(chǔ)元素。
// 雙向鏈表的頭,用作AccessOrder時(shí)也是最老的元素transient LinkedHashMap.Entry<K,V> head;// 雙向鏈表的尾,用作AccessOrder時(shí)也是最新的元素transient LinkedHashMap.Entry<K,V> tail;// true則為訪問(wèn)順序,false則為插入順序final boolean accessOrder;二、構(gòu)造函數(shù)
關(guān)于LinkedHashMap的構(gòu)造函數(shù)我們只關(guān)注一個(gè),其他的都和HashMap類似,只是把a(bǔ)ccessOrder設(shè)置為了false。在上邊的文檔說(shuō)過(guò),initialCapacity并沒(méi)有在HashMap中那般重要,因?yàn)殒湵聿恍枰駭?shù)組那樣必須先聲明足夠的空間。下面這個(gè)構(gòu)造函數(shù)是支持訪問(wèn)順序的。
// 雙向鏈表的頭,用作AccessOrder時(shí)也是最老的元素transient LinkedHashMap.Entry<K,V> head;// 雙向鏈表的尾,用作AccessOrder時(shí)也是最新的元素transient LinkedHashMap.Entry<K,V> tail;// true則為訪問(wèn)順序,false則為插入順序final boolean accessOrder;三、重要方法
LinkedHashMap并沒(méi)有再實(shí)現(xiàn)一整套增刪改查的方法,而是通過(guò)復(fù)寫(xiě)HashMap在此過(guò)程中定義的幾個(gè)方法來(lái)實(shí)現(xiàn)的。對(duì)此不熟悉的可以查看上一篇關(guān)于HashMap分析的文章,或者對(duì)照HashMap的源碼來(lái)看。
1、插入一個(gè)元素
HashMap在插入時(shí),調(diào)用了newNode來(lái)新建一個(gè)節(jié)點(diǎn),或者是通過(guò)replacementNode來(lái)替換值。在樹(shù)化時(shí)也有兩個(gè)對(duì)應(yīng)的方法,分別是newTreeNode和replacementTreeNode。完成之后,還調(diào)用了afterNodeInsertion方法,這個(gè)方法允許我們?cè)诓迦胪瓿珊笞鲂┦虑椋J(rèn)是空實(shí)現(xiàn)。
為了方便分析,我們會(huì)對(duì)比HashMap中的實(shí)現(xiàn)與LinkedHashMap的實(shí)現(xiàn),來(lái)摸清它是如何做的。
// HashMap中的實(shí)現(xiàn)Node<K, V> newNode(int hash, K key, V value, Node<K, V> next) { return new Node<>(hash, key, value, next);}// LinkedHashMap中的實(shí)現(xiàn)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;}// HashMap中的實(shí)現(xiàn)Node<K, V> replacementNode(Node<K, V> p, Node<K, V> next) { return new Node<>(p.hash, p.key, p.value, next);}// LinkedHashMap中的實(shí)現(xiàn)Node<K,V> replacementNode(Node<K,V> p, Node<K,V> next) { LinkedHashMap.Entry<K,V> q = (LinkedHashMap.Entry<K,V>)p; LinkedHashMap.Entry<K,V> t =new LinkedHashMap.Entry<K,V>(q.hash, q.key, q.value, next); transferLinks(q, t); return t;}// newTreeNode和replacementTreeNode和此類似
通過(guò)以上對(duì)比,可以發(fā)現(xiàn),LinkedHashMap在新增時(shí),調(diào)用了linkNodeLast,再替換時(shí)調(diào)用了transferLinks。以下是這兩個(gè)方法的實(shí)現(xiàn)。
// 就是將元素掛在鏈尾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; }}// 用dst替換srcprivate void transferLinks(LinkedHashMap.Entry<K,V> src, LinkedHashMap.Entry<K,V> dst) { LinkedHashMap.Entry<K,V> b = dst.before = src.before; LinkedHashMap.Entry<K,V> a = dst.after = src.after; if (b == null)head = dst; elseb.after = dst; if (a == null)tail = dst; elsea.before = dst;}
最后我們看下afterNodeInsertion做了哪些事情吧:
// evict在HashMap中說(shuō)過(guò),為false表示是創(chuàng)建階段void afterNodeInsertion(boolean evict) { // possibly remove eldest LinkedHashMap.Entry<K,V> first; // 不是創(chuàng)建階段 if (evict && (first = head) != null && removeEldestEntry(first)) {K key = first.key;// 自動(dòng)刪除最老的元素,也就是head元素removeNode(hash(key), key, null, false, true); }}
removeEldestEntry是當(dāng)想要在插入元素時(shí)自動(dòng)刪除最老的元素時(shí)需要復(fù)寫(xiě)的方法。其默認(rèn)實(shí)現(xiàn)如下:
protected boolean removeEldestEntry(Map.Entry<K,V> eldest) { return false;}
2、查詢
因?yàn)橐С衷L問(wèn)順序,所以獲取元素的方法和HashMap也有所不同。下面我們看下其實(shí)現(xiàn):
public V get(Object key) { Node<K,V> e; if ((e = getNode(hash(key), key)) == null)return null; if (accessOrder)// 數(shù)據(jù)被訪問(wèn),需要將其移動(dòng)到末尾afterNodeAccess(e); return e.value;}
getNode方法是在HashMap中實(shí)現(xiàn)的,所以這是包裝了一下HashMap的方法,并添加了一個(gè)afterNodeAccess,其實(shí)現(xiàn)如下:
void afterNodeAccess(Node<K,V> e) { // move node to last LinkedHashMap.Entry<K,V> last; // e元素不在末尾 if (accessOrder && (last = tail) != e) {// p是e,b是前一個(gè)元素,a是后一個(gè)元素LinkedHashMap.Entry<K,V> p = (LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;// e要放在末尾,所以沒(méi)有afterp.after = null;// 把e去掉,把b和a接起來(lái)if (b == null) head = a;else b.after = a;if (a != null) a.before = b;else last = b;//把e接在末尾if (last == null) head = p;else { p.before = last; last.after = p;}tail = p;++modCount; }}
到此這篇關(guān)于Java源碼解析之LinkedHashMap的文章就介紹到這了,更多相關(guān)Java LinkedHashMap內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明2. CSS hack用法案例詳解3. ASP 處理JSON數(shù)據(jù)的實(shí)現(xiàn)代碼4. PHP設(shè)計(jì)模式中工廠模式深入詳解5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. asp中response.write("中文")或者js中文亂碼問(wèn)題7. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法8. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)9. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向10. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲
