探究HashMap1.8的扩容

扩容前

探究HashMap1.8的扩容_第1张图片

 

扩容后

探究HashMap1.8的扩容_第2张图片

 机制

探究HashMap1.8的扩容_第3张图片

 

 

else { // preserve order
    Node loHead = null, loTail = null;//低指针
    Node hiHead = null, hiTail = null;//高指针
    Node 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
    }
}

 

你可能感兴趣的:(探究HashMap1.8的扩容)