Java集合之 Map原始碼解析

2021-08-20 11:37:14 字數 3202 閱讀 1020

hashmap:

也是我們平時開發中使用頻率很高的雙列集合,直接父類是abstractmap,是基於hash表儲存的一種集合。

幾個重要的類變數:

//hash表的初始化大小,預設為16.是基於陣列實現的。

static

final

int default_initial_capacity = 1

<< 4; // aka 16

//hash表最大容量,最大值不能超過這個數值。

static

final

int maximum_capacity = 1

<< 30;

//預設載入因子,當我們初始化時預設hash表為16,當鍵值對的大小大於16*0.75=12時,就會觸發擴容

static

final

float default_load_factor = 0.75f;

//樹形化得閾(yu)值,當hash表中某乙個桶位置儲存鍊錶的長度大於8時,將鍊錶轉換為紅黑樹,至於為什麼是8,因為理想狀態下雜湊表的每個箱子中,元素的數量遵守泊松分布,當鍊表長度大於8的時候,得出的概率幾乎為0.

static

final

int treeify_threshold = 8;

//當紅黑樹的大小小於6,解散紅黑樹

static

final

int untreeify_threshold = 6;

//在轉變成樹之前,還會有一次判斷,只有鍵值對數量大於 64 才會發生轉換。這是為了避免在雜湊表建立初期,多個鍵值對恰好被放入了同乙個鍊錶中而導致不必要的轉化。

static

final

int min_treeify_capacity = 64;

//map容量

transient node table;

//儲存鍵值對個數

transient

int size;

//修改計數

transient

int modcount;

//擴容閾值

int threshold;

//載入因子

final

float loadfactor;

//map儲存的基本單元node,

static class nodeimplements map.entry

public final k getkey()

public final v getvalue()

public final string tostring()

//計算當前元素的hash

public final int

hashcode()

public final v setvalue(v newvalue)

public final boolean equals(object o)

return

false;

}}

//新增元素

public v put(k key, v value)

//計算hash,最終會用以這個值作為tab陣列的下標來儲存

//我們不直接 key.hashcode()計算hash的原因是容易出現 雜湊碼 與 陣列大小範圍不匹配的情況,即 計算出來的雜湊碼可能 不在陣列大小範圍內,從而導致無法匹配儲存位置。所以我們通過[ 雜湊碼 與運算(&) (陣列長度-1)],根據hashmap的容量大小(陣列長度),按需取 雜湊碼一定數量的低位 作為儲存的陣列下標位置,從而 解決 「雜湊碼與陣列大小範圍不匹配」 的問題

static final int hash(object key)

final v putval(int hash, k key, v value, boolean onlyifabsent,

boolean evict)

if (e.hash == hash &&

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

break;

p = e;}}

v oldvalue = e.value;

if (!onlyifabsent || oldvalue == null)

e.value = value;

afternodeaccess(e);

return oldvalue;}}

++modcount;

//tab大小比擴容閾值大,進行擴容。

if (++size > threshold)

resize();

afternodeinsertion(evict);

return

null;

}

//擴容 :該函式有2種使用情況:1.初始化雜湊表 2.當前陣列容量過小,需擴容

final node resize()

// 若無超過最大值,就擴充為原來的2倍

else

if ((newcap = oldcap << 1) < maximum_capacity &&

oldcap >= default_initial_capacity)

newthr = oldthr << 1; // double threshold

}//初始化雜湊表(採用指定 or 預設值)

else

if (oldthr > 0) // initial capacity was placed in threshold

newcap = oldthr;

else

// 計算新的resize上限

if (newthr == 0)

threshold = newthr;

//@suppresswarnings()

node newtab = (node)new node[newcap];

table = newtab;

if (oldtab != null)

else

} while ((e = next) != null);

if (lotail != null)

if (hitail != null) }}

}}

return newtab;

}

java原始碼解析 Map

an object that maps keys to values.a map cannot contain duplicate keys each key can map to at most one value.將鍵對映到值的物件。不能包含重複的鍵 每個鍵最多可以對映乙個值。map 的類資訊 ...

map原始碼解析

public v put k key,v value 若沒有在table i 位置找到相同的key,則新增key到table i 位置,新的元素總是在table i 位置的第乙個元素,原來的元素後移 modcount addentry hash,key,value,i return null voi...

Map集合 原始碼分析

map的實現類的結構 map 雙列資料,儲存key value對的資料 hashmap 作為map的主要實現類 執行緒不安全的,效率高 可以儲存null和key的value hashmap的底層 陣列 鍊錶 jdk7之前 陣列 鍊錶 紅黑樹 jdk8 linkedhashmap 保證在遍歷map元素...