色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

關(guān)于Java HashMap自動(dòng)排序的簡(jiǎn)單剖析

瀏覽:43日期:2022-08-25 09:28:09

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');

輸出

關(guān)于Java HashMap自動(dòng)排序的簡(jiǎn)單剖析

3.實(shí)現(xiàn)原理

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

關(guān)于Java HashMap自動(dòng)排序的簡(jiǎn)單剖析

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é)果就是

關(guān)于Java HashMap自動(dòng)排序的簡(jiǎn)單剖析

和分析的一樣,

如果再添加兩個(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());

輸出是

關(guān)于Java HashMap自動(dòng)排序的簡(jiǎn)單剖析

又排好了順序

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)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 精品国产一区二区二三区在线观看 | 久艹视频在线 | 国产一国产一有一级毛片 | 国产aⅴ片 | 成人欧美网站免费 | 亚洲视频免费在线看 | 欧美日韩专区国产精品 | 2020久久国产最新免费观看 | 黄色一及毛片 | 国产三级精品久久三级国专区 | 成人毛片视频免费网站观看 | 一级片免费视频 | 热99re久久国超精品首页 | 亚洲成年网站在线观看 | 精品欧美高清一区二区免费 | 日韩黄色在线 | 成人免费大片黄在线观看com | 日本欧美视频 | 国产最爽的乱淫视频国语对 | 国产成人3p视频免费观看 | 日本一区二区三区免费视频 | 日本一级高清片免费 | 成年女人免费看片 | 成年人在线免费观看视频网站 | 91久久精品视频 | 欧美成人性色生活片免费在线观看 | 一区二区三区欧美视频 | 韩国三级大全久久网站 | 加勒比一区二区 | 欧美午夜影院 | 最新怡红院全部视频在线 | 在线国产观看 | 国产四区| 亚洲国产激情一区二区三区 | 久久合| 中文久久| 日本免费毛片在线高清看 | 免费在线观看一区二区 | 97在线免费视频 | 久久久久亚洲香蕉网 | 成人看片在线观看免费 |