天天看點

[Java 8 HashMap 詳解系列] 1.HashMap 的存儲資料結構

[Java 8 HashMap 詳解系列] 文章目錄

​​1.HashMap 的存儲資料結構​​

​​2.HashMap 中 Key 的 index 是怎樣計算的?​​

​​3.HashMap 的 put() 方法執行原理​​

​​4.HashMap 的 get() 方法執行原理​​

​​5.HashMap 的 remove() 方法執行原理​​

​​6.HashMap 的擴容 resize() 原理​​

​​7.HashMap 中的紅黑樹原理​​

1.HashMap 的存儲資料結構

為什麼使用 Node<K,V>[] 數組的資料結構來存儲?

從底層資料結構來說,HashMap是通過數組+連結清單+紅黑樹來進行資料存儲的,數組是為了通過通過下标直接定位到資料,連結清單和紅黑樹都是為了解決沖突而引入的,紅黑樹是為了解決在沖突比較嚴重時,連結清單過長而導緻查詢效率降低,從面通過紅黑樹來提升查詢效率。HashMap底層基本的存儲結構如下圖所示:

[Java 8 HashMap 詳解系列] 1.HashMap 的存儲資料結構
public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable {
    /* ---------------- Fields -------------- */
    /**
     * The table, initialized on first use, and resized as
     * necessary. When allocated, length is always a power of two.
     * (We also tolerate length zero in some operations to allow
     * bootstrapping mechanics that are currently not needed.)
     */
    transient Node<K,V>[] table;
}      

從上圖可以看出,HashMap底層基本的結構就是一個數組table=Node<K,V>[],通過Key的hashCode定位到相應的位置(下标),然後在連結清單或紅黑樹中插入Node<K,V>節點,進而完成整個HashMap資料的存儲。接下來,我們看一下承載資料的Node<K,V>的定義是怎麼樣的:

/**
     * Basic hash bin node, used for most entries.  (See below for
     * TreeNode subclass, and in LinkedHashMap for its Entry subclass.)
     */
    static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        V value;
        Node<K,V> next;
        
        ...
        
        public final boolean equals(Object o) {
            if (o == this)
                return true;
            if (o instanceof Map.Entry) {
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
                if (Objects.equals(key, e.getKey()) &&
                    Objects.equals(value, e.getValue()))
                    return true;
            }
            return false;
        }
    }      

厘清楚幾個概念:capacity、size、threshold

作為Java中最常用的K-V資料類型,HashMap的源碼有很多地方值得細讀。首先,需要區厘清楚幾個概念:capacity、size、threshold.

容量(capacity)

是指目前map最多可以存放多少個元素,

大小 ( size )

是指目前map已經存放了多少個k-v鍵值對。

threshold 門檻值

是擴容的門檻值,當size超過門檻值後,便需要對map進行擴容。也就是說,一般情況下,map當中的鍵值對數量不會達到其容量上限。門檻值一般為:capacity*loadFactor(負載因子)

一、預設情況下,new HashMap()得到的對象,其容量為16,負載因子為0.75

/**
     * The default initial capacity - MUST be a power of two.
     */
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

    /**
     * The maximum capacity, used if a higher value is implicitly specified
     * by either of the constructors with arguments.
     * MUST be a power of two <= 1<<30.
     */
    static final int MAXIMUM_CAPACITY = 1 << 30;

    /**
     * The load factor used when none specified in constructor.
     */
    static final float DEFAULT_LOAD_FACTOR = 0.75f;      

二、在初始化map時,若指定了容量大小,那麼,實際的容量值為大于等于該數的第一個2的幂的值。

即:tableSizeFor方法結果: (1 => 1 ; 5=> 8 ; 8=>8 ; 9=> 16)

在下面的代碼執行時:

Map m = new HashMap(5);      

實際調用了:

/**
     * Returns a power of two size for the given target capacity.
     */
    static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }

public HashMap(int initialCapacity, float loadFactor) {
    if (initialCapacity < 0)
        throw new IllegalArgumentException("Illegal initial capacity: " +
                                           initialCapacity);
    if (initialCapacity > MAXIMUM_CAPACITY)
        initialCapacity = MAXIMUM_CAPACITY;
    if (loadFactor <= 0 || Float.isNaN(loadFactor))
        throw new IllegalArgumentException("Illegal load factor: " +
                                           loadFactor);
    this.loadFactor = loadFactor;
    this.threshold = tableSizeFor(initialCapacity);
}      

這裡的tableForSize方法,位移操作,在進行按位或,實際上是将一個二進制數從最高位不為0起,将其後面所有的位數都置為1.

例如:

0010 0000   (原始資料)
0001 0000    (右移1位)
-------------(按位 或)
0011 0000   
0000 1100
-------------
0011 1100
0000 0011
-------------
0011 1111      

是以,tableForSize方法,實際上是将一個32位的資料,從最高位不為0起,後面全部置為1,然後再+1,結果就是最接近指定大小的數的2的幂.

值得注意的是,上面的源碼中,是将tableForSize的值指派給了threshold, 那為何說是我們初始化容量(capacity)的大小為該值呢?

因為在Map初始化時,是第一次向map添加資料才會觸發的。第一次put資料時,調用:

public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }

    final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;   //注意這一行代碼
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
           ...
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }      

注意上面,putVal會判斷table是否為null

if ((tab = table) == null || (n = tab.length) == 0)      

如果為null,則調用resize方法:

n = (tab = resize()).length;      

而resize方法中:

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作為了初始化的容量大小。

不過,對于初始化容量為1時,即 ​

​new HashMap(1)​

​​ .

此刻的capacity有一點不同,就是在沒有調用put方法時,capacity==1 ;

再調用put之後,capacity ==2。這是因為發生了擴容:

HashMap<String,String> m = new HashMap(1); 
        //m.put("",""); //調用之後,capacatiy會等于2
        Method method = m.getClass().getDeclaredMethod("capacity");
        method.setAccessible(true);
        System.out.println(method.invoke(m)); // 輸出:1      

其它情況下:

HashMap<String,String> m = new HashMap(3); 
        //m.put("",""); //調用之後,capacatiy會等于4,調用與否都是一緻的
        Method method = m.getClass().getDeclaredMethod("capacity");
        method.setAccessible(true);
        System.out.println(method.invoke(m)); // 輸出:4      

實際上,調用capacity方法時:

final int capacity() {
        return (table != null) ? table.length :
            (threshold > 0) ? threshold :
            DEFAULT_INITIAL_CAPACITY;
    }      

如果已經初始化了 table 數組,則傳回數組的大小,否則傳回 threadhold.

三、擴容時的操作

/**
     * 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;
                            newTab[j] = loHead;
                        }
                        if (hiTail != null) {
                            hiTail.next = null;
                            newTab[j + oldCap] = hiHead;
                        }
                    }
                }
            }
        }
        return newTab;
    }      

每次觸發擴容時,capacity會變為原來的兩倍: ​

​newThr = oldThr << 1; // double threshold​

​ .

為什麼 HashMap 的容量必須是2的n次方?

為什麼HashMap 最大容量 MAXIMUM_CAPACITY 設定成1 << 30?

​HashMap​

​​ 内部由​

​Entry[]​

​​數組構成,Java 的數組下标是由 ​

​int​

​​ 表示的。是以對于HashMap來說其最大的容量應該是不超過int最大值的一個 2 的指數幂,而最接近 ​

​int​

​​ 最大值的2個指數幂用位運算符表示就是 ​

​1 << 30​

​ .

Kotlin 開發者社群