關(guān)于Java HashMap自動(dòng)排序的簡(jiǎn)單剖析
1.HashMap概述
HashMap是無(wú)序的,這里無(wú)序的意思是你取出數(shù)據(jù)的順序與你存入數(shù)據(jù)的順序不同
2.發(fā)現(xiàn)問(wèn)題
當(dāng)嘗試向HashMap中存入int類型的key,可以看到在輸出的時(shí)候會(huì)自動(dòng)排序
HashMap<Integer, String> map = new HashMap<>(); map.put(3, 'asdf'); map.put(2, 'asdf'); map.put(1, 'asdf'); map.put(6, 'asdf'); map.put(5, 'asdf'); map.put(4, 'asdf'); map.put(8, 'asdf'); map.put(9, 'asdf'); map.put(7, 'asdf'); map.put(0, 'asdf');
輸出

3.實(shí)現(xiàn)原理
我們都知道,HashMap是數(shù)組加鏈表實(shí)現(xiàn)的,在鏈表長(zhǎng)度大于8的時(shí)候?qū)㈡湵磙D(zhuǎn)化為紅黑樹(shù)數(shù)組加鏈表畫(huà)一下模型圖是這樣的,黑色的是數(shù)組,橙色的是鏈表,遍歷HashMap的key的時(shí)候,先遍歷第一列,然后第二列。。。

4.翻看源碼
HashMap的默認(rèn)數(shù)組長(zhǎng)度為16,默認(rèn)負(fù)載因子是0.75,意思就是當(dāng)數(shù)組內(nèi)不為null的元素大于(數(shù)組長(zhǎng)度*負(fù)載因子)的時(shí)候就會(huì)拓容數(shù)組
如果數(shù)組長(zhǎng)度和負(fù)載因子都是默認(rèn)值,那當(dāng)在數(shù)組中存入第13個(gè)元素后就會(huì)拓容16*0.75=12
/** * The default initial capacity - MUST be a power of two. */ static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 /** * The load factor used when none specified in constructor. */ static final float DEFAULT_LOAD_FACTOR = 0.75f;
再讀一下put方法和hash方法
public V put(K key, V value) { return putVal(hash(key), key, value, false, true); } //獲取key的hash,當(dāng)key為int類型的時(shí)候hash=key的值 static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); } final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; //tab就是HashMap的數(shù)組,這句話就是初始化數(shù)組 if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; //如果根據(jù)hash值來(lái)判斷將此元素放在什么位置,如果數(shù)組當(dāng)前位置 //為空直接存放,成為一個(gè)長(zhǎng)度為一的鏈表 if ((p = tab[i = (n - 1) & hash]) == null)//================== tab[i] = newNode(hash, key, value, null); //如果不是,則將當(dāng)前元素放在當(dāng)前位置下元素的后邊形成鏈表 else { Node<K,V> e; K k; if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; else if (p instanceof TreeNode) e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); else { for (int binCount = 0; ; ++binCount) { if ((e = p.next) == null) { p.next = newNode(hash, key, value, null); if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st treeifyBin(tab, hash); break; } 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; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; } } ++modCount; if (++size > threshold) //拓容數(shù)組 resize(); afterNodeInsertion(evict); return null; }
5.分析
17行:if ((p = tab[i = (n - 1) & hash]) == null)
(n - 1) & hash
n-1就是數(shù)組的length-1,現(xiàn)在數(shù)組長(zhǎng)度是16,
15&hash,
例如,現(xiàn)在存入key為1,
15轉(zhuǎn)成二進(jìn)制為1111
1 轉(zhuǎn)成二進(jìn)制為0001,
所以i=15&1=1;
現(xiàn)在1就存放在數(shù)組下標(biāo)為1的位置
如果放2,那就放在數(shù)組下標(biāo)為2的位置,
如果再存放17的話,
1501111
1710001
15&17=1;因?yàn)閿?shù)組下標(biāo)1的位置有上一次存放的key為1的元素,所以就將key=17的元素掛在key=1的下邊,
這是遍歷HashMap的key就會(huì)變成1,17,2
順序就會(huì)亂掉,現(xiàn)在數(shù)組的長(zhǎng)度是16,已使用的是2,還沒(méi)有達(dá)到拓容那一步,
6.驗(yàn)證
下邊的代碼是存放11個(gè)數(shù)據(jù),拓容要存入第13個(gè)數(shù)據(jù)時(shí)進(jìn)行拓容
HashMap<Integer, String> map = new HashMap<>(); map.put(3, 'asdf'); map.put(2, 'asdf'); map.put(1, 'asdf'); map.put(6, 'asdf'); map.put(5, 'asdf'); map.put(4, 'asdf'); map.put(8, 'asdf'); map.put(9, 'asdf'); map.put(7, 'asdf'); map.put(0, 'asdf'); map.put(17,'saf');// map.put(10,'saf');// map.put(11,'saf'); for (int i : map.keySet()) { System.out.println('key=' + i); } System.out.println('map.size()===============' + map.size());
下邊這段代碼的輸出結(jié)果就是

和分析的一樣,
如果再添加兩個(gè)數(shù)據(jù),使其拓容
HashMap<Integer, String> map = new HashMap<>(); map.put(3, 'asdf'); map.put(2, 'asdf'); map.put(1, 'asdf'); map.put(6, 'asdf'); map.put(5, 'asdf'); map.put(4, 'asdf'); map.put(8, 'asdf'); map.put(9, 'asdf'); map.put(7, 'asdf'); map.put(0, 'asdf'); map.put(17,'saf');// map.put(10,'saf');// map.put(11,'saf'); for (int i : map.keySet()) { System.out.println('key=' + i); } System.out.println('map.size()===============' + map.size());
輸出是

又排好了順序
7.結(jié)論
當(dāng)所有key的hash的最大值<數(shù)組的長(zhǎng)度-1時(shí)HashMap可以將存入的元素按照key的hash從小到大排序不過(guò)這個(gè)發(fā)現(xiàn)沒(méi)有什么用就是了,不過(guò)看了一天源碼收獲不少,還看到好幾種沒(méi)見(jiàn)過(guò)的寫法
到此這篇關(guān)于關(guān)于Java HashMap自動(dòng)排序簡(jiǎn)單剖析的文章就介紹到這了,更多相關(guān)Java HashMap自動(dòng)排序內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Android table布局開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器2. jQuery 實(shí)現(xiàn)DOM元素拖拽交換位置的實(shí)例代碼3. 理解PHP5中static和const關(guān)鍵字4. php模擬實(shí)現(xiàn)斗地主發(fā)牌5. IntelliJ IDEA安裝插件的方法步驟6. spring acegi security 1.0.0 發(fā)布7. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)8. Python random庫(kù)使用方法及異常處理方案9. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備