JDK8?HashMap擴(kuò)容算法demo
HashMap初始化方法
HashMap() 不帶參數(shù),默認(rèn)初始化大小為16,加載因子為0.75;
HashMap(int initialCapacity) 指定初始化大??;
HashMap(int initialCapacity, float loadFactor) 指定初始化大小和加載因子大??;
HashMap(Map extends K,? extends V> m) 用現(xiàn)有的一個(gè)map來構(gòu)造HashMap。
JDK8 HashMap擴(kuò)容算法
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
//擴(kuò)容之前數(shù)組不為空
if (oldCap > 0) {
//容量超過最大值
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
//double之后的新容量小于2的30次方且old容量大于等于16
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
// 新的threshold double
newThr = oldThr << 1;
}
//如果old thereshold 大于 0
else if (oldThr > 0)
//舊的threshold 賦給 新的容量
newCap = oldThr;
else {//進(jìn)行初始化容量和閾值賦值
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {//如果old容量為0,
//新的閾值計(jì)算公式
float ft = (float)newCap * loadFactor;
//賦值
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
//新的閾值復(fù)制到當(dāng)前hashMap實(shí)例中
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
if (oldTab != null) {
//遍歷old數(shù)組
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;
/**
設(shè)計(jì)很巧妙,整個(gè)擴(kuò)容之后的數(shù)組分為兩部分:
低位 高位
newCap = oldCap+擴(kuò)容之后的Cap
**/
//判斷hash命中低位分布或者高位部分
if ((e.hash & oldCap) == 0) {//命中低位部分
if (loTail == null)
//第一次遍歷對(duì)低位部分頭節(jié)點(diǎn)進(jìn)行賦值
loHead = e;
else
//當(dāng)前節(jié)點(diǎn)放在鏈表最后
loTail.next = e;
//尾節(jié)點(diǎn)指針后移
loTail = e;
}
//命中高位部分
else {
if (hiTail == null)
//第一次遍歷對(duì)低位部分頭節(jié)點(diǎn)進(jìn)行賦值
hiHead = e;
else
//當(dāng)前節(jié)點(diǎn)放在鏈表最后
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
//將低位部分鏈表頭節(jié)點(diǎn)賦值在newTab[j]
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
//將高位部分鏈表頭節(jié)點(diǎn)賦值在newTab[j+oldCap]
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}以上就是JDK8 HashMap擴(kuò)容算法demo的詳細(xì)內(nèi)容,更多關(guān)于JDK8 HashMap擴(kuò)容算法的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
spring boot使用RabbitMQ實(shí)現(xiàn)topic 主題
本篇文章主要介紹了spring boot使用RabbitMQ實(shí)現(xiàn)topic 主題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
@Value設(shè)置默認(rèn)值后,獲取不到配置值的原因分析
這篇文章主要介紹了@Value設(shè)置默認(rèn)值后,獲取不到配置值的原因,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Spring Boot 2.0多數(shù)據(jù)源配置方法實(shí)例詳解
這篇文章主要介紹了Spring Boot 2.0多數(shù)據(jù)源配置方法實(shí)例詳解,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
解析Java?中for循環(huán)和foreach循環(huán)哪個(gè)更快
這篇文章主要介紹了Java中for循環(huán)和foreach循環(huán)哪個(gè)更快示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

