概述
- jdk1.7才会出现的死循环问题。
-
过程
扩容时,链表节点,经过计算会转移到新数组同一个位置的链表节点上。
转移代码
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);// 无论新数组对应位置是什么,都放到当前节点的下一个(这样兼容了链表节点和普通节点,因为普通节点的next会是null)e.next = newTable[i];// 把新节点作为头结点放在新数组newTable[i] = e;// 进行下一步循环e = next;}}}
