看了下HashMap的源碼,做下記錄,首先還是先從流程圖開始
下面用代碼分析下方法
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//未初始化
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//該索引處節點無值
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
//該節點處有值
else {
Node<K,V> e; K k;
//和該處節點的 hash 相同並且 key相同
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//是紅黑樹節點
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
//是鏈表
else {
for (int binCount = 0; ; ++binCount) {
//尾部節點
if ((e = p.next) == null) {
//添加到尾部
p.next = newNode(hash, key, value, null);
//大於等於7 轉為樹節點
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//如果鏈表不是尾部節點,並且是否遇到了key和hash都相同的
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
// 找到了 key和value都相同的節點
if (e != null) { // existing mapping for key
V oldValue = e.value;
// value不為null 並且 onlyIfAbsent為false 就重新賦下值
if (!onlyIfAbsent || oldValue == null)
e.value = value;
// 一個待實現的方法,hashMap無用
afterNodeAccess(e);
// 結束
return oldValue;
}
}
//結構更改的計數,為了在迭代中快速判斷是否被修改了 而拋出異常
++modCount;
// 長度++,擴容閾值
if (++size > threshold)
resize();
//一個待實現的方法,hashMap無用
afterNodeInsertion(evict);
return null;
}
//初始化和擴容
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;
}
//新容量為舊容量*2 並且小於最大值,且舊容量大於最小的初始化容量
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
//新閾值也為舊閾值*2
newThr = oldThr << 1;
}
//舊閾值不為0,新的容量就是舊的閾值
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
//兩個值都是0,此時是初始化代碼
else {
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
//新閾值為0時 重新計算
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復制到新table
if (oldTab != null) {
//從低到高復制
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
//單純一個Node對象結構,直接復制
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
//把原始的鏈表拆成兩個,判斷標准就是 hash& oldCap ==0
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);
//拆完之后,新table的j位置放lohead
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
//拆完后,新table的j+oldCap的位置放hihead
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}