hashmap的resize函数,用于对hashmap初始化或者扩容。
首先看一下该函数的注释,如下图。从注释中可以看到,该函数的作用是初始化或者使table的size翻倍。如果table是null,那么就申请空间进行初始化。否则,因为我们在使用2的指数的扩张,在原来table的每个位置的元素,在新的table中,他们要么待在原来的位置,要么移动2的指数的偏移。从这里可以看出,扩容前table每个位置上如果有多个元素,元素之间组成链表时,在扩容后,该链表中的元素,有一部分会待在原地,剩下的元素会往后移动2的指数的偏移。
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数组,用作的新的hashmap的存储空间。
之后的过程是,对原来hashmap中的每一个位置进行遍历,把该位置上的各个元素重新放置到新的table中。所以在循环的过程中,会定义lohead,lotail,hihead,hitail,分别表示留着原地的链表的头和尾,移动到更高位置的链表的头和尾。这里需要注意一点,在jdk1.8中,扩容后链表中元素的顺序和扩容前链表中元素的位置,是相同的,并不会像jdk1.7那样会发生逆序。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/li_canhui/article/details/85681699
查看更多关于Java源码解析HashMap的resize函数的详细内容...