关于 jdk 8 HashMap::resize方法的简单理解
/**
* 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
*/
// 初始化或加倍表大小。 如果为空,则根据字段阈值中持有的初始容量目标进行分配。
// 否则,因为我们使用的是 2 的幂扩展,所以每个 bin 中的元素必须保持相同的索引,或者在新表中以 2 的幂的偏移量移动。
final Node<K,V>[] resize() {
// transient Node<K,V>[] table;
Node<K,V>[] oldTab = table; //初始的table
int oldCap = (oldTab == null) ? 0 : oldTab.length;
// int threshold;
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) { //如果oldCap大于0,意味着table不是空的
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && //newCap = oldCap*2 小于最大容量
oldCap >= DEFAULT_INITIAL_CAPACITY) //且 大于初始默认容量
newThr = oldThr << 1; // double threshold 相当于 乘2
}
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); //newTht = 阈值。即负载因子*默认初始容量 (0.75* 16 = 12),
}
if (newThr == 0) { //这一步说明oldThr > 0,oldCap <= 0 。说明原有的table = 0的,但是oldThr(阈值)是不等于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]; //创建一个Node数组
table = newTab; //新的table
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) //只有一个值,没有发生hash碰撞
// p = tab[i = (n - 1) & hash]是put放入时的方式, n = tab.length。
// 假设newCap = 32. newCap-1 = 00011111
newTab[e.hash & (newCap - 1)] = e; //(newCap-1)是低位,00000000 00101001(可能是这么多),最后的值一定是小于 newCap - 1的。
// 如果put()放入值的 是 e.hash & (newCap - 1) = e.hash % newCap
else if (e instanceof TreeNode) //至少有8个值(至少发生过8次hash碰撞)且已经以ThreeNode(树)的形式存储了
((TreeNode<K,V>)e).split(this, newTab, j, oldCap); //这里this是HashMap本身
else { // preserve order 保持秩序 大于2小于8的个数,是一个链表
Node<K,V> loHead = null, loTail = null; //低位
Node<K,V> hiHead = null, hiTail = null; //高位
Node<K,V> next;
do {
next = e.next;
// 此时的oldCap并没有*2,newCap是*2的
// 这里有点想不通?这样一个方式在这里是为什么? 如果oldeCap 是16= 00010000,此时说明储存的格子数没有超过12,而存储了的格子是任意的,是无序的排列。
// p = tab[i = (n - 1) & hash]是put放入时的方式, n = tab.length。并且这个方法是在本方法之后的,发现他们相差了1,即放入的方式不一样
// 什么情况 e.hash & oldCap 会等于0。 依然假设oldCap = 16 = 00010000。 第5位不为1都会是0
// 测了一下同一个hash相与不同的数(16,32,64,128)相差比较大,不同的数,可能是高位也可能是低位。随机属性比较大
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;
}