final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//如果當前對象爲null或者它內部沒有任何元素,那麼resize()重置一下
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//傳入hash值在當前對象的數組中是否已經有元素,如果沒有就直接new一個Node
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
//執行到這裏,說明發生碰撞,即tab[i]不爲空,需要組成單鏈表或紅黑樹
Node<K,V> e; K k;
//判斷該位置上的Node的hash值和key是否和傳入的參數一致
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
//此時p指的是table[i]中存儲的那個Node,如果待插入的節點中hash值和key值在p中已經存在,則將p賦給e
e = p;
//如果table數組中node類的hash、key的值與將要插入的Node的hash、key不吻合,就需要在這個node節點鏈表或者樹節點中查找。
else if (p instanceof TreeNode)
//當p屬於紅黑樹結構時,則按照紅黑樹方式插入
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
//到這裏說明碰撞的節點以單鏈表形式存儲,for循環用來使單鏈表依次向後查找
for (int binCount = 0; ; ++binCount) {
//查詢到鏈表的最後一個節點也沒有找到,那麼新建一個Node,然後加到第一個元素的後面
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
//如果衝突節點達到8個,調用treeifyBin(tab, hash),這個treeifyBin首先回去判斷當前hash表的長度,如果不足64的話,實際上就只進行resize,擴容table,如果已經達到64,那麼纔會將衝突項存儲結構改爲紅黑樹。
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//如果有相同的hash和key,則退出循環
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
//將p調整爲下一個節點
p = e;
}
}
//若e不爲null,表示已經存在與待插入節點hash、key相同的節點,hashmap後插入的key值對應的value會覆蓋以前相同key值對應的value值,就是下面這塊代碼實現的
if (e != null) { // existing mapping for key
V oldValue = e.value;
//判斷是否修改已插入節點的value
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
//HashMap中節點數+1,如果大於threshold,那麼要進行一次擴容
resize();
afterNodeInsertion(evict);
return null;
}
//初始化或者是將table大小加倍。如果爲空,則按threshold分配空間,否則,加倍後,每個容器中的元素在新table中要麼呆在原索引處,要麼有一個2的次冪的位移
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) {
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
}
// 如果oldCap<=0,初始容量爲閾值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"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
//當原來的table不爲null時,需要將table[i]中的節點遷移
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;
else if (e instanceof TreeNode)
//紅黑樹分裂
((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;
}
HashMap.put(K key, V value)源碼分析
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.