探究HashMap1.8的扩容

扩容前

扩容后

 机制

else { // preserve order
    Node<K,V> loHead = null, loTail = null;//低指针
    Node<K,V> hiHead = null, hiTail = null;//高指针
    Node<K,V> next;
    do {//采用尾插法,与1.7头插法不同,不会产生环
        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;//newIndex = oldIndex
    }
    if (hiTail != null) {
        hiTail.next = null;
        newTab[j + oldCap] = hiHead;//newIndex = oldIndex + oldCap
    }
}

猜你喜欢

转载自www.cnblogs.com/AllenDuke/p/12287093.html