final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
HashMap.Node<K,V>[] tab; HashMap.Node<K,V> p; int n, i;
// 如果table為空剖踊,則resize(),初始化n,n=16;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//如果當(dāng)前table中沒有值芒划,則創(chuàng)建新的Node
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
//否則更新已有Key的值
else {
HashMap.Node<K,V> e; K k;
//如果當(dāng)前Key和p相等耳奕,則e = p;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//如果是一個(gè)TreeNode房交,則調(diào)用preeVal插入
else if (p instanceof HashMap.TreeNode)
e = ((HashMap.TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
//如果不是TreeNode且不和當(dāng)前的Hash值相等的Key相等
else {
//順著鏈表往后插
for (int binCount = 0; ; ++binCount) {
//e = p.next
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
//如果鏈表長度大于規(guī)定的長度,則插入紅黑樹
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//如果在鏈表中找到了Hash值相等且equals的值則break亲配;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
//如果該Key存在于HashMap中則返回舊的值
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
//修改次數(shù)加1殿较,size+1,然后判斷當(dāng)前size是否大于閾值泽裳,如果大于則resie();
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
image.gif
final HashMap.Node<K,V>[] resize() {
HashMap.Node<K,V>[] oldTab = table;
//如果當(dāng)前table為空,則oldCap = 0增拥,否則 oldCap = table.length;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) {
//如果舊的長度>=MAXIMUM_CAPACITY,則讓閾值變?yōu)镮nteger.MAX_VALUE
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
//否則啄巧,容量變?yōu)樵瓉淼?倍,閾值也變?yōu)樵瓉淼?倍
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
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"})
HashMap.Node<K,V>[] newTab = (HashMap.Node<K,V>[])new HashMap.Node[newCap];
table = newTab;
if (oldTab != null) {
//遍歷舊的table
for (int j = 0; j < oldCap; ++j) {
HashMap.Node<K,V> e;
//如果table[j]!=null
if ((e = oldTab[j]) != null) {
//先將oldTab[j]置為null
oldTab[j] = null;
//如果沒有Hash沖突掌栅,也就是該Node存儲(chǔ)在數(shù)組中秩仆,則將該節(jié)點(diǎn)放到再hash里面的數(shù)組中
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
//如果該節(jié)點(diǎn)是一個(gè)TreeNode
else if (e instanceof HashMap.TreeNode)
((HashMap.TreeNode<K,V>)e).split(this, newTab, j, oldCap);
//否則,也就是存在hash沖突猾封,該節(jié)點(diǎn)后面有鏈表
else { // preserve order
HashMap.Node<K,V> loHead = null, loTail = null;
HashMap.Node<K,V> hiHead = null, hiTail = null;
HashMap.Node<K,V> next;
do {
next = e.next;
//尾插法可以避免JDK7中的頭插法在多線程中產(chǎn)生的鏈表死循環(huán)的問題澄耍,但是仍未解決多線程下數(shù)據(jù)丟失問題
//尾插法新建鏈表 table.size()也就是hash值范圍,hash值范圍在[0,oldCap-1]之間
//eg:oldCap = 1000,最大Hash值為111.
//那么晌缘,如果當(dāng)前Hash值為111&1000==0
//如果當(dāng)前Hash值為1010&1000!=0
//如果當(dāng)前的Hash和高為做&運(yùn)算為0齐莲,則loHead為e,然后一直遍歷e鏈表。
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
//否則磷箕,也就是當(dāng)前hash值在高為有不為0
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
//Hash值不變选酗,直接插入數(shù)組中。
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
//eg:1010&1000!=0,則應(yīng)該插入岳枷,[8+j]處
//否則芒填,插入到[oldCap+j]的位置處呜叫。
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
image.gif
final HashMap.Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
HashMap.Node<K,V>[] tab; HashMap.Node<K,V> p; int n, index;
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
HashMap.Node<K,V> node = null, e; K k; V v;
//如果p在table數(shù)組中,node = p;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
//否則殿衰,遍歷鏈表
else if ((e = p.next) != null) {
//如果是一個(gè)TreeNode朱庆,則調(diào)用getTreeNode獲取節(jié)點(diǎn)
if (p instanceof HashMap.TreeNode)
node = ((HashMap.TreeNode<K,V>)p).getTreeNode(hash, key);
//否則,是一個(gè)鏈表中的節(jié)點(diǎn)播玖,遍歷鏈表椎工,直到找到相等的Node。
else {
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
//如果找到了node
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
//如果是一個(gè)TreeNode蜀踏,則調(diào)用removeTreeNode刪除節(jié)點(diǎn)
if (node instanceof HashMap.TreeNode)
((HashMap.TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
//如果是在數(shù)組中的節(jié)點(diǎn)维蒙,則直接將當(dāng)前node的next放入數(shù)組中
else if (node == p)
tab[index] = node.next;
//如果是鏈表中的節(jié)點(diǎn),則當(dāng)前刪除node是通過改變node上一個(gè)節(jié)點(diǎn)的next,將p->node-node.next改為p->node.next刪除node
else
p.next = node.next;
//將修改次數(shù)加一
++modCount;
//將當(dāng)前hashMap的size-1
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}