JDK8 HashMap擴(kuò)容機(jī)制分析詳解
HashMap
HashMap 主要用來(lái)存放鍵值對(duì),它基于哈希表的 Map 接口實(shí)現(xiàn),是常用的 Java 集合之一,是非線程安全的??梢源鎯?chǔ)null值,但是只有一個(gè)key可以為null,有多個(gè)值可以為null。
JDK1.8 以后的 HashMap 在解決哈希沖突時(shí)有了較大的變化,當(dāng)鏈表長(zhǎng)度大于閾值(默認(rèn)為 8)(將鏈表轉(zhuǎn)換成紅黑樹(shù)前會(huì)判斷,如果當(dāng)前數(shù)組的長(zhǎng)度小于 64,那么會(huì)選擇先進(jìn)行數(shù)組擴(kuò)容,而不是轉(zhuǎn)換為紅黑樹(shù))時(shí),將鏈表轉(zhuǎn)化為紅黑樹(shù),以減少搜索時(shí)間。
HashMap 默認(rèn)的初始化大小為 16。之后每次擴(kuò)充,容量變?yōu)樵瓉?lái)的 2 倍。并且, HashMap 總是使用 2 的冪作為哈希表的大小。
put方法
HashMap只提供了put來(lái)添加元素,put內(nèi)部調(diào)用putVal方法
對(duì)putVal 的分析如下
如果定位到的數(shù)組沒(méi)有元素,就直接插入;
如果定位到的數(shù)組有元素,比較key,如果key相同就直接覆蓋,如果相同,判斷p是否是一個(gè)樹(shù)節(jié)點(diǎn)
- 如果是,調(diào)用樹(shù)節(jié)點(diǎn)的插入方法將元素插入;
- 如果不是,遍歷鏈表,將元素插入到鏈表尾部;
說(shuō)明:直接覆蓋之后會(huì)return,不會(huì)有后續(xù)操作;鏈表長(zhǎng)度大于閾值8并且數(shù)組長(zhǎng)度大于64的時(shí)候才會(huì)轉(zhuǎn)換紅黑樹(shù),否則只是擴(kuò)容數(shù)組。
put方法示例
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; //table未初始化或者長(zhǎng)度為0,進(jìn)行擴(kuò)容 if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; //(n - 1) & hash 確定元素存放在哪個(gè)桶中,桶為空, //新生成結(jié)點(diǎn)放入桶中(此時(shí),這個(gè)結(jié)點(diǎn)是放在數(shù)組中) if ((p = tab[i = (n - 1) & hash]) == null) tab[i] = newNode(hash, key, value, null); //桶中已經(jīng)存在元素 else { Node<K,V> e; K k; //key相等,hash相等 if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) //將元素賦值給e e = p; //hash值不相等,即key不相等,并且該節(jié)點(diǎn)是紅黑樹(shù)節(jié)點(diǎn) else if (p instanceof TreeNode) //放入樹(shù)中 e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); //鏈表節(jié)點(diǎn) else { //在鏈表末尾插入節(jié)點(diǎn) for (int binCount = 0; ; ++binCount) { if ((e = p.next) == null) { p.next = newNode(hash, key, value, null); //節(jié)點(diǎn)數(shù)量達(dá)到閾值8,執(zhí)行treeifyBin方法 //此方法會(huì)根據(jù)HashMap的數(shù)組來(lái)決定是否要轉(zhuǎn)換為紅黑樹(shù) //數(shù)組長(zhǎng)度大于等于64才會(huì)轉(zhuǎn)換為紅黑樹(shù),否則只會(huì)擴(kuò)容數(shù)組 if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st treeifyBin(tab, hash); //跳出循環(huán) break; } if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } //桶中有key值和hash值于插入節(jié)點(diǎn)相等的節(jié)點(diǎn) if (e != null) { // existing mapping for key V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); //覆蓋以后返回舊值 return oldValue; } } ++modCount; //插入完成后實(shí)際大小大于閾值,需要擴(kuò)容 if (++size > threshold) resize(); afterNodeInsertion(evict); return null; }
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; //容量超過(guò)最大值就不再擴(kuò)容 if (oldCap > 0) { if (oldCap >= MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE; return oldTab; } //沒(méi)超過(guò)最大值擴(kuò)容到原來(lái)的2倍 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) { //擴(kuò)容完成后,重新進(jìn)行hash分配,寫(xiě)入數(shù)據(jù) 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; }
以上就是JDK8 HashMap擴(kuò)容機(jī)制分析詳解的詳細(xì)內(nèi)容,更多關(guān)于JDK8 HashMap擴(kuò)容機(jī)制的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java面向?qū)ο?API(接口)與集合(ArrayList)
這篇文章主要介紹了Java語(yǔ)言面向?qū)ο蟮腁PI與集合,還是十分不錯(cuò)的,這里給大家分享下,需要的朋友可以參考,希望能夠給你帶來(lái)幫助2021-08-08Spring中InitializingBean的使用詳細(xì)解析
這篇文章主要介紹了Spring中InitializingBean的使用詳細(xì)解析,InitializingBean是Spring提供的拓展性接口,提供了屬性初始化后的處理方法,它只有一個(gè)afterPropertiesSet方法,凡是繼承該接口的類(lèi),在bean的屬性初始化后都會(huì)執(zhí)行該方法,需要的朋友可以參考下2024-02-02Spark Streaming算子開(kāi)發(fā)實(shí)例
這篇文章主要介紹了Spark Streaming算子開(kāi)發(fā)實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06SpringBoot中@ConditionalOnBean實(shí)現(xiàn)原理解讀
這篇文章主要介紹了SpringBoot中@ConditionalOnBean實(shí)現(xiàn)原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02JAVA實(shí)現(xiàn)生成順序ID,不浪費(fèi)ID
這篇文章主要介紹了JAVA實(shí)現(xiàn)生成順序ID,不浪費(fèi)ID問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04