源碼:數組
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) {//若是舊的容量是大於0
if (oldCap >= MAXIMUM_CAPACITY) {//若是舊的容量超過最大容量,閾值設置爲integer最大值,返回舊的數組
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)//容量擴充1倍
newThr = oldThr << 1; // double threshold //若是新的容量小於最大容量而且舊的容量大於等於初始化的容量16時,新的閾值擴充1倍
}
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;
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {//遍歷舊的數組
Node<K,V> e;
if ((e = oldTab[j]) != null) {//若是數組中對應的值不爲null
oldTab[j] = null;//舊的數組對應值引用爲null
if (e.next == null) //e指向的next爲null,說明只有一個值,直接根據hash取模移到新的數組中
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)//若是是紅黑樹,走紅黑樹的邏輯
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
//位置不變的鏈表 低位=0 頭和尾
Node<K,V> loHead = null, loTail = null;
//位置變的鏈表 高位=0 頭和尾
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {//判斷對應的hash,屬於位置不變仍是須要變
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;//位置須要變的鏈表 位置爲位置遷移(index+oldCap)
}
}
}
}
}
return newTab; //返回新的數組
}
複製代碼
鏈表是紅黑樹擴容bash
final void split(HashMap<K,V> map, Node<K,V>[] tab, int index, int bit) {
TreeNode<K,V> b = this;
// Relink into lo and hi lists, preserving order
//不須要改變位置的紅黑樹
TreeNode<K,V> loHead = null, loTail = null;
//須要改變位置的紅黑樹
TreeNode<K,V> hiHead = null, hiTail = null;
//對應紅黑樹大小
int lc = 0, hc = 0;
for (TreeNode<K,V> e = b, next; e != null; e = next) {//遍歷鏈表進行分類
next = (TreeNode<K,V>)e.next;
e.next = null;
if ((e.hash & bit) == 0) {
if ((e.prev = loTail) == null)
loHead = e;
else
loTail.next = e;
loTail = e;
++lc;
}
else {
if ((e.prev = hiTail) == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
++hc;
}
}
if (loHead != null) {//不須要改變位置的紅黑樹不爲null時
if (lc <= UNTREEIFY_THRESHOLD)//若是大小 小於等於6時,轉換成普通的鏈表
tab[index] = loHead.untreeify(map);
else {
tab[index] = loHead;
if (hiHead != null) // (else is already treeified)
//若是沒有須要改變位置的,說明原先的紅黑樹數據結構沒有改變,不然說明數據結構改變了,須要從新轉換成對應的紅黑樹
loHead.treeify(tab);
}
}
if (hiHead != null) {
if (hc <= UNTREEIFY_THRESHOLD)
tab[index + bit] = hiHead.untreeify(map);
else {
tab[index + bit] = hiHead;
if (loHead != null)
//若是原先位置的沒有數據,說明原先的紅黑樹數據結構沒有改變,所有都須要移到新的位置,不然說明數據結構改變了,須要從新轉換成對應的紅黑樹
hiHead.treeify(tab);
}
}
}
複製代碼
總結:數據結構