微信公衆號:I am CR7
若有問題或建議,請在下方留言
最近更新:2018-09-14java
做爲哈希表的Map接口實現,其具有如下幾個特色:數組
1/**
2 * 默認初始大小,值爲16,要求必須爲2的冪
3 */
4static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
5
6/**
7 * 最大容量,必須不大於2^30
8 */
9static final int MAXIMUM_CAPACITY = 1 << 30;
10
11/**
12 * 默認加載因子,值爲0.75
13 */
14static final float DEFAULT_LOAD_FACTOR = 0.75f;
15
16/**
17 * hash衝突默認採用單鏈表存儲,當單鏈表節點個數大於8時,會轉化爲紅黑樹存儲
18 */
19static final int TREEIFY_THRESHOLD = 8;
20
21/**
22 * hash衝突默認採用單鏈表存儲,當單鏈表節點個數大於8時,會轉化爲紅黑樹存儲。
23 * 當紅黑樹中節點少於6時,則轉化爲單鏈表存儲
24 */
25static final int UNTREEIFY_THRESHOLD = 6;
26
27/**
28 * hash衝突默認採用單鏈表存儲,當單鏈表節點個數大於8時,會轉化爲紅黑樹存儲。
29 * 可是有一個前提:要求數組長度大於64,不然不會進行轉化
30 */
31static final int MIN_TREEIFY_CAPACITY = 64;
複製代碼
注意:HashMap默認採用數組+單鏈表方式存儲元素,當元素出現哈希衝突時,會存儲到該位置的單鏈表中。可是單鏈表不會一直增長元素,當元素個數超過8個時,會嘗試將單鏈錶轉化爲紅黑樹存儲。可是在轉化前,會再判斷一次當前數組的長度,只有數組長度大於64才處理。不然,進行擴容操做。此處先提到這,後續會有詳細的講解。安全
問:爲什麼加載因子默認爲0.75?
答:經過源碼裏的javadoc註釋看到,元素在哈希表中分佈的桶頻率服從參數爲0.5的泊松分佈,具體能夠參考下StackOverflow裏的解答:stackoverflow.com/questions/1…微信
1public HashMap() {
2 this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
3}
複製代碼
1public HashMap(int initialCapacity) {
2 this(initialCapacity, DEFAULT_LOAD_FACTOR);
3}
複製代碼
1public HashMap(int initialCapacity, float loadFactor) {
2 if (initialCapacity < 0)
3 throw new IllegalArgumentException("Illegal initial capacity: " +
4 initialCapacity);
5 if (initialCapacity > MAXIMUM_CAPACITY)
6 initialCapacity = MAXIMUM_CAPACITY;
7 if (loadFactor <= 0 || Float.isNaN(loadFactor))
8 throw new IllegalArgumentException("Illegal load factor: " +
9 loadFactor);
10 this.loadFactor = loadFactor;
11 this.threshold = tableSizeFor(initialCapacity)//經過後面擴容的方法知道,該值就是初始建立數組時的長度
12}
13
14//返回大於等於cap最小的2的冪,如cap爲12,結果就是16
15static final int tableSizeFor(int cap) {
16 int n = cap - 1;//爲了保證當cap自己是2的冪的狀況下,可以返回本來的數,不然返回的是cap的2倍
17 n |= n >>> 1;
18 n |= n >>> 2;
19 n |= n >>> 4;
20 n |= n >>> 8;
21 n |= n >>> 16;
22 return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
23}
複製代碼
下面咱們以cap等於8爲例:app
問:爲什麼數組容量必須是2次冪?
答:索引計算公式爲i = (n - 1) & hash,若是n爲2次冪,那麼n-1的低位就全是1,哈希值進行與操做時能夠保證低位的值不變,從而保證分佈均勻,效果等同於hash%n,可是位運算比取餘運算要高效的多。ide
1public HashMap(Map<? extends K, ? extends V> m) {
2 this.loadFactor = DEFAULT_LOAD_FACTOR;
3 putMapEntries(m, false);
4}
5
6final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
7 int s = m.size();
8 if (s > 0) {
9 if (table == null) { // pre-size
10 float ft = ((float)s / loadFactor) + 1.0F;
11 int t = ((ft < (float)MAXIMUM_CAPACITY) ?
12 (int)ft : MAXIMUM_CAPACITY);
13 if (t > threshold)
14 threshold = tableSizeFor(t);
15 }
16 else if (s > threshold)
17 resize();
18 for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
19 K key = e.getKey();
20 V value = e.getValue();
21 putVal(hash(key), key, value, false, evict);
22 }
23 }
24}
複製代碼
1public V put(K key, V value) {
2 return putVal(hash(key), key, value, false, true);
3}
4
5//將key的哈希值,進行高16位和低16位異或操做,增長低16位的隨機性,下降哈希衝突的可能性
6static final int hash(Object key) {
7 int h;
8 return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
9}
10
11final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
12 boolean evict) {
13 Node<K,V>[] tab; Node<K,V> p; int n, i;
14 //首次table爲null,首先經過resize()進行數組初始化
15 if ((tab = table) == null || (n = tab.length) == 0)
16 n = (tab = resize()).length;
17 //利用index=(n-1)&hash的方式,找到索引位置
18 //若是索引位置無元素,則建立Node對象,存入數組該位置中
19 if ((p = tab[i = (n - 1) & hash]) == null)
20 tab[i] = newNode(hash, key, value, null);
21 else { //若是索引位置已有元素,說明hash衝突,存入單鏈表或者紅黑樹中
22 Node<K,V> e; K k;
23 //hash值和key值都同樣,則進行value值的替代
24 if (p.hash == hash &&
25 ((k = p.key) == key || (key != null && key.equals(k))))
26 e = p;
27 else if (p instanceof TreeNode) //hash值一致,key值不一致,且p爲紅黑樹結構,則往紅黑樹中添加
28 e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
29 else { //hash值一致,key值不一致,且p爲單鏈表結構,則往單鏈表中添加
30 for (int binCount = 0; ; ++binCount) {
31 if ((e = p.next) == null) {
32 p.next = newNode(hash, key, value, null); //追加到單鏈表末尾
33 if (binCount >= TREEIFY_THRESHOLD - 1) // //超過樹化閾值則進行樹化操做
34 treeifyBin(tab, hash);
35 break;
36 }
37 if (e.hash == hash &&
38 ((k = e.key) == key || (key != null && key.equals(k))))
39 break;
40 p = e;
41 }
42 }
43 if (e != null) { // existing mapping for key
44 V oldValue = e.value;
45 if (!onlyIfAbsent || oldValue == null)
46 e.value = value;
47 afterNodeAccess(e);
48 return oldValue;
49 }
50 }
51 ++modCount;
52 if (++size > threshold) //當元素個數大於新增閾值,則經過resize()擴容
53 resize();
54 afterNodeInsertion(evict);
55 return null;
56}
複製代碼
問:獲取hash值時:爲什麼在hash方法中加上異或無符號右移16位的操做?
答:此方式是採用"擾亂函數"的解決方案,將key的哈希值,進行高16位和低16位異或操做,增長低16位的隨機性,下降哈希衝突的可能性。函數
下面咱們經過一個例子,來看下有無"擾亂函數"的狀況下,計算出來索引位置的值:
性能
1final Node<K,V>[] resize() {
2 Node<K,V>[] oldTab = table;
3 int oldCap = (oldTab == null) ? 0 : oldTab.length;
4 int oldThr = threshold;
5 int newCap, newThr = 0;
6 if (oldCap > 0) {//數組不爲空
7 if (oldCap >= MAXIMUM_CAPACITY) { //當前長度超過MAXIMUM_CAPACITY,新增閾值爲Integer.MAX_VALUE
8 threshold = Integer.MAX_VALUE;
9 return oldTab;
10 }
11 else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
12 oldCap >= DEFAULT_INITIAL_CAPACITY) //進行2倍擴容,若是當前長度超過初始16,新增閾值也作2倍擴容
13 newThr = oldThr << 1; // double threshold
14 }
15 else if (oldThr > 0) // 數組爲空,指定了新增閾值
16 newCap = oldThr;
17 else { //數組爲空,未指定新增閾值,採用默認初始大小和加載因子,新增閾值爲16*0.75=12
18 newCap = DEFAULT_INITIAL_CAPACITY;
19 newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
20 }
21 if (newThr == 0) { //按照給定的初始大小計算擴容後的新增閾值
22 float ft = (float)newCap * loadFactor;
23 newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
24 (int)ft : Integer.MAX_VALUE);
25 }
26 threshold = newThr; //擴容後的新增閾值
27 @SuppressWarnings({"rawtypes","unchecked"})
28 Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap]; //擴容後的數組
29 table = newTab;
30 if (oldTab != null) { //將原數組中元素放入擴容後的數組中
31 for (int j = 0; j < oldCap; ++j) {
32 Node<K,V> e;
33 if ((e = oldTab[j]) != null) {
34 oldTab[j] = null;
35 if (e.next == null) //無後繼節點,則直接計算在新數組中位置,放入便可
36 newTab[e.hash & (newCap - 1)] = e;
37 else if (e instanceof TreeNode) //爲樹節點須要拆分
38 ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
39 else { //有後繼節點,且爲單鏈表,將原數組中單鏈表元素進行拆分,一部分在原索引位置,一部分在原索引+原數組長度
40 Node<K,V> loHead = null, loTail = null; //保存在原索引的鏈表
41 Node<K,V> hiHead = null, hiTail = null; //保存在新索引的鏈表
42 Node<K,V> next;
43 do {
44 next = e.next;
45 if ((e.hash & oldCap) == 0) { //哈希值和原數組長度進行&操做,爲0則在原數組的索引位置,非0則在原數組索引位置+原數組長度的新位置
46 if (loTail == null)
47 loHead = e;
48 else
49 loTail.next = e;
50 loTail = e;
51 }
52 else {
53 if (hiTail == null)
54 hiHead = e;
55 else
56 hiTail.next = e;
57 hiTail = e;
58 }
59 } while ((e = next) != null);
60 if (loTail != null) {
61 loTail.next = null;
62 newTab[j] = loHead;
63 }
64 if (hiTail != null) {
65 hiTail.next = null;
66 newTab[j + oldCap] = hiHead;
67 }
68 }
69 }
70 }
71 }
72 return newTab;
73}
複製代碼
狀況一:ui
1HashMap<String, Integer> hashMap = new HashMap<>();
複製代碼
1int oldCap = (oldTab == null) ? 0 : oldTab.length;
2int oldThr = threshold;
複製代碼
1newCap = DEFAULT_INITIAL_CAPACITY;
2newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
3threshold = newThr;
4@SuppressWarnings({"rawtypes","unchecked"})
5 Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
6table = newTab;
複製代碼
狀況二:this
1HashMap<String, Integer> hashMap = new HashMap<>(7);
複製代碼
1else if (oldThr > 0) // initial capacity was placed in threshold
2 newCap = oldThr;
複製代碼
1if (newThr == 0) {
2 float ft = (float)newCap * loadFactor;
3 newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
4 (int)ft : Integer.MAX_VALUE);
5}
6threshold = newThr;
7@SuppressWarnings({"rawtypes","unchecked"})
8 Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
9table = newTab;
複製代碼
接着2.2裏的狀況二,繼續添加元素,直到擴容:
1if (oldCap > 0) {
2 if (oldCap >= MAXIMUM_CAPACITY) {
3 threshold = Integer.MAX_VALUE;
4 return oldTab;
5 }
6 else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
7 oldCap >= DEFAULT_INITIAL_CAPACITY)
8 newThr = oldThr << 1; // double threshold
9}
複製代碼
1if (newThr == 0) {
2 float ft = (float)newCap * loadFactor;
3 newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
4 (int)ft : Integer.MAX_VALUE);
5}
6threshold = newThr;
7@SuppressWarnings({"rawtypes","unchecked"})
8 Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
9table = newTab;
10..........省略.......//將原數組元素存入新數組中
複製代碼
繼續添加元素,直到擴容:
1if (oldCap > 0) {
2 if (oldCap >= MAXIMUM_CAPACITY) {
3 threshold = Integer.MAX_VALUE;
4 return oldTab;
5 }
6 else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
7 oldCap >= DEFAULT_INITIAL_CAPACITY)
8 newThr = oldThr << 1; // double threshold
9}
複製代碼
1threshold = newThr;
2@SuppressWarnings({"rawtypes","unchecked"})
3 Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
4table = newTab;
5..........省略.......//將原數組元素存入新數組中
複製代碼
繼續添加元素,擴容到數組長度等於MAXIMUM_CAPACITY:
1if (oldCap > 0) {
2 if (oldCap >= MAXIMUM_CAPACITY) {
3 threshold = Integer.MAX_VALUE;
4 return oldTab;
5 }
6 else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
7 oldCap >= DEFAULT_INITIAL_CAPACITY)
8 newThr = oldThr << 1; // double threshold
9}
複製代碼
將本來的單鏈錶轉化爲雙向鏈表,再遍歷這個雙向鏈表轉化爲紅黑樹:
1final void treeifyBin(Node<K,V>[] tab, int hash) {
2 int n, index; Node<K,V> e;
3 //樹形化還有一個要求就是數組長度必須大於等於64,不然繼續採用擴容策略
4 if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
5 resize();
6 else if ((e = tab[index = (n - 1) & hash]) != null) {
7 TreeNode<K,V> hd = null, tl = null;//hd指向首節點,tl指向尾節點
8 do {
9 TreeNode<K,V> p = replacementTreeNode(e, null);//將鏈表節點轉化爲紅黑樹節點
10 if (tl == null) // 若是尾節點爲空,說明尚未首節點
11 hd = p; // 當前節點做爲首節點
12 else { // 尾節點不爲空,構造一個雙向鏈表結構,將當前節點追加到雙向鏈表的末尾
13 p.prev = tl; // 當前樹節點的前一個節點指向尾節點
14 tl.next = p; // 尾節點的後一個節點指向當前節點
15 }
16 tl = p; // 把當前節點設爲尾節點
17 } while ((e = e.next) != null); // 繼續遍歷單鏈表
18 //將本來的單鏈錶轉化爲一個節點類型爲TreeNode的雙向鏈表
19 if ((tab[index] = hd) != null) // 把轉換後的雙向鏈表,替換數組原來位置上的單向鏈表
20 hd.treeify(tab); // 將當前雙向鏈表樹形化
21 }
22}
複製代碼
將雙向鏈表轉化爲紅黑樹的具體實現:
1final void treeify(Node<K,V>[] tab) {
2 TreeNode<K,V> root = null; // 定義紅黑樹的根節點
3 for (TreeNode<K,V> x = this, next; x != null; x = next) { // 從TreeNode雙向鏈表的頭節點開始逐個遍歷
4 next = (TreeNode<K,V>)x.next; // 頭節點的後繼節點
5 x.left = x.right = null;
6 if (root == null) {
7 x.parent = null;
8 x.red = false;
9 root = x; // 頭節點做爲紅黑樹的根,設置爲黑色
10 }
11 else { // 紅黑樹存在根節點
12 K k = x.key;
13 int h = x.hash;
14 Class<?> kc = null;
15 for (TreeNode<K,V> p = root;;) { // 從根開始遍歷整個紅黑樹
16 int dir, ph;
17 K pk = p.key;
18 if ((ph = p.hash) > h) // 當前紅黑樹節點p的hash值大於雙向鏈表節點x的哈希值
19 dir = -1;
20 else if (ph < h) // 當前紅黑樹節點的hash值小於雙向鏈表節點x的哈希值
21 dir = 1;
22 else if ((kc == null &&
23 (kc = comparableClassFor(k)) == null) ||
24 (dir = compareComparables(kc, k, pk)) == 0) // 當前紅黑樹節點的hash值等於雙向鏈表節點x的哈希值,則若是key值採用比較器一致則比較key值
25 dir = tieBreakOrder(k, pk); //若是key值也一致則比較className和identityHashCode
26
27 TreeNode<K,V> xp = p;
28 if ((p = (dir <= 0) ? p.left : p.right) == null) { // 若是當前紅黑樹節點p是葉子節點,那麼雙向鏈表節點x就找到了插入的位置
29 x.parent = xp;
30 if (dir <= 0) //根據dir的值,插入到p的左孩子或者右孩子
31 xp.left = x;
32 else
33 xp.right = x;
34 root = balanceInsertion(root, x); //紅黑樹中插入元素,須要進行平衡調整(過程和TreeMap調整邏輯如出一轍)
35 break;
36 }
37 }
38 }
39 }
40 //將TreeNode雙向鏈表轉化爲紅黑樹結構以後,因爲紅黑樹是基於根節點進行查找,因此必須將紅黑樹的根節點做爲數組當前位置的元素
41 moveRootToFront(tab, root);
42}
複製代碼
將紅黑樹的根節點移動到數組的索引所在位置上:
1static <K,V> void moveRootToFront(Node<K,V>[] tab, TreeNode<K,V> root) {
2 int n;
3 if (root != null && tab != null && (n = tab.length) > 0) {
4 int index = (n - 1) & root.hash; //找到紅黑樹根節點在數組中的位置
5 TreeNode<K,V> first = (TreeNode<K,V>)tab[index]; //獲取當前數組中該位置的元素
6 if (root != first) { //紅黑樹根節點不是數組當前位置的元素
7 Node<K,V> rn;
8 tab[index] = root;
9 TreeNode<K,V> rp = root.prev;
10 if ((rn = root.next) != null) //將紅黑樹根節點先後節點相連
11 ((TreeNode<K,V>)rn).prev = rp;
12 if (rp != null)
13 rp.next = rn;
14 if (first != null) //將數組當前位置的元素,做爲紅黑樹根節點的後繼節點
15 first.prev = root;
16 root.next = first;
17 root.prev = null;
18 }
19 assert checkInvariants(root);
20 }
21}
複製代碼
1final TreeNode<K,V> putTreeVal(HashMap<K,V> map, Node<K,V>[] tab,
2 int h, K k, V v) {
3 Class<?> kc = null;
4 boolean searched = false;
5 TreeNode<K,V> root = (parent != null) ? root() : this;
6 for (TreeNode<K,V> p = root;;) {
7 int dir, ph; K pk;
8 if ((ph = p.hash) > h)//進行哈希值的比較
9 dir = -1;
10 else if (ph < h)
11 dir = 1;
12 else if ((pk = p.key) == k || (k != null && k.equals(pk)))
13 return p;
14 else if ((kc == null &&
15 (kc = comparableClassFor(k)) == null) ||
16 (dir = compareComparables(kc, k, pk)) == 0) {//hash值相同,則按照key進行比較
17 if (!searched) {
18 TreeNode<K,V> q, ch;
19 searched = true;
20 if (((ch = p.left) != null &&
21 (q = ch.find(h, k, kc)) != null) ||//去左子樹中查找哈希值相同,key相同的節點
22 ((ch = p.right) != null &&
23 (q = ch.find(h, k, kc)) != null))//去右子樹中查找哈希值相同,key相同的節點
24 return q;
25 }
26 dir = tieBreakOrder(k, pk);//經過比較k與pk的hashcode
27 }
28
29 TreeNode<K,V> xp = p;
30 if ((p = (dir <= 0) ? p.left : p.right) == null) {//找到紅黑樹合適的位置插入
31 Node<K,V> xpn = xp.next;
32 TreeNode<K,V> x = map.newTreeNode(h, k, v, xpn);
33 if (dir <= 0) //插入到左節點或者右節點
34 xp.left = x;
35 else
36 xp.right = x;
37 xp.next = x;//插入到雙向鏈表合適的位置
38 x.parent = x.prev = xp;
39 if (xpn != null)
40 ((TreeNode<K,V>)xpn).prev = x;
41 moveRootToFront(tab, balanceInsertion(root, x));//作插入後的平衡調整 將平衡後的紅黑樹節點做爲數組該位置的元素
42 return null;
43 }
44 }
45}
複製代碼
當hash衝突時,單鏈表元素個數超過樹化閾值(TREEIFY_THRESHOLD)後,轉化爲紅黑樹存儲。以後再繼續衝突,則就變成往紅黑樹中插入元素了。關於紅黑樹插入元素,請看我以前寫的文章:TreeMap之元素插入
將紅黑樹按照擴容後的數組,從新計算索引位置,而且拆分後的紅黑樹還須要判斷個數,從而決定是作去樹化操做仍是樹化操做:
1final void split(HashMap<K,V> map, Node<K,V>[] tab, int index, int bit) {
2 TreeNode<K,V> b = this;
3 // Relink into lo and hi lists, preserving order
4 TreeNode<K,V> loHead = null, loTail = null; //保存在原索引的紅黑樹
5 TreeNode<K,V> hiHead = null, hiTail = null; //保存在新索引的紅黑樹
6 int lc = 0, hc = 0;
7 for (TreeNode<K,V> e = b, next; e != null; e = next) {
8 next = (TreeNode<K,V>)e.next;
9 e.next = null;
10 if ((e.hash & bit) == 0) { //哈希值和原數組長度進行&操做,爲0則在原數組的索引位置,非0則在原數組索引位置+原數組長度的新位置
11 if ((e.prev = loTail) == null)
12 loHead = e;
13 else
14 loTail.next = e;
15 loTail = e;
16 ++lc;
17 }
18 else {
19 if ((e.prev = hiTail) == null)
20 hiHead = e;
21 else
22 hiTail.next = e;
23 hiTail = e;
24 ++hc;
25 }
26 }
27
28 if (loHead != null) {
29 if (lc <= UNTREEIFY_THRESHOLD) //當紅黑樹的節點不大於去樹化閾值,則將原索引處的紅黑樹進行去樹化操做
30 tab[index] = loHead.untreeify(map); //紅黑樹根節點做爲原索引處的元素
31 else { //當紅黑樹的節點大於去樹化閾值,則將原索引處的紅黑樹進行樹化操做
32 tab[index] = loHead;
33 if (hiHead != null) // (else is already treeified)
34 loHead.treeify(tab);
35 }
36 }
37 if (hiHead != null) {
38 if (hc <= UNTREEIFY_THRESHOLD) //當紅黑樹的節點不大於去樹化閾值,則將新索引處的紅黑樹進行去樹化操做
39 tab[index + bit] = hiHead.untreeify(map); //紅黑樹根節點做爲新索引處的元素
40 else { //當紅黑樹的節點大於去樹化閾值,則將新索引處的紅黑樹進行樹化操做
41 tab[index + bit] = hiHead;
42 if (loHead != null)
43 hiHead.treeify(tab);
44 }
45 }
46}
複製代碼
遍歷紅黑樹,還原成單鏈表結構:
1final Node<K,V> untreeify(HashMap<K,V> map) {
2 Node<K,V> hd = null, tl = null;
3 for (Node<K,V> q = this; q != null; q = q.next) { //遍歷紅黑樹,依次將TreeNode轉化爲Node,還原成單鏈表形式
4 Node<K,V> p = map.replacementNode(q, null);
5 if (tl == null)
6 hd = p;
7 else
8 tl.next = p;
9 tl = p;
10 }
11 return hd;
12}
複製代碼
1//插入38個元素,無hash衝突,依次存入索引0~37的位置
2HashMap<Integer, Integer> hashMap = new HashMap<>(64);
3for(int i=0; i<38; i++){
4 hashMap.put(i, i);
5}
6//依次插入6四、12八、18二、25六、320、384,448,索引位置爲0,出現hash衝突,往單鏈表中插入
7for (int i=1; i <= 7; i++) {
8 hashMap.put(64*i, 64*i);
9}
10//插入512,hash衝突,往單鏈表中插入。此時單鏈表個數大於TREEIFY_THRESHOLD,將單鏈錶轉化爲紅黑樹
11hashMap.put(64*8, 64*8);
12//插入576,hash衝突,往紅黑樹中插入
13hashMap.put(64*9, 64*9);
14//hash不衝突,保存到數組索引爲38的位置,此時總元素個數爲48,新增閾值爲48,不作處理。
15hashMap.put(38, 38);
16//hash不衝突,保存到數組索引爲39的位置,此時總元素個數爲49,新增閾值爲48,擴容!!!
17hashMap.put(39, 39);
複製代碼