haspMap源碼分析之-鏈表拆分問題

Jdk1.8 hashmap在擴容的時候,爲什麼當將一個鏈接拆分成兩個鏈表的時候,key的hash值和oldCap與爲0時,放在原來下標j位置,不爲0時,放在下標j+oldCap位置?

一、源碼:

    /**
     * Initializes or doubles table size.  If null, allocates in
     * accord with initial capacity target held in field threshold.
     * Otherwise, because we are using power-of-two expansion, the
     * elements from each bin must either stay at same index, or move
     * with a power of two offset in the new table.
     *
     * @return the table
     */
    final Node<K,V>[] resize() {
        Node<K,V>[] oldTab = table;
        int oldCap = (oldTab == null) ? 0 : oldTab.length;
        int oldThr = threshold;
        int newCap, newThr = 0;
        if (oldCap > 0) {
            if (oldCap >= MAXIMUM_CAPACITY) {
                threshold = Integer.MAX_VALUE;
                return oldTab;
            }
            else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
                     oldCap >= DEFAULT_INITIAL_CAPACITY)
                newThr = oldThr << 1; // double threshold
        }
        else if (oldThr > 0) // initial capacity was placed in threshold
            newCap = oldThr;
        else {               // zero initial threshold signifies using defaults
            newCap = DEFAULT_INITIAL_CAPACITY;
            newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
        }
        if (newThr == 0) {
            float ft = (float)newCap * loadFactor;
            newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
                      (int)ft : Integer.MAX_VALUE);
        }
        threshold = newThr;
        @SuppressWarnings({"rawtypes","unchecked"})
        Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
        table = newTab;
        if (oldTab != null) {
            for (int j = 0; j < oldCap; ++j) {
                Node<K,V> e;
                if ((e = oldTab[j]) != null) {
                    oldTab[j] = null;
                    if (e.next == null)
                        newTab[e.hash & (newCap - 1)] = e;
                    else if (e instanceof TreeNode)
                        ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
                    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;
                          	//loHead爲什麼放在擴容後的下標j位置?
                            newTab[j] = loHead;
                        }
                        if (hiTail != null) {
                            hiTail.next = null;
                            //hiHead爲什麼放在擴容後的下標j+oldCap位置?
                            newTab[j + oldCap] = hiHead;
                        }
                    }
                }
            }
        }
        return newTab;
    }

二、分析:

爲了簡單起見,設oldCap=4,擴容後newCap=8
在這裏插入圖片描述

左圖爲擴容前,右圖爲擴容後的結果

key hash 二進制 和oldCap-1=3的&結果 和newCap-1=7的&結果
1 1 0000 0001 1 1
5 5 0000 0101 1 5
9 9 0000 1001 1 1
13 13 0000 1101 1 5

上圖中key對應的hash值是假設值,那hash值分別和3、7與的結果如上表,那麼擴容後,重新通過hash值計算對應的桶下標1和9還在原來的下標1位置,5和13已經移到擴容的下標5位置。

而hashMap擴容的時候,並沒有這樣重新計算key對應的下標,而是hash值和oldCap與,如果結果爲0,放在原來下標位置,如果結果非0,那麼放在原來下標+oldCap的下標位置,那麼通過hashmap中的這個方式計算,看看結果

key hash 二進制 和oldCap=4的&結果
1 1 0000 0001 0000 0000
5 5 0000 0101 0000 0100
9 9 0000 1001 0000 0000
13 13 0000 1101 0000 0100

1、9和4與的結果爲0,會放在原來的下標位置,5、13和4與的結果爲非0,會放到原來下標+oldCap=5的位置,這個結果和newCap-1=7的計算下標完全一致,說明用這種方式代替上面那種方式重新計算下標是完全正確的,那麼用下面更一般的方式來證明這種代替方法的正確性。

假設一個數的hash值爲xxxx,那麼和oldCap-1=3與,
 0011
&xxxx
=00xx

如果和oldCap=4與
 0100
&xxxx
=0x00
如果和oldCap=4與結果爲0,那麼hash值xxxx中的第二位爲0,即x0xx

那麼和擴容之後newCap-1=7與,
 0111
&x0xx
=00xx
此時正好和擴容前計算的下標相同

如果和oldCap=4與結果爲非0,那麼hash值xxxx中的第二位爲1,即x1xx

那麼和擴容之後newCap-1=7與
 0111
&x1xx
=01xx
01xx 正好比00xx大oldCap的值,所以此結果是正確的
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章