關(guān)于HashMap的put方法執(zhí)行全過程
HashMap的put方法執(zhí)行過程
Map的數(shù)據(jù)結(jié)構(gòu)
一個table中放著一個一個的node<>數(shù)組 數(shù)組+鏈表(單向雙向)+紅黑樹
首先執(zhí)行的時候?qū)嶋H上執(zhí)行的是 hashmap中的putVal()方法,putVal()有五個參數(shù)
三個最重要的參數(shù)分別是key的hash值,key,val,key的hash值計算方法為:
static final int hash(Object key) { int h; //擾動加移址 減少hash沖突 return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);}
是hash值的高16位 與低16為進行異或運算得到的值,這樣做是為了讓put的值均勻的存在map中。
在putVal()的源碼是這樣的:
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { //定義一個新的數(shù)組tab 下方有很多需要操作的地方,如果直接使用table的話每次都去對堆中取,比較浪費 ,tab存在線程棧中的,提高性能 Node<K,V>[] tab; Node<K,V> p; int n, i; //先賦值在判斷 //判斷map中的table是否存在,不存在則初始化一個新的,resize()方法作用是擴容和初始化 if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; //計算下標,不存在元素 //n為長度 因為長度為2的n次, n-1之后均為0000 1111 與上hash值計算出下標 //hash值為32位,之前將高16位和低16位異或之后,生成的新的hash值只和低位有關(guān)系,而 //如果a、b兩個值不相同,則異或結(jié)果為1。如果a、b兩個值相同,異或結(jié)果為0。 //只要生成的hash是散列的,那么就均勻分布 減少hash沖突 if ((p = tab[i = (n - 1) & hash]) == null) tab[i] = newNode(hash, key, value, null); else { //存在元素 Node<K,V> e; K k; //判斷是否是同一個key,如果相同,則將新的val賦值給她,返回舊的值 if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; //如果不是 判斷是否為treeNode else if (p instanceof TreeNode) //是的話插入紅黑樹 e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); else { //不是的話便利整個鏈表,找到尾部插入數(shù)據(jù),尾插法 for (int binCount = 0; ; ++binCount) { if ((e = p.next) == null) { p.next = newNode(hash, key, value, null); //鏈表上有9個節(jié)點時進行紅黑樹轉(zhuǎn)換 之前有八個不會變 //轉(zhuǎn)換成紅黑樹是為了提升查詢性能, if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st //如果數(shù)組長度小于64的話會進行擴容,而不是轉(zhuǎn)換成紅黑樹 treeifyBin(tab, hash); break; } //判斷是否有相同的key,相同則替換value if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } //如果存在的話 把舊的值返回 if (e != null) { // existing mapping for key V oldValue = e.value; //只有不存在并且舊的值不為null時 if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; } } //修改次數(shù)+1 ++modCount; //判斷打下哦是否超越閾值,是否需要擴容,需要的話進行擴容 if (++size > threshold) resize(); afterNodeInsertion(evict); return null; }
擴容源碼
final Node<K,V>[] resize() { Node<K,V>[] oldTab = table; int oldCap = (oldTab == null) ? 0 : oldTab.length; int oldThr = threshold; int newCap, newThr = 0; //如果老數(shù)組的容量大于0,即存在 if (oldCap > 0) { //如果超出最大值,按最大值計算 if (oldCap >= MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE; return oldTab; } //如果沒有,擴容之后 *2之后 容量沒有超出最大值并且大于默認容量 else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && oldCap >= DEFAULT_INITIAL_CAPACITY) newThr = oldThr << 1; // double threshold } //規(guī)定了容量的 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; //不用減1 =0 為低位 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; }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Jpa多數(shù)據(jù)源工程配置過程解析
這篇文章主要介紹了Spring Jpa多數(shù)據(jù)源工程配置過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08Retrofit+RxJava實現(xiàn)帶進度條的文件下載
這篇文章主要為大家詳細介紹了Retrofit+RxJava實現(xiàn)帶進度條的文件下載,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06Java定時任務(wù)Timer、TimerTask與ScheduledThreadPoolExecutor詳解
這篇文章主要介紹了Java定時任務(wù)Timer、TimerTask與ScheduledThreadPoolExecutor詳解, 定時任務(wù)就是在指定時間執(zhí)行程序,或周期性執(zhí)行計劃任務(wù),Java中實現(xiàn)定時任務(wù)的方法有很多,本文從從JDK自帶的一些方法來實現(xiàn)定時任務(wù)的需求,需要的朋友可以參考下2024-01-01Java實現(xiàn)RedisUtils操作五大集合(增刪改查)
本文主要介紹了Java實現(xiàn)RedisUtils操作五大集合,文中通過示例代碼介紹的非常詳細,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07SpringBoot定時任務(wù)多線程實現(xiàn)示例
在真實的Java開發(fā)環(huán)境中,我們經(jīng)常會需要用到定時任務(wù)來幫助我們完成一些特殊的任務(wù),本文主要介紹了SpringBoot定時任務(wù)多線程實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12