if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//如果有相同的hash和key,则退出循环
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;//将p调整为下一个节点
}
}
//若e不为null,表示已经存在与待插入节点hash、key相同的节点,hashmap后插入的key值对应的value会覆盖以前相同key值对应的value值,就是下面这块代码实现的
if (e != null) { // existing mapping for key
V oldValue = e.value;
//判断是否修改已插入节点的value
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;//插入新节点后,hashmap的结构调整次数+1
if (++size > threshold)
resize();//HashMap中节点数+1,如果大于threshold,那么要进行一次扩容
afterNodeInsertion(evict);
return null;
}
2.扩容函数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
*/
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;//定义临时Node数组型变量,作为hash table
//读取hash table的长度
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;//读取扩容门限
int newCap, newThr = 0;//初始化新的table长度和门限值
if (oldCap > 0) {
//执行到这里,说明table已经初始化
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
//用构造器初始化了门限值,将门限值直接赋给新table容量
newCap = oldThr;
else {
// zero initial threshold signifies using defaults
//老的table容量和门限值都为0,初始化新容量,新门限值,在调用hashmap()方式构造容器时,就采用这种方式初始化
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {
//如果门限值为0,重新设置门限
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;//更新新门限值为threshold
@SuppressWarnings({"rawtypes","unchecked"})
//初始化新的table数组
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
//当原来的table不为null时,需要将table[i]中的节点迁移
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
//取出链表中第一个节点保存,若不为null,继续下面操作
if ((e = oldTab[j]) != null) {
oldTab[j] = null;//主动释放
if (e.next == null)
//链表中只有一个节点,没有后续节点,则直接重新计算在新table中的index,并将此节点存储到新table对应的index位置处
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)
//若e是红黑树节点,则按红黑树移动
((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 {
//下面这段暂时没有太明白,通过e.hash & oldCap将链表分为两队,参考知乎上的一段解释
/**
* 把链表上的键值对按hash值分成lo和hi两串,lo串的新索引位置与原先相同[原先位
* j],hi串的新索引位置为[原先位置j+oldCap];
* 链表的键值对加入lo还是hi串取决于 判断条件if ((e.hash & oldCap) == 0),因为* capacity是2的幂,所以oldCap为10...0的二进制形式,若判断条件为真,意味着
* oldCap为1的那位对应的hash位为0,对新索引的计算没有影响(新索引
* =hash&(newCap-*1),newCap=oldCap<<2);若判断条件为假,则 oldCap为1的那位* 对应的hash位为1,
* 即新索引=hash&( newCap-1 )= hash&( (oldCap<<2) - 1),相当于多了10...0,
* 即 oldCap