在理解死循环之前你得弄懂transfer()
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
for (Entry<K,V> e : table) {
while(null != e) {
Entry<K,V> next = e.next;
if (rehash) {
e.hash = null == e.key ? 0 : hash(e.key);
}
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}
table:待扩容的
代码分析:
遍历table,对不为空的table子元素进行操作;
对table子元素进行重新求hash值;
根据hash求出当前table子元素在新的table中的位置;
最后将子元素插入table中
transfer之后发现,B在A上面 == 头插入
下面来讲一讲 1.7下的 HashMap的死循环
偷偷懒不想写了
可以参考别的博主的文章 HashMap的循环链表图解_hashmap循环链表-CSDN博客