HashMap 在 java map 中的繼承關(guān)系
image.png
-
底層存儲結(jié)構(gòu): Node 類型數(shù)組
image.png 存儲數(shù)據(jù)的 node 節(jié)點
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next; // 解決 hash 沖突
Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
public final boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
return true;
}
return false;
}
}
-
初始容量及其相關(guān)參數(shù)
- 默認(rèn)容量為 capacity = 16,
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
- 負(fù)載系數(shù): loadFactor = 0.75,
static final float DEFAULT_LOAD_FACTOR = 0.75f;
- 負(fù)載極限: threshold=capacity*loadFactor
- 默認(rèn)容量為 capacity = 16,
- 如何擴容 resize
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) { // double size
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab; // 無法擴容
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold and cap
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab; // 實際擴容操作,復(fù)制舊的數(shù)據(jù)到新的數(shù)組上
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e; // 該節(jié)點不存在 hash 沖突, 賦值
else if (e instanceof TreeNode) // 判斷是否為 TreeNode, 進行 split 處理
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
- 計算出擴容后的 cap 和 threshold
- 如果當(dāng)前 cap 已經(jīng)最大整數(shù)值,則無法擴容或详,調(diào)整 threshold 到最大整數(shù)值
- 如果 dobue cap 小于最大整數(shù)值逸嘀,那么 cap 和 threshold 都翻倍
- 如果 double cap 大于最大整數(shù)值, 重新計算 threshold=newCap * loadFactor;
- 實際擴容操作搏屑,遍歷舊的數(shù)組
- 如果當(dāng)前 index 的元素為 null钙态,繼續(xù)便利下個元素
- 如果當(dāng)前 index 的元素為 TreeNode,調(diào)用 split
- 分兩種情況: 定義兩個鏈表: hiHead,loHead
* e.hash & oldCap 當(dāng)前: (hash 值 & oldCap) == 0, 則該節(jié)點在新表的下標(biāo)位置與舊表一致都為 j畜挥,放入 loHead
* (e.hash & oldCap) == 1 則該節(jié)點在新表的下標(biāo)位置 j + oldCap甫菠,放入 hiHead - 迭代 lohead 元素: newTab[j] = loHead;
-
迭代 hihead 元素: newTab[j + oldCap] = hiHead;
image.png
resize 如何處理 TreeNode 類型節(jié)點
put 方法解析
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;
// table未初始化或者長度為0挠铲,進行擴容
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
// (n - 1) & hash 確定元素存放在哪個桶中,桶為空寂诱,新生成結(jié)點放入桶中(此時拂苹,這個結(jié)點是放在數(shù)組中)
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
// 桶中已經(jīng)存在元素
else {
Node<K,V> e; K k;
// 比較桶中第一個元素(數(shù)組中的結(jié)點)的hash值相等,key相等
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
// 將第一個元素賦值給e痰洒,用e來記錄
e = p;
// hash值不相等瓢棒,即key不相等;為紅黑樹結(jié)點
else if (p instanceof TreeNode)
// 放入樹中
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
// 為鏈表結(jié)點
else {
// 在鏈表最末插入結(jié)點
for (int binCount = 0; ; ++binCount) {
// 到達(dá)鏈表的尾部
if ((e = p.next) == null) {
// 在尾部插入新結(jié)點
p.next = newNode(hash, key, value, null);
// 結(jié)點數(shù)量達(dá)到閾值丘喻,轉(zhuǎn)化為紅黑樹
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
// 跳出循環(huán)
break;
}
// 判斷鏈表中結(jié)點的key值與插入的元素的key值是否相等
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
// 相等脯宿,跳出循環(huán)
break;
// 用于遍歷桶中的鏈表,與前面的e = p.next組合仓犬,可以遍歷鏈表
p = e;
}
}
// 表示在桶中找到key值嗅绰、hash值與插入元素相等的結(jié)點
if (e != null) {
// 記錄e的value
V oldValue = e.value;
// onlyIfAbsent為false或者舊值為null
if (!onlyIfAbsent || oldValue == null)
//用新值替換舊值
e.value = value;
// 訪問后回調(diào)
afterNodeAccess(e);
// 返回舊值
return oldValue;
}
}
// 結(jié)構(gòu)性修改
++modCount;
// 實際大小大于閾值則擴容
if (++size > threshold)
resize();
// 插入后回調(diào)
afterNodeInsertion(evict);
return null;
}
- 斷數(shù)組是否已經(jīng)初始化創(chuàng)建,若無創(chuàng)建數(shù)組
- 若數(shù)組 index = (n - 1) & hash 的元素不存在搀继,創(chuàng)建 Node 并對數(shù)組 tab[index] 賦值
- 判斷數(shù)組 index 的元素是否是 TreeNode,如果是則將放入樹中
- 數(shù)組 index 的鏈表長度沒超過 8 還沒有轉(zhuǎn)化為 紅黑樹翠语,迭代到鏈表尾部叽躯,將數(shù)據(jù)插入,判斷插入后的鏈表長度肌括,如果大于等于 8点骑,則將鏈表轉(zhuǎn)換為紅黑樹