hashmap的resize函數(shù),用于對hashmap初始化或者擴容。
首先看一下該函數(shù)的注釋,如下圖。從注釋中可以看到,該函數(shù)的作用是初始化或者使table的size翻倍。如果table是null,那么就申請空間進行初始化。否則,因為我們在使用2的指數(shù)的擴張,在原來table的每個位置的元素,在新的table中,他們要么待在原來的位置,要么移動2的指數(shù)的偏移。從這里可以看出,擴容前table每個位置上如果有多個元素,元素之間組成鏈表時,在擴容后,該鏈表中的元素,有一部分會待在原地,剩下的元素會往后移動2的指數(shù)的偏移。
1
2
3
4
5
6
7
8
|
/** * initializes or doubles table size. if null, allocates in * accord with initial capacity target held in field threshold. * otherwise, because we are using power-of-two expansion, the * elements from each bin must either stay at same index, or move * with a power of two offset in the new table. * @return the table **/ |
接下來看一下resize的代碼,如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
final node<k,v>[] resize() { node<k,v>[] oldtab = table; int oldcap = (oldtab == null ) ? 0 : oldtab.length; int oldthr = threshold; int newcap, newthr = 0 ; if (oldcap > 0 ) { if (oldcap >= maximum_capacity) { threshold = integer.max_value; return oldtab; } 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 ) { 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; } |
擴容的過程分為兩部分,第一部分是對threshold和table的初始化或者重新計算,第二部分是對hashmap中的元素進行重新放置。初始化的過程比較簡單,基本就是使用默認值,初始化hashmap的各個成員變量。重新計算時,是會申請一個2倍大小的node數(shù)組,用作的新的hashmap的存儲空間。
之后的過程是,對原來hashmap中的每一個位置進行遍歷,把該位置上的各個元素重新放置到新的table中。所以在循環(huán)的過程中,會定義lohead,lotail,hihead,hitail,分別表示留著原地的鏈表的頭和尾,移動到更高位置的鏈表的頭和尾。這里需要注意一點,在jdk1.8中,擴容后鏈表中元素的順序和擴容前鏈表中元素的位置,是相同的,并不會像jdk1.7那樣會發(fā)生逆序。
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內(nèi)容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/li_canhui/article/details/85681699