LinkedHashMap

 本質上,HashMap和雙向鏈表合二為一即是LinkedHashMap。所謂LinkedHashMap,其落腳點在HashMap,因此更準確地說,它是一個將所有Entry節點鏈入一個雙向鏈表雙向鏈表的HashMap。在LinkedHashMapMap中,所有put進來的Entry都保存在如下面第一個圖所示的哈希表中,但由于它又額外定義了一個以head為頭結點的雙向鏈表(如下面第二個圖所示),因此對于每次put進來Entry,除了將其保存到哈希表中對應的位置上之外,還會將其插入到雙向鏈表的尾部。


4、基本元素 Entry

LinkedHashMap采用的hash算法和HashMap相同,但是它重新定義了Entry。LinkedHashMap中的Entry增加了兩個指針?before?和?after,它們分別用于維護雙向鏈接列表。特別需要注意的是,next用于維護HashMap各個桶中Entry的連接順序,before、after用于維護Entry插入的先后順序的,源代碼如下:

privatestaticclassEntryextendsHashMap.Entry {// These fields comprise the doubly linked list used for iteration.Entry before, after;? ? Entry(inthash, K key, V value, HashMap.Entry next) {super(hash, key, value, next);? ? }? ? ...}

private static classEntryextendsHashMap.Entry { // These fields comprise the doubly linked list used for iteration. Entry before, after;

? ? Entry(int hash, K key, V value, HashMap.Entry next) {

? ? ? ? super(hash, key, value, next);

? ? }

? ? ...

}

形象地,HashMap與LinkedHashMap的Entry結構示意圖如下圖所示:

http://blog.csdn.net/justloveyou_/article/details/71713781

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容