书读的少你别骗我。。。hashmap源码
int
capacity =
1
;
//
初始容量
14 while (capacity < initialCapacity) //确保容量为2的n次幂,使capacity为大于initialCapacity的最小的2的n次幂 15 capacity <<= 1; 16 17 this.loadFactor = loadFactor; 18 threshold = (int)(capacity * loadFactor); 19 table = new Entry[capacity];
-
if
(numKeysToBeAdded > threshold) {
-
// 重新计算新的容量先resize
-
int
targetCapacity = (
int
)(numKeysToBeAdded / loadFactor +
1
);
-
if
(targetCapacity > MAXIMUM_CAPACITY)
-
targetCapacity = MAXIMUM_CAPACITY;
-
int
newCapacity = table.length;
-
while
(newCapacity < targetCapacity)
-
newCapacity <<= 1
;
-
if
(newCapacity > table.length)
-
resize(newCapacity);
扩容为两倍,部位2N次补齐再两倍