该方法的第一道流程:
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;
}
}
}
对该流程的认识:
假定的情况是:
当下hashMap对象中的存在 键值对 且
当下hashMap对象中的键值对数大于
HashMap的最大容量MAXIMUM_CAPACITY
在这种情况下做了什么事情:
1. 直接将当下hashMap对象的临界值(threshold)设置为
Integer.MAX_VALUE
2. 并 返回当下hashMap对象中成员变量 Node<K,V>[] table
该方法的第二道流程:
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) {
}else if ((newCap = oldCap << 1) <
MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY){
newThr = oldThr << 1;
}
}
if (newThr == 0) {
因为newThr的0值被改变了,所以不执行这里了
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
下面的代码就是将当下hashMap对象中的存在 键值对 都放入新建数组 Node<K,V>[] newTab 中
返回新建的数组newTab
}
假定的情况1是:
当下hashMap对象中的存在 键值对 且
当下hashMap对象中的键值对数 乘以 2 小于
HashMap的最大容量MAXIMUM_CAPACITY 且
当下hashMap对象中的键值对数 大于 HashMap的最小容量DEFAULT_INITIAL_CAPACITY
在这种情况下做了什么事情:
将当下hashMap对象中的临界值(threshold) 扩大2倍
新建一个元素个数是 当下hashMap对象中的键值对数乘以2 的 Node<K,V>[] 数组
将当下hashMap对象中的键值对 放入新的Node<K,V>[] 数组 ,并作为当下hashMap对象中table的新值
返回新建的数组 Node<K,V>[] newTab
该方法的第三道流程:
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) {
当下hashMap对象中没有键值对
}else if (oldThr > 0){
newCap = oldThr;
}
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) {
当下hashMap对象没有键值对,所以不执行这里
}
}
返回新建的数组newTab
}
假定的情况1是:
当下hashMap对象中的不存在 键值对 且
当下hashMap对象中的临界值(threshold) 不为0 且
当当下hashMap对象中的临界值(threshold)小于
HashMap最大容量MAXIMUM_CAPACITY 且
当下hashMap对象中的临界值(threshold)乘以
HashMap的负载因子 小于
HashMap最大容量MAXIMUM_CAPACITY
在这种情况1下做了什么事情:
将当下hashMap对象中的临界值(threshold)乘以
HashMap的负载因子 的值 作为 当下hashMap对象中的临界值(threshold) 的新值
新建一个元素个数是 当下hashMap对象中没有改变前 临界值(threshold) 的 Node<K,V>[] 数组
返回新建的数组 Node<K,V>[] newTab
假定的情况2是:
当下hashMap对象中的不存在 键值对 且
当下hashMap对象中的临界值(threshold) 不为0 且
当当下hashMap对象中的临界值(threshold)大于等于
HashMap最大容量MAXIMUM_CAPACITY 或
当下hashMap对象中的临界值(threshold)乘以
HashMap的负载因子 大于等于
HashMap最大容量MAXIMUM_CAPACITY
在这种情况2下做了什么事情:
将当下hashMap对象中的临界值(threshold)设置为Integer.MAX_VALUE
新建一个元素个数是 当下hashMap对象中没有改变前 临界值(threshold) 的 Node<K,V>[] 数组
返回新建的数组 Node<K,V>[] newTab
该方法的第四道流程:
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) {
当下hashMap对象中没有键值对
}else if (oldThr > 0){
当下hashMap对象中的临界值等于0
}else {
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
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) {
当下hashMap对象没有键值对,所以不执行这里
}
}
}
假定的情况是:
当下hashMap对象中的不存在 键值对 且
当下hashMap对象中的临界值(threshold) 为0
在这种情况下做了什么事情:
将当下hashMap对象中的临界值(threshold)设置为HashMap的最小容量DEFAULT_INITIAL_CAPACITY 乘以 HashMap的负载因子loadFactor
新建一个元素个数是 HashMap的最小容量值DEFAULT_INITIAL_CAPACITY 的 Node<K,V>[] 数组
返回新建的数组 Node<K,V>[] newTab