扩容发生的条件
- 当 HashMap 中键值对 key-value 个数大于阀值的时候(注意不是什么桶或数组的占用情况)
- HashMap 为空或者数组长度为 0
- 升级成红黑树时,数组长度小于64,会进行一次扩容代替升级
//在put时会执行putVal()方法
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
... ...
//数组为空,或者数组长度为0会扩容
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
... ...
//键值对的个数大于阀值会扩容
if (++size > threshold)