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ò)容算法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java分批將List數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫的解決過程
這篇文章主要給大家介紹了關(guān)于Java分批將List數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫的解決過程,文中通過代碼示例介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-08-08
Springboot使用test無法啟動(dòng)問題的解決
這篇文章主要介紹了Springboot使用test無法啟動(dòng)問題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringBoot利用切面注解及反射實(shí)現(xiàn)事件監(jiān)聽功能
這篇文章主要介紹了springboot事件監(jiān)聽,通過利用切面、注解、反射實(shí)現(xiàn),接下來將對(duì)這幾種方式逐一說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2022-07-07
IDEA:Git stash 暫存分支修改的實(shí)現(xiàn)代碼
這篇文章主要介紹了IDEA:Git stash 暫存分支修改的實(shí)現(xiàn)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
java實(shí)現(xiàn)上傳文件類型檢測(cè)過程解析
這篇文章主要介紹了java實(shí)現(xiàn)上傳文件類型檢測(cè)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
springboot中事務(wù)管理@Transactional的注意事項(xiàng)與使用場(chǎng)景
今天小編就為大家分享一篇關(guān)于springboot中事務(wù)管理@Transactional的注意事項(xiàng)與使用場(chǎng)景,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04
使用迭代器模式來進(jìn)行Java的設(shè)計(jì)模式編程
這篇文章主要介紹了使用迭代器模式來進(jìn)行Java的設(shè)計(jì)模式編程,文中對(duì)迭代器模式中的容器封裝方面的知識(shí)進(jìn)行了講解,需要的朋友可以參考下2016-02-02

