扩容机制
初始化分析
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
*/
注释: 初始化或者双倍表的大小, 如果是空的话 则在符合场阈值内的初始容量目标。 否则因为我们使用的是二次展开的能力 扩容两倍每个bin中的元素必须保持在同一索引中,或者移动在新表中有两个偏移量的幂。
扩容方法很长!!! 首先先分析初始化的过程~得到信息
- 会使用默认的负载因子和阈值来计算出容量
- DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
- newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); // 12
- 会使用默认的扩容阈值 DEFAULT_LOAD_FACTOR = 0.75f;
- 在初始化之后, 当前 map的阈值也会被初始化
- newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); // 12
- threshold = newThr;
final Node<K,V>[] resize() {
// 旧的 hashmap
Node<K,V>[] oldTab = table;
// 判断是否为 0
int oldCap = (oldTab == null) ? 0 : oldTab.length;
// 得到阈值, 如果是初始化的话 oldThr 是个空
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 {
// 开始初始化 大小为 16 阈值为 12
// DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
// DEFAULT_LOAD_FACTOR = 0.75f;
newCap = DEFAULT_INITIAL_CAPACITY; // 16
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); // 12
}
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
// 定义阈值
// newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); // 12
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
// 新建 返回初始化的map
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;
}
扩容分析
final Node<K,V>[] resize() {
// 旧的 hashmap
Node<K,V>[] oldTab = table;
// 判断是否为 0
int oldCap = (oldTab == null) ? 0 : oldTab.length;
// 得到阈值, 如果是初始化的话 oldThr 是个空
int oldThr = threshold;
int newCap, newThr = 0;
// 扩容 走的代码 得到 原始 map的容量!
// 注意此时又进行了判断!!
if (oldCap > 0) {
// 此时容量已经很大的时候 (1073741824), 就直接一次把扩容的阈值调节到最大!!! 直接返回原来的map
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
// 此时容量已经大于 DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
// 并且也没有到达一个很大的值
// 把newThr 阈值调节到一倍大小 也就意味者扩容了一倍
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 此时就h是扩容一倍后的大小了
newCap = oldThr;
else {
// 开始初始化 大小为 16 阈值为 12
// DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
// DEFAULT_LOAD_FACTOR = 0.75f;
newCap = DEFAULT_INITIAL_CAPACITY; // 16
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); // 12
}
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
// 定义阈值
// newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); // 12
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
// 新建 返回初始化的map
// 扩容时 走到这里, 就是新建立一个 双倍大小的map
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;
}