探究HashMap1.8的扩容

前提是你 提交于 2020-02-09 15:02:33

扩容前

 

扩容后

 机制

 

 

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
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!