Why is the implementation of resizing HashMap # so complicated?

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 { // preserve order
    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?

+8
source share
2 answers

EDIT: The threshold for the tree basket changes as the table grows larger. This is what he does.

I have not read the entire file, but this may be a possible cause (line 220)

plain vs tree LinkedHashMap. , , , LinkedHashMap . ( , .)

0

. , , : "hi" "lo", ((e.hash & oldCap) == 0). Java 7 , , .

0

All Articles