Java源碼解析HashMap的resize函數(shù)
HashMap的resize函數(shù),用于對HashMap初始化或者擴(kuò)容。
首先看一下該函數(shù)的注釋,如下圖。從注釋中可以看到,該函數(shù)的作用是初始化或者使table的size翻倍。如果table是null,那么就申請空間進(jìn)行初始化。否則,因為我們在使用2的指數(shù)的擴(kuò)張,在原來table的每個位置的元素,在新的table中,他們要么待在原來的位置,要么移動2的指數(shù)的偏移。從這里可以看出,擴(kuò)容前table每個位置上如果有多個元素,元素之間組成鏈表時,在擴(kuò)容后,該鏈表中的元素,有一部分會待在原地,剩下的元素會往后移動2的指數(shù)的偏移。
/** * Initializes or doubles table size. If null, allocates in * accord with initial capacity target held in field threshold. * Otherwise, because we are using power-of-two expansion, the * elements from each bin must either stay at same index, or move * with a power of two offset in the new table. * @return the table **/
接下來看一下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) {
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
}
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) {
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;
}
擴(kuò)容的過程分為兩部分,第一部分是對threshold和table的初始化或者重新計算,第二部分是對HashMap中的元素進(jìn)行重新放置。初始化的過程比較簡單,基本就是使用默認(rèn)值,初始化HashMap的各個成員變量。重新計算時,是會申請一個2倍大小的Node數(shù)組,用作的新的HashMap的存儲空間。
之后的過程是,對原來HashMap中的每一個位置進(jìn)行遍歷,把該位置上的各個元素重新放置到新的table中。所以在循環(huán)的過程中,會定義loHead,loTail,hiHead,hiTail,分別表示留著原地的鏈表的頭和尾,移動到更高位置的鏈表的頭和尾。這里需要注意一點,在jdk1.8中,擴(kuò)容后鏈表中元素的順序和擴(kuò)容前鏈表中元素的位置,是相同的,并不會像jdk1.7那樣會發(fā)生逆序。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
Java實現(xiàn)SMS短信通發(fā)送手機(jī)驗證碼案例講解
這篇文章主要介紹了Java實現(xiàn)SMS短信通發(fā)送手機(jī)驗證碼案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Spring Boot利用@Async如何實現(xiàn)異步調(diào)用:自定義線程池
這篇文章主要給大家介紹了關(guān)于Spring Boot利用@Async如何實現(xiàn)異步調(diào)用:自定義線程池的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2018-05-05

