After reading the source code from java.util.HashMap#resize, I was very confused with some part - that is, when some binhave more than one node.
else {
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;
}
}
Why do I feel this part should not exist? Just use the code below
newTab[e.hash & (newCap - 1)] = e;
okay - I think they have the same effect.
So why bother with so much code in the else branch?
source
share