final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
//如果是旧对象大小不为0,则判断是否超过最大值,是则返回最大值,否则扩容*2
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
}
//如果旧对象大小为0且旧阈值大于零,说明初始容量已放入阈值
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) {
//旧map不为空,循环迁移数据
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)
//链表长度大于等于8的时候会转换成红黑树
//则在红黑树结构上进行rehash在放到新的地方
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
/**
* 对象链表长度为2-7之间,会进入这里,判断逻辑是
* 因为每次扩容都是原容量*2,即都是initCap*2^n(2的n次幂)
* 假设old=initCap*2^n,则new=initCap*2^(n+1)
* 如果old&hash==0,而(old-1)&hash==index(index也就是j,get/put都可以看到j=(size-1)&hash)
* 证明如下(假设old为16)则:0x10000&hash==0, 0x1111&hash==index --> 0x11111&hash=index
* 0x10000&hash!=0, 0x1111&hash==index --> 0x11111&hash=0x10000 + index = old + index
* 以上,同理初始化大小无论是否是2的等次幂,只要扩容的递增大小都是2的等次幂都适用此证明。
**/
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;
}
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
//如果是旧对象大小不为0,则判断是否超过最大值,是则返回最大值,否则扩容*2
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
}
//如果旧对象大小为0且旧阈值大于零,说明初始容量已放入阈值
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) {
//旧map不为空,循环迁移数据
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)
//链表长度大于等于8的时候会转换成红黑树
//则在红黑树结构上进行rehash在放到新的地方
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
/**
* 对象链表长度为2-7之间,会进入这里,判断逻辑是
* 因为每次扩容都是原容量*2,即都是initCap*2^n(2的n次幂)
* 假设old=initCap*2^n,则new=initCap*2^(n+1)
* 如果old&hash==0,而(old-1)&hash==index(index也就是j,get/put都可以看到j=(size-1)&hash)
* 证明如下(假设old为16)则:0x10000&hash==0, 0x1111&hash==index --> 0x11111&hash=index
* 0x10000&hash!=0, 0x1111&hash==index --> 0x11111&hash=0x10000 + index = old + index
* 以上,同理初始化大小无论是否是2的等次幂,只要扩容的递增大小都是2的等次幂都适用此证明。
**/
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;
}