LinkedHashMap源碼分析

//LinkedHashMap繼承HashMap

public class LinkedHashMap

extends HashMap

implements Map

{

/**

* The head of the doubly linked list.

*/

private transient Entry header;

/**

* The iteration ordering method for this linked hash map: true

* for access-order, false for insertion-order.

* LinkedHashMap迭代順序:訪問順序為true;? 插入順序為false

*/

private final boolean accessOrder;

public LinkedHashMap(int initialCapacity, float loadFactor) {

super(initialCapacity, loadFactor);

accessOrder = false;

}

//默認負載因子:0.75

public LinkedHashMap(int initialCapacity) {

super(initialCapacity);

accessOrder = false;

}

//默認大序殉堋:16,默認負載因子:0.75

public LinkedHashMap() {

super();

accessOrder = false;

}

public LinkedHashMap(Map m) {

super(m);

accessOrder = false;

}

//創(chuàng)建LinkedHashMap實例诸尽,可以指定排序模式

? ? public LinkedHashMap(int initialCapacity,

float loadFactor,

boolean accessOrder) {

super(initialCapacity, loadFactor);

this.accessOrder = accessOrder;

}

/**

* Called by superclass constructors and pseudoconstructors (clone,

* readObject) before any entries are inserted into the map.? Initializes

* the chain.

*/

@Override

void init() {

header = new Entry<>(-1, null, null, null);

header.before = header.after = header;

}

/**將table中的元素拷貝到新的數組

? ? ? ? * Transfers all entries to new table array.? This method is called

* by superclass resize.? It is overridden for performance, as it is

* faster to iterate using our linked list.

*/

@Override

void transfer(HashMap.Entry[] newTable, boolean rehash) {

int newCapacity = newTable.length;

for (Entry e = header.after; e != header; e = e.after) {

if (rehash)

e.hash = (e.key == null) ? 0 : hash(e.key);

//計算每個Entry所在的桶

int index = indexFor(e.hash, newCapacity);

//將e作為頭節(jié)點蓄拣,插入這個桶里的entry鏈表

e.next = newTable[index];

//將新的鏈表放在這個桶里

newTable[index] = e;

}

}

public boolean containsValue(Object value) {

// Overridden to take advantage of faster iterator

if (value==null) {

for (Entry e = header.after; e != header; e = e.after)

if (e.value==null)

return true;

} else {

for (Entry e = header.after; e != header; e = e.after)

if (value.equals(e.value))

return true;

}

return false;

}

public V get(Object key) {

Entry e = (Entry)getEntry(key);

if (e == null)

return null;

e.recordAccess(this);

return e.value;

}

//判斷Entry鏈表上某個entry是否相等驰后,1:hash code相同癞埠,2:key相同

? ? final Entry getEntry(Object key) {

int hash = (key == null) ? 0 : hash(key);

for (Entry e = table[indexFor(hash, table.length)];

e != null;

e = e.next) {

Object k;

if (e.hash == hash &&

((k = e.key) == key || (key != null && key.equals(k))))

return e;

}

return null;

}

public void clear() {

super.clear();

header.before = header.after = header;

}

private static class Entry extends HashMap.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);

}

/**

* Removes this entry from the linked list.

*/

private void remove() {

before.after = after;

after.before = before;

}

/**

* Inserts this entry before the specified existing entry in the list.

*/

private void addBefore(Entry existingEntry) {

after? = existingEntry; //after --> this.after

before = existingEntry.before; //before --> this.before

before.after = this;

after.before = this;

}

/**

* This method is invoked by the superclass whenever the value

* of a pre-existing entry is read by Map.get or modified by Map.set.

* If the enclosing Map is access-ordered, it moves the entry

* to the end of the list; otherwise, it does nothing.

*/

void recordAccess(HashMap m) {

LinkedHashMap lm = (LinkedHashMap)m;

if (lm.accessOrder) {

lm.modCount++;

remove();

addBefore(lm.header);

}

}

void recordRemoval(HashMap m) {

remove();

}

}

/**

* This override alters behavior of superclass put method. It causes newly

* allocated entry to get inserted at the end of the linked list and

* removes the eldest entry if appropriate.

*/

void addEntry(int hash, K key, V value, int bucketIndex) {

super.addEntry(hash, key, value, bucketIndex);

// Remove eldest entry if instructed

Entry eldest = header.after;

if (removeEldestEntry(eldest)) {

removeEntryForKey(eldest.key);

}

}

void createEntry(int hash, K key, V value, int bucketIndex) {

HashMap.Entry old = table[bucketIndex];

Entry e = new Entry<>(hash, key, value, old);

table[bucketIndex] = e;

//將節(jié)點e,添加到header的前面

? ? ? ? ? ? e.addBefore(header);

size++;

}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市盈罐,隨后出現的幾起案子突雪,更是在濱河造成了極大的恐慌,老刑警劉巖崎淳,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件堪夭,死亡現場離奇詭異,居然都是意外死亡拣凹,警方通過查閱死者的電腦和手機森爽,發(fā)現死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來嚣镜,“玉大人爬迟,你說我怎么就攤上這事【漳洌” “怎么了付呕?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長跌捆。 經常有香客問我徽职,道長,這世上最難降的妖魔是什么佩厚? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任活箕,我火速辦了婚禮,結果婚禮上可款,老公的妹妹穿的比我還像新娘。我一直安慰自己克蚂,他們只是感情好闺鲸,可當我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著埃叭,像睡著了一般摸恍。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天立镶,我揣著相機與錄音壁袄,去河邊找鬼。 笑死媚媒,一個胖子當著我的面吹牛嗜逻,可吹牛的內容都是我干的。 我是一名探鬼主播缭召,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼栈顷,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了嵌巷?” 一聲冷哼從身側響起萄凤,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎搪哪,沒想到半個月后靡努,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡晓折,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年惑朦,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片已维。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡行嗤,死狀恐怖,靈堂內的尸體忽然破棺而出垛耳,到底是詐尸還是另有隱情栅屏,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布堂鲜,位于F島的核電站栈雳,受9級特大地震影響,放射性物質發(fā)生泄漏缔莲。R本人自食惡果不足惜哥纫,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望痴奏。 院中可真熱鬧蛀骇,春花似錦、人聲如沸读拆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽檐晕。三九已至暑诸,卻和暖如春蚌讼,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背个榕。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工篡石, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人西采。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓凰萨,卻偏偏與公主長得像,于是被迫代替她去往敵國和親苛让。 傳聞我的和親對象是個殘疾皇子沟蔑,可洞房花燭夜當晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內容