JDK11版HashMap源碼所有解析(詳細)-一文覆蓋各方面

1. 概述

本文很長,詳細描述了HashMap源碼級別的實現原理,並討論了包括擴容,hash計算,新建HashMap的開銷等問題,同時還提供了一些外部資料。因爲內容太多,建議閱讀時結合目錄快速跳轉查看。java

Java源碼閱讀最好採用IDEA,Ctrl + N 輸入HashMap便可看到HashMap的源碼了,HashMap總共有2444行源碼 本文查看的是JDK-11.0.1的源碼node

[toc]git

我們按照源碼順序來分析HashMap,除了HashMap自己的變量和方法,HashMap中還定義了定義以下內部類:github

1.1 內部類

  1. Node
  2. KeySet
  3. Values
  4. EntrySet
  5. HashIterator
  6. KeyIterator
  7. ValueIterator
  8. EntryIterator
  9. HashMapSpliterator
  10. KeySpliterator
  11. ValueSpliterator
  12. EntrySpliterator
  13. TreeNode:這個類表明紅黑樹節點,HashMap中對紅黑樹的操做的方法都在此類中

1.2 基本實現

HashMap底層使用哈希表(數組 + 單鏈表),當鏈表過長會將鏈表轉成 紅黑樹以實現 O(logn) 時間複雜度內查找。算法

HashMap的定義class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable數組

1.3 擴容原理

HashMap採用的擴容策略是,每次加倍,這樣,原來位置的Entry在新擴展的數組中要麼依然在原來的位置,要麼在<原來位置+原來的容量>的位置。緩存

1.4 hash計算

hash()函數計算hash值方法爲(key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16),計算出的hash值會被緩存在Node.hash中。bash

爲何這樣計算hash值

hash值計算至關於就是將高16位與低16位進行異或,結果是高16不變,低16位變成其異或的新結果。服務器

爲何讓低16位與高16爲異或合成一個新的結果呢?是由於HashMap的容量一般比較小,在進行長度取模運算時採用的是隻取二進制最右端幾位,這樣高位的二進制信息就沒有用到,所帶來的結果就是Hash結果分佈不太均勻。而高16位與低16位異或後就可讓低位附帶高位的信息,加大低位的隨機性。具體請參考JDK 源碼中 HashMap 的 hash 方法原理是什麼? - 胖君的回答 - 知乎 微信

不明白異或結果的朋友來看下這段驗證代碼,複製此代碼運行便可明白高16位與低16位的異或的結果:

import java.util.Random;

class Scratch {
    public static void main(String[] args) {
        generateTestCase(41132564);

        Random random = new Random();
        for (int j = 0; j < 10; j++) {
            generateTestCase(random.nextInt(Integer.MAX_VALUE));
        }
    }

    /** * 顯示根據key的hashCode算出最終元素的hash值 * * @param hashCode 表明key的hashCode */
    public static void generateTestCase(int hashCode) {
        System.out.println("hashCode = " + hashCode + " 時");
        show(hashCode);

        int k = hashCode >>> 16;
        show(k);

        int x = hashCode ^ k;
        show(x);

        System.out.println();
    }

    /** * 顯示一個數字的二進制,按照高16位在左,低16位在右的方式顯示 */
    public static void show(int n) {
        String s = Integer.toBinaryString(n);
        s = fillZero(s);

        System.out.print(s.substring(0, 16));
        System.out.print(" | ");
        System.out.println(s.substring(16));
    }

    /** * 填充0到字符串前面使得總長32 */
    public static String fillZero(String src) {
        StringBuilder sb = new StringBuilder(32);
        for (int i = 0; i < 32 - src.length(); i++) {
            sb.append('0');
        }
        return sb.append(src).toString();
    }
}
複製代碼

這就是爲何HashMap能夠放入鍵值null,由於計算hash中爲null的hash值爲0,而後putVal插入

1.5 元素實際位置計算

根據hash()獲取元素所在鏈表的位置的方法爲:tab[(n - 1) & hash],因爲n爲容量是2的冪,n-1的二進制形式是111111這類二進制左邊全1的形式,因此這個方法本質是截取hash二進制相應長度的0和1,以下例。

hash:   10111101
n - 1:  00111111
result: 00111101
// hash的最左端的1沒有了,至關於只取二進制最右端幾位
複製代碼

1.6 插入null原理

在hash計算中(上文),null的hash值爲0,而後按照正常的putVal()插入

1.7 new HashMap()開銷

從源碼中(下文構造函數)咱們能夠看到:

new HashMap()開銷很是少,僅僅確認裝載因子。真正的建立table的操做盡量的日後延遲,這使得HashMap有很多操做都須要檢查table是否初始化。這種設計我猜測應該是爲了讓人們能夠不用擔憂建立HashMap的開銷,大量建立HashMap,好比ArrayList<HashMap> a = new ArrayList<>(1000)

2. HashMap的變量

2.1 DEFAULT_INITIAL_CAPACITY

HashMap的默認容量是16,被DEFAULT_INITIAL_CAPACITY定義。

static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
複製代碼

2.2 MAXIMUM_CAPACITY

其最大容量爲 1073741824(2的30次方)

static final int MAXIMUM_CAPACITY = 1 << 30;
複製代碼

2.3 DEFAULT_LOAD_FACTOR

默認裝載因子0.75

static final float DEFAULT_LOAD_FACTOR = 0.75f;
複製代碼

2.4 TREEIFY_THRESHOLD

將鏈表轉換成紅黑樹的閾值爲8,即當鏈表長度>=8時,鏈表轉換成紅黑樹

static final int TREEIFY_THRESHOLD = 8;
複製代碼

2.5 UNTREEIFY_THRESHOLD

將紅黑樹轉換成鏈表的閾值爲6(<6時轉換),注意,這個是在resize()的過程當中調用TreeNode.split()實現

static final int UNTREEIFY_THRESHOLD = 6;
複製代碼

2.6 MIN_TREEIFY_CAPACITY

要樹化並不只僅是超過TREEIFY_THRESHOLD ,同時容量要超過MIN_TREEIFY_CAPACITY,若是隻是超過TREEIFY_THRESHOLD,則會進行擴容(調用resize(),由於擴容可讓鏈表變短),直到擴容>=MIN_TREEIFY_CAPACITY

static final int MIN_TREEIFY_CAPACITY = 64;
複製代碼

2.7 table

哈希表的數組主體定義,使用時初始化,在構造函數中並不會初始化,因此在各類操做中老是要檢查其是否爲null

transient Node<K,V>[] table;
複製代碼

2.8 entrySet

做爲一個entrySet緩存,使用entrySet方法首先檢查其是否爲null,不爲null則使用這個緩存,不然生成一個並緩存至此。

transient Set<Map.Entry<K,V>> entrySet;
複製代碼

2.9 size

HashMap中Entry的數量

transient int size;
複製代碼

2.10 modCount

記錄修改內部結構化修改次數,用於實現fail-fast,ConcurrentModificationException就是經過檢測這個拋出

transient int modCount;
複製代碼

2.11 threshold

其值=capacity * load factor,當size超過threshhold便進行一次擴容

int threshold;
複製代碼

2.12 loadFactor

裝載因子

final float loadFactor;
複製代碼

2.13 serialVersionUID

用於序列化

private static final long serialVersionUID = 362498820763181265L;
複製代碼

3. HashMap的方法

3.1 構造函數

  1. 該構造函數並不初始化transient Node<K,V>[] table;,進行容量和裝載因子的(範圍)合法性驗證,然而並無對容量進行存儲,只是用來肯定擴容閾值threshold

    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);
        }
    複製代碼
  2. 顯然

    public HashMap(int initialCapacity) {
            this(initialCapacity, DEFAULT_LOAD_FACTOR);
        }
    複製代碼
  3. 無參構造函數僅僅確認裝載因子

    public HashMap() {
            this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
        }
    複製代碼
  4. 經過Map構造HashMap時,使用默認裝載因子,並調用putMapEntries將Map裝入HashMap

    public HashMap(Map<? extends K, ? extends V> m) {
            this.loadFactor = DEFAULT_LOAD_FACTOR;
            putMapEntries(m, false);
        }
    複製代碼

3.2 hash(Object key)

Hash函數負責產生HashCode,計算法則爲若key爲null則返回0,不然:對key的hashCode的高16位和低16位進行異或

static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);// >>>表示無符號右移
}
複製代碼

3.3 comparableClassFor(Object x)

對於一個Object,若其定義時是class X implement Comparable<X>,則返回X,不然返回null,注意必定Comparable<X>中的X必定得是X不能爲其子類或父類,用於紅黑樹中的比較

  • 源碼以下
static Class<?> comparableClassFor(Object x) {
        if (x instanceof Comparable) {
            Class<?> c; Type[] ts, as; ParameterizedType p;
            if ((c = x.getClass()) == String.class) // bypass checks
                return c;
            if ((ts = c.getGenericInterfaces()) != null) {
                for (Type t : ts) {
                    if ((t instanceof ParameterizedType) &&
                        ((p = (ParameterizedType) t).getRawType() ==
                         Comparable.class) &&
                        (as = p.getActualTypeArguments()) != null &&
                        as.length == 1 && as[0] == c) // type arg is c
                        return c;
                }
            }
        }
        return null;
    }
複製代碼
  • 驗證代碼以下,將以下代碼拷貝便可明白其原理。
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
class Scratch {
    public static void main(String[] args) {
        System.out.println(comparableClassFor(new C()));// class Scratch$C
        System.out.println(comparableClassFor(new CS()));// null
        System.out.println(comparableClassFor(new CSI()));// null
        System.out.println(comparableClassFor(new CSIC()));// class Scratch$CSIC
    }
    static Class<?> comparableClassFor(Object x) {
        if (x instanceof Comparable) {
            Class<?> c; Type[] ts, as; ParameterizedType p;
            if ((c = x.getClass()) == String.class) // bypass checks
                return c;
            if ((ts = c.getGenericInterfaces()) != null) {
                for (Type t : ts) {
                    if ((t instanceof ParameterizedType) &&
                            ((p = (ParameterizedType) t).getRawType() ==
                                    Comparable.class) &&
                            (as = p.getActualTypeArguments()) != null &&
                            as.length == 1 && as[0] == c) // type arg is c
                        return c;
                }
            }
        }
        return null;
    }
    static class C implements Comparable<C> {
        @Override
        public int compareTo(C o) {
            return 0;
        }
    }
    static class CS extends C {}
    static class CSI implements Comparable<C> {
        @Override
        public int compareTo(C o) {
            return 0;
        }
    }
    static class CSIC implements Comparable<CSIC> {
        @Override
        public int compareTo(CSIC o) {
            return 0;
        }
    }
}
複製代碼

3.4 compareComparables(Class<?> kc, Object k, Object x)

若是x=null,返回0;若是x的類型爲kc,則返回k.compare(x);不然返回0

static int compareComparables(Class<?> kc, Object k, Object x) {
        return (x == null || x.getClass() != kc ? 0 :
                ((Comparable)k).compareTo(x));
    }
複製代碼

3.5 tableSizeFor(int cap)

對於給定cap,計算>=cap的2的冪。用於計算table數組大小

static final int tableSizeFor(int cap) {
        int n = -1 >>> Integer.numberOfLeadingZeros(cap - 1);
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }
複製代碼

3.6 putMapEntries(Map<? extends K, ? extends V> m, boolean evict)

先肯定放入map時容量是否應該調整,調整好後,經過putVal一個個放入

final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
        int s = m.size();
        if (s > 0) {//放入的map的size要大於0才插入
            if (table == null) { // pre-size,若是本map的table未初始化(同時沒有任何元素),就根據放入map大小以及loadfactor計算出threshold,依然不初始化table
                float ft = ((float)s / loadFactor) + 1.0F;
                int t = ((ft < (float)MAXIMUM_CAPACITY) ?
                         (int)ft : MAXIMUM_CAPACITY);
                if (t > threshold)
                    threshold = tableSizeFor(t);
            }
            else if (s > threshold)//放入的map超過threshold就擴容
                resize();
            //到這裏容量問題解決了,就一個一個putVal插入
            for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
                K key = e.getKey();
                V value = e.getValue();
                putVal(hash(key), key, value, false, evict);
            }
        }
    }
複製代碼

3.7 size()

直接返回size變量的值

public int size() {
        return size;
}
複製代碼

3.8 isEmpty()

判斷size是否等於0

public boolean isEmpty() {
        return size == 0;
}
複製代碼

3.9 get(Object key)

經過計算hash並調用getNode找到節點Node,而後返回Node.value,找不到Node則返回null

public V get(Object key) {
    Node<K,V> e;
    return (e = getNode(hash(key), key)) == null ? null : e.value;
}
複製代碼

3.10 getNode(int hash, Object key)

若是table未初始化以及長度=0或者根據hash找到鏈表的第一個元素爲nul便返回null,不然判斷第一個節點是否爲要找的節點,不然之後的節點根據紅黑樹類型或鏈表類型採用各自的查找策略

final Node<K,V> getNode(int hash, Object key) {
    Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
    if ((tab = table) != null && (n = tab.length) > 0 &&
        (first = tab[(n - 1) & hash]) != null) {//在table初始化了,且table長度大於0而且根據hash找到鏈表第一個節點不爲null時
        if (first.hash == hash && // always check first node,若是鏈表第一個節點就是要找的便返回
            ((k = first.key) == key || (key != null && key.equals(k))))
            return first;
        if ((e = first.next) != null) {
        	//根據節點是紅黑樹仍是鏈表類型採用不一樣的查找策略
            if (first instanceof TreeNode)
                return ((TreeNode<K,V>)first).getTreeNode(hash, key);
            do {//鏈表類型就是順序遍歷查找
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k))))
                    return e;
            } while ((e = e.next) != null);
        }
    }
    return null;
}
複製代碼

3.11 containsKey(Object key)

根據getNode是否找到節點來判斷是否存在

public boolean containsKey(Object key) {
    return getNode(hash(key), key) != null;
}
複製代碼

3.12 put(K key, V value)

調用putVal放入節點

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

3.13 putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict)

參數解釋:

  • onlyIfAbsent:爲true則不改變已有的值
  • evict:用於LinkedList使用,對於hashmap沒用,若是爲false,table進入建立模式()

先進行table初始化和0長檢查判斷是否須要擴容,以後若是key所在鏈表表頭未初始化便初始化並插入,不然根據是否樹化根據相應策略查找節點,若是onlyIfAbsent爲false則插入。插入後size++並根據threshold判斷是否擴容resize()

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)//table未初始化或0長便調用resize()初始化
        n = (tab = resize()).length;
    if ((p = tab[i = (n - 1) & hash]) == null)//若是相應位置的鏈表還未建立表頭,便建立表頭
        tab[i] = newNode(hash, key, value, null);
    else {
        Node<K,V> e; K k;
        if (p.hash == hash &&
            ((k = p.key) == key || (key != null && key.equals(k))))
            e = p;
        else if (p instanceof TreeNode)
            e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
        else {//在鏈表中順序查找節點
            for (int binCount = 0; ; ++binCount) {
                if ((e = p.next) == null) {
                    p.next = newNode(hash, key, value, null);
                    if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                        treeifyBin(tab, hash);//添加節點的過程當中若是超過樹化閾值,便執行樹化操做
                    break;
                }
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k))))
                    break;
                p = e;
            }
        }
        if (e != null) { // existing mapping for key,e即是插入的key的位置
            V oldValue = e.value;
            if (!onlyIfAbsent || oldValue == null)
                e.value = value;
            afterNodeAccess(e);
            return oldValue;
        }
    }
    ++modCount;
    if (++size > threshold)//若是插入後超過擴容閾值便擴容
        resize();
    afterNodeInsertion(evict);
    return null;
}
複製代碼

3.14 resize() 擴容

閱讀這部分源碼應注意:

  • 全部的 Cap (即newCap,oldCap) 都是2的冪,即二進制爲100000這類形式,Cap-1的二進制都爲111111這類形式。

原理:

  • 先根據限制條件肯定newThr和newCap,而後建立新的table,再把舊table的數據(即鏈表引用)複製過來。同時複製時根據hash值,在同一個鏈表中的元素根據oldCap(二進制是10000的形式)& e.hash(判斷相應位爲0仍是1)來劃分紅兩類,一類放在原位置,一類放在原位置+oldCap位置。
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);
    }
    //以上代碼都是根據限制條件肯定newThr和newCap
    threshold = newThr;
    @SuppressWarnings({"rawtypes","unchecked"})
    //建立新table
    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) {//根據oldCap肯定元素hash指定位上是0仍是1來劃分放在原位置仍是原位置+oldCap位置,這裏是放在原位置
                            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;//原位置+oldCap
                    }
                }
            }
        }
    }
    return newTab;
}
複製代碼

3.15 treeifyBin(Node < K,V > [] tab, int hash)

達到樹化閾值還須要超過MIN_TREEIFY_CAPACITY纔會樹化,不然先進行擴容操做,達到後,先將鏈表Node逐個替換成TreeNode,在調用TreeNode.treeify創建紅黑樹

final void treeifyBin(Node<K,V>[] tab, int hash) {
		int n, index; Node<K,V> e;
		if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)//達到樹化閾值還須要超過MIN_TREEIFY_CAPACITY纔會樹化,不然先進行擴容操做
			resize();
		else if ((e = tab[index = (n - 1) & hash]) != null) {
			TreeNode<K,V> hd = null, tl = null;
			do {//先將鏈表Node逐個替換成TreeNode
				TreeNode<K,V> p = replacementTreeNode(e, null);
				if (tl == null)
					hd = p;
				else {
					p.prev = tl;
					tl.next = p;
				}
				tl = p;
			} while ((e = e.next) != null);
			if ((tab[index] = hd) != null)
				hd.treeify(tab);//這裏進行樹化
		}
	}
複製代碼

3.16 putAll(Map<? extends K, ? extends V> m)

putAll就是調用putMapEntries

public void putAll(Map<? extends K, ? extends V> m) {
	putMapEntries(m, true);
}
複製代碼

3.17 remove(Object key)

remove調用removeNode移除節點後,返回節點value

public V remove(Object key) {
		Node<K,V> e;
		return (e = removeNode(hash(key), key, null, false, true)) == null ?
			null : e.value;
}
複製代碼

3.18 removeNode(int hash, Object key, Object value, boolean matchValue, boolean movable)

根據hash查找key的位置並刪除,若是matchValue爲true,則只有值也相等時才刪除,若是movable爲false,紅黑樹的刪除不移動node,而後size減少,若是沒找到key,返回null

final Node<K,V> removeNode(int hash, Object key, Object value, boolean matchValue, boolean movable) {
		Node<K,V>[] tab; Node<K,V> p; int n, index;
		if ((tab = table) != null && (n = tab.length) > 0 &&
			(p = tab[index = (n - 1) & hash]) != null) {
			Node<K,V> node = null, e; K k; V v;//查找key的位置
			if (p.hash == hash &&
				((k = p.key) == key || (key != null && key.equals(k))))
				node = p;
			else if ((e = p.next) != null) {
				if (p instanceof TreeNode)
					node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
				else {
					do {
						if (e.hash == hash &&
							((k = e.key) == key ||
							(key != null && key.equals(k)))) {
							node = e;
							break;
						}
						p = e;
					} while ((e = e.next) != null);
				}
			}
			if (node != null && (!matchValue || (v = node.value) == value ||
								(value != null && value.equals(v)))) {//matchValue控制值也要相等才刪除
				if (node instanceof TreeNode)	//刪除key並減少size
					((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
				else if (node == p)
					tab[index] = node.next;
				else
					p.next = node.next;
				++modCount;
				--size;
				afterNodeRemoval(node);
				return node;
			}
		}
		return null;
	}
複製代碼

3.19 clear()

size置0,table每一個Node都置爲null

public void clear() {
		Node<K,V>[] tab;
		modCount++;
		if ((tab = table) != null && size > 0) {
			size = 0;
			for (int i = 0; i < tab.length; ++i)
				tab[i] = null;
		}
	}
複製代碼

3.20 containsValue(Object value)

雙重循環查找數組+鏈表中的每一個value

public boolean containsValue(Object value) {
		Node<K,V>[] tab; V v;
		if ((tab = table) != null && size > 0) {
			for (Node<K,V> e : tab) {
				for (; e != null; e = e.next) {
					if ((v = e.value) == value ||
						(value != null && value.equals(v)))
						return true;
				}
			}
		}
		return false;
	}
複製代碼

3.21 keySet()

查看keySet中是否有已緩存的keySet,沒有就建立並加入緩存。值得注意的是因爲keySet採用視圖技術(沒有成員變量),因此建立開銷近乎爲0。

public Set<K> keySet() {
		Set<K> ks = keySet;
		if (ks == null) {
			ks = new KeySet();
			keySet = ks;
		}
		return ks;
	}
複製代碼

3.22 values()

查看是否有已緩存的,沒有則建立Value()並緩存,因爲沒有成員變量,因此建立Values開銷近乎爲0

public Collection<V> values() {
	Collection<V> vs = values;
	if (vs == null) {
		vs = new Values();
		values = vs;
	}
	return vs;
}
複製代碼

3.23 entrySet()

查看是否有已緩存的,沒有則建立EntrySet()並緩存,因爲沒有成員變量,因此建立開銷近乎爲0

public Set<Map.Entry<K,V>> entrySet() {
	Set<Map.Entry<K,V>> es;
	return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
}
複製代碼

3.24 getOrDefault(Object key, V defaultValue)

用getNode查找,找不到則返回defaultValue

@Override
public V getOrDefault(Object key, V defaultValue) {
	Node<K,V> e;
	return (e = getNode(hash(key), key)) == null ? defaultValue : e.value;
}
複製代碼

3.25 putIfAbsent(K key, V value)

調用putVal,並設定putVal的參數onlyIfAbsent=true。只有不存在key的時候才插入

@Override
public V putIfAbsent(K key, V value) {
	return putVal(hash(key), key, value, true, true);
}
複製代碼

3.26 remove(Object key, Object value)

調用removeNode,返回是否刪除成功

@Override
public boolean remove(Object key, Object value) {
	return removeNode(hash(key), key, value, true, true) != null;
}
複製代碼

3.27 replace(K key, V oldValue, V newValue)

經過getNode尋找到Node,判斷oldValue是否與value相等,而後替換value,並有afterNodeAccess鉤子用於LinkedHashMap

@Override
public boolean replace(K key, V oldValue, V newValue) {
    Node<K,V> e; V v;
    if ((e = getNode(hash(key), key)) != null &&
        ((v = e.value) == oldValue || (v != null && v.equals(oldValue)))) {
        e.value = newValue;
        afterNodeAccess(e);
        return true;
    }
    return false;
}
複製代碼

3.28 replace(K key, V value)

經過getNode尋找到Node,而後替換value,並有afterNodeAccess鉤子用於LinkedHashMap

@Override
public V replace(K key, V value) {
	Node<K,V> e;
	if ((e = getNode(hash(key), key)) != null) {
		V oldValue = e.value;
		e.value = value;
		afterNodeAccess(e);
		return oldValue;
	}
	return null;
}
複製代碼

3.29 computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)

若是查找不到key不就運行函數,並將函數返回值插入到key中。function不能更改modCount(不能修改HashMap),不然會ConcurrentModificationException

@Override
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
	if (mappingFunction == null)
		throw new NullPointerException();
	int hash = hash(key);
	Node<K,V>[] tab; Node<K,V> first; int n, i;
	int binCount = 0;
	TreeNode<K,V> t = null;
	Node<K,V> old = null;
	if (size > threshold || (tab = table) == null ||
		(n = tab.length) == 0)
		n = (tab = resize()).length;
	if ((first = tab[i = (n - 1) & hash]) != null) {
		if (first instanceof TreeNode)
			old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);
		else {
			Node<K,V> e = first; K k;
			do {
				if (e.hash == hash &&
					((k = e.key) == key || (key != null && key.equals(k)))) {
					old = e;
					break;
				}
				++binCount;
			} while ((e = e.next) != null);
		}
		V oldValue;
		if (old != null && (oldValue = old.value) != null) {//找到後便返回並處理鉤子
			afterNodeAccess(old);
			return oldValue;
		}
	}//沒找到時
	int mc = modCount;
	V v = mappingFunction.apply(key);
	if (mc != modCount) { throw new ConcurrentModificationException(); }
	//將mappingFunction產生的返回值做爲value插入到key中,若是超過樹化閾值就樹化,size++
	if (v == null) {
		return null;
	} else if (old != null) {
		old.value = v;
		afterNodeAccess(old);
		return v;
	}
	else if (t != null)
		t.putTreeVal(this, tab, hash, key, v);
	else {
		tab[i] = newNode(hash, key, v, first);
		if (binCount >= TREEIFY_THRESHOLD - 1)
			treeifyBin(tab, hash);
	}
	modCount = mc + 1;
	++size;
	afterNodeInsertion(true);
	return v;
}
複製代碼

3.30 computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)

若是查找到key就運行remappingFunction,並將返回值做爲value插入。function不能更改modCount(不能修改HashMap),不然會ConcurrentModificationException。若是返回值爲null,就經過removeNode刪除key

@Override
	public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
		if (remappingFunction == null)
			throw new NullPointerException();
		Node<K,V> e; V oldValue;
		int hash = hash(key);
		if ((e = getNode(hash, key)) != null &&
			(oldValue = e.value) != null) {//
			int mc = modCount;
			V v = remappingFunction.apply(key, oldValue);
			if (mc != modCount) { throw new ConcurrentModificationException(); }
			if (v != null) {
				e.value = v;
				afterNodeAccess(e);
				return v;
			}
			else
				removeNode(hash, key, null, false, true);
		}
		return null;
	}
複製代碼

3.31 compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)

查找key對應的value,若是key不在map中,則value爲null,並以這兩個參數調用remappingFunction,函數返回值爲v。若是以前key不在map中而v不爲null,就插入key和v。若是key在map中:若是v爲null,就刪除key;若是v不爲null,則修改value爲v。其間還有併發檢查和訪問插入鉤子函數用於實現LRUCache

@Override
	public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
		if (remappingFunction == null)
			throw new NullPointerException();
		int hash = hash(key);
		Node<K,V>[] tab; Node<K,V> first; int n, i;
		int binCount = 0;
		TreeNode<K,V> t = null;
		Node<K,V> old = null;
		if (size > threshold || (tab = table) == null ||
			(n = tab.length) == 0)//常規table檢查
			n = (tab = resize()).length;
		if ((first = tab[i = (n - 1) & hash]) != null) {//判斷hash相應鏈表是否存在,而後尋找節點
			if (first instanceof TreeNode)
				old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);
			else {
				Node<K,V> e = first; K k;
				do {
					if (e.hash == hash &&
						((k = e.key) == key || (key != null && key.equals(k)))) {
						old = e;
						break;
					}
					++binCount;
				} while ((e = e.next) != null);
			}
		}
		V oldValue = (old == null) ? null : old.value;
		int mc = modCount;
		V v = remappingFunction.apply(key, oldValue);//調用函數
		if (mc != modCount) { throw new ConcurrentModificationException(); }
		if (old != null) {
			if (v != null) {
				old.value = v;
				afterNodeAccess(old);
			}
			else
				removeNode(hash, key, null, false, true);
		}
		else if (v != null) {
			if (t != null)
				t.putTreeVal(this, tab, hash, key, v);
			else {
				tab[i] = newNode(hash, key, v, first);
				if (binCount >= TREEIFY_THRESHOLD - 1)
					treeifyBin(tab, hash);
			}
			modCount = mc + 1;
			++size;
			afterNodeInsertion(true);
		}
		return v;
	}
複製代碼

3.32 merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)

查找key,若是key存在:map中key對應的value若是不爲null,就調用remappingFunction,返回值爲v,若是返回值爲null且調用merge的value爲null,則從map中刪除key;不然用remappingFunction的返回值插入value;若是調用merge的value不爲null,插入value。

@Override
public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
	if (value == null)
		throw new NullPointerException();
	if (remappingFunction == null)
		throw new NullPointerException();
	int hash = hash(key);
	Node<K,V>[] tab; Node<K,V> first; int n, i;
	int binCount = 0;
	TreeNode<K,V> t = null;
	Node<K,V> old = null;
	if (size > threshold || (tab = table) == null ||
		(n = tab.length) == 0)//常規table檢查
		n = (tab = resize()).length;
	if ((first = tab[i = (n - 1) & hash]) != null) {//檢查hash後table中是否存在相應鏈表,而後尋找key
		if (first instanceof TreeNode)
			old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);
		else {
			Node<K,V> e = first; K k;
			do {
				if (e.hash == hash &&
					((k = e.key) == key || (key != null && key.equals(k)))) {
					old = e;
					break;
				}
				++binCount;
			} while ((e = e.next) != null);
		}
	}
	if (old != null) {
		V v;
		if (old.value != null) {
			int mc = modCount;
			v = remappingFunction.apply(old.value, value);
			if (mc != modCount) {
				throw new ConcurrentModificationException();
			}
		} else {
			v = value;
		}
		if (v != null) {
			old.value = v;
			afterNodeAccess(old);
		}
		else
			removeNode(hash, key, null, false, true);
		return v;
	}
	if (value != null) {
		if (t != null)
			t.putTreeVal(this, tab, hash, key, value);
		else {
			tab[i] = newNode(hash, key, value, first);
			if (binCount >= TREEIFY_THRESHOLD - 1)
				treeifyBin(tab, hash);
		}
		++modCount;
		++size;
		afterNodeInsertion(true);
	}
	return value;
}
複製代碼

3.33 forEach(BiConsumer<? super K, ? super V> action)

進行基本合法性檢查,而後調用foreach循環調用actioin

@Override
public void forEach(BiConsumer<? super K, ? super V> action) {
	Node<K,V>[] tab;
	if (action == null)
		throw new NullPointerException();
	if (size > 0 && (tab = table) != null) {
		int mc = modCount;
		for (Node<K,V> e : tab) {
			for (; e != null; e = e.next)
				action.accept(e.key, e.value);
		}
		if (modCount != mc)
			throw new ConcurrentModificationException();
	}
}
複製代碼

3.34 replaceAll(BiFunction<? super K, ? super V, ? extends V> function)

進行基本合法性檢查,而後foreach循環調用action並將返回值賦給value

@Override
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
	Node<K,V>[] tab;
	if (function == null)
		throw new NullPointerException();
	if (size > 0 && (tab = table) != null) {
		int mc = modCount;
		for (Node<K,V> e : tab) {
			for (; e != null; e = e.next) {
				e.value = function.apply(e.key, e.value);
			}
		}
		if (modCount != mc)
			throw new ConcurrentModificationException();
	}
}
複製代碼

3.35 clone()

從新初始化,而後把當前HashMap插入到克隆的HashMap中

@Override
public Object clone() {
	HashMap<K,V> result;
	try {
		result = (HashMap<K,V>)super.clone();
	} catch (CloneNotSupportedException e) {
		// this shouldn't happen, since we are Cloneable
		throw new InternalError(e);
	}
	result.reinitialize();
	result.putMapEntries(this, false);
	return result;
}
複製代碼

3.36 loadFactor()

直接返回loadFactor

final float loadFactor() { return loadFactor; }
複製代碼

3.37 capacity()

若是table不爲null,返回table的長度,不然若是threshold大於0,返回threshol,不然返回16(即DEFAULT_INITIAL_CAPACITY)

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

3.38 writeObject(java.io.ObjectOutputStream s)

經過ObjectOutputStream先寫入非static和非transient的變量(即threshold和loadFactor),而後寫入capacity,size,最後調用internalWriteEntries()寫入table中的鍵值對

private void writeObject(java.io.ObjectOutputStream s) throws IOException {
		int buckets = capacity();
		// Write out the threshold, loadfactor, and any hidden stuff
		s.defaultWriteObject();
		s.writeInt(buckets);
		s.writeInt(size);
		internalWriteEntries(s);
	}
複製代碼

3.39 readObject(java.io.ObjectInputStream s)

按照writeObject相反的順序讀出變量,可是capacity會被丟棄從新計算出來。最後鍵值對讀出來再putVal進去

private void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException {
		// Read in the threshold (ignored), loadfactor, and any hidden stuff
		s.defaultReadObject();
		reinitialize();
		if (loadFactor <= 0 || Float.isNaN(loadFactor))
			throw new InvalidObjectException("Illegal load factor: " +
											loadFactor);
		s.readInt();                // Read and ignore number of buckets
		int mappings = s.readInt(); // Read number of mappings (size)
		if (mappings < 0)
			throw new InvalidObjectException("Illegal mappings count: " +
											mappings);
		else if (mappings > 0) { // (if zero, use defaults)
			// Size the table using given load factor only if within
			// range of 0.25...4.0
			float lf = Math.min(Math.max(0.25f, loadFactor), 4.0f);
			float fc = (float)mappings / lf + 1.0f;
			int cap = ((fc < DEFAULT_INITIAL_CAPACITY) ?
					DEFAULT_INITIAL_CAPACITY :
					(fc >= MAXIMUM_CAPACITY) ?
					MAXIMUM_CAPACITY :
					tableSizeFor((int)fc));
			float ft = (float)cap * lf;
			threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ?
						(int)ft : Integer.MAX_VALUE);

			// Check Map.Entry[].class since it's the nearest public type to
			// what we're actually creating.
			SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Map.Entry[].class, cap);
			@SuppressWarnings({"rawtypes","unchecked"})
			Node<K,V>[] tab = (Node<K,V>[])new Node[cap];
			table = tab;

			// Read the keys and values, and put the mappings in the HashMap
			for (int i = 0; i < mappings; i++) {
				@SuppressWarnings("unchecked")
					K key = (K) s.readObject();
				@SuppressWarnings("unchecked")
					V value = (V) s.readObject();
				putVal(hash(key), key, value, false, false);
			}
		}
	}
複製代碼

3.40 newNode(int hash, K key, V value, Node<K,V> next) 顯然

Node<K,V> newNode(int hash, K key, V value, Node<K,V> next) {
	return new Node<>(hash, key, value, next);
}
複製代碼

3.41 replacementNode(Node < K,V > p, Node < K,V > next)

顯然

Node<K,V> replacementNode(Node<K,V> p, Node<K,V> next) {
	return new Node<>(p.hash, p.key, p.value, next);
}
複製代碼

3.42 newTreeNode(int hash, K key, V value, Node < K,V > next)

顯然

TreeNode<K,V> newTreeNode(int hash, K key, V value, Node<K,V> next) {
	return new TreeNode<>(hash, key, value, next);
}
複製代碼

3.43 replacementTreeNode(Node < K,V > p, Node < K,V > next)

顯然

TreeNode<K,V> replacementTreeNode(Node<K,V> p, Node<K,V> next) {
	return new TreeNode<>(p.hash, p.key, p.value, next);
}
複製代碼

3.44 reinitialize()

將除了threshold和loadFactor以外的變量所有初始化

void reinitialize() {
	table = null;
	entrySet = null;
	keySet = null;
	values = null;
	modCount = 0;
	threshold = 0;
	size = 0;
}
複製代碼

3.45 afterNodeAccess(Node < K,V > p)

給子類(LinkedHashMap)的鉤子

void afterNodeAccess(Node<K,V> p) { }
複製代碼

3.46 afterNodeInsertion(boolean evict)

給子類(LinkedHashMap)的鉤子

void afterNodeInsertion(boolean evict) { }
複製代碼

3.47 afterNodeRemoval(Node < K,V > p)

給子類(LinkedHashMap)的鉤子

void afterNodeRemoval(Node<K,V> p) { }
複製代碼

3.48 internalWriteEntries(java.io.ObjectOutputStream s)

雙重循環遍歷全部key同時寫入key value至序列化流中。

void internalWriteEntries(java.io.ObjectOutputStream s) throws IOException {
		Node<K,V>[] tab;
		if (size > 0 && (tab = table) != null) {
			for (Node<K,V> e : tab) {
				for (; e != null; e = e.next) {
					s.writeObject(e.key);
					s.writeObject(e.value);
				}
			}
		}
	}
複製代碼

4. HashMap的內部類

這裏就介紹下Node,KeySet,ValueSet和TreeNode,其它的暫且略去,也比較易懂

4.1 Node

static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        V value;
        Node<K,V> next;

        Node(int hash, K key, V value, Node<K,V> next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }

        public final K getKey() { return key; }
        public final V getValue() { return value; }
        public final String toString() { return key + "=" + value; }

        public final int hashCode() {
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        }

        public final V setValue(V newValue) {
            V oldValue = value;
            value = newValue;
            return oldValue;
        }

        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;
        }
    }
複製代碼

Node的成員變量包括:

final int hash;
    final K key;
    V value;
    Node<K,V> next;
複製代碼

成員函數基本很明朗,就不怎麼解釋了

4.2 KeySet

final class KeySet extends AbstractSet<K> {
        public final int size() { return size; }
        public final void clear() { HashMap.this.clear(); }
        public final Iterator<K> iterator() { return new KeyIterator(); }
        public final boolean contains(Object o) { return containsKey(o); }
        public final boolean remove(Object key) {
            return removeNode(hash(key), key, null, false, true) != null;
        }
        public final Spliterator<K> spliterator() {
            return new KeySpliterator<>(HashMap.this, 0, -1, 0, 0);
        }
        public final void forEach(Consumer<? super K> action) {
            Node<K,V>[] tab;
            if (action == null)
                throw new NullPointerException();
            if (size > 0 && (tab = table) != null) {
                int mc = modCount;
                for (Node<K,V> e : tab) {
                    for (; e != null; e = e.next)
                        action.accept(e.key);
                }
                if (modCount != mc)
                    throw new ConcurrentModificationException();
            }
        }
    }
複製代碼

能夠看到,KeySet是經過將大部分方法委託給其它類(主要是HashMap)來實現的,foreach例外。建立KeySet沒有任何開銷(就跟new Object())同樣。

4.3 EntrySet

final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
        public final int size() { return size; }
        public final void clear() { HashMap.this.clear(); }
        public final Iterator<Map.Entry<K,V>> iterator() {
            return new EntryIterator();
        }
        public final boolean contains(Object o) {
            if (!(o instanceof Map.Entry))
                return false;
            Map.Entry<?,?> e = (Map.Entry<?,?>) o;
            Object key = e.getKey();
            Node<K,V> candidate = getNode(hash(key), key);
            return candidate != null && candidate.equals(e);
        }
        public final boolean remove(Object o) {
            if (o instanceof Map.Entry) {
                Map.Entry<?,?> e = (Map.Entry<?,?>) o;
                Object key = e.getKey();
                Object value = e.getValue();
                return removeNode(hash(key), key, value, true, true) != null;
            }
            return false;
        }
        public final Spliterator<Map.Entry<K,V>> spliterator() {
            return new EntrySpliterator<>(HashMap.this, 0, -1, 0, 0);
        }
        public final void forEach(Consumer<? super Map.Entry<K,V>> action) {
            Node<K,V>[] tab;
            if (action == null)
                throw new NullPointerException();
            if (size > 0 && (tab = table) != null) {
                int mc = modCount;
                for (Node<K,V> e : tab) {
                    for (; e != null; e = e.next)
                        action.accept(e);
                }
                if (modCount != mc)
                    throw new ConcurrentModificationException();
            }
        }
    }
複製代碼

能夠看到,EntrySet是經過將大部分方法委託給其它類(主要是HashMap)來實現的,foreach例外。建立EntrySet沒有任何開銷(就跟new Object())同樣。

4.4 TreeNode

因爲源碼太長,就不貼上來了。TreeNode主要實現了紅黑樹的全部操做,對於紅黑樹的原理,建議查找相關資料。

咱們先看下TreeNode的成員變量有哪些:

//繼承自Node
        final int hash;
        final K key;
        V value;
        Node<K,V> next;
		//繼承自LinkedHashMap.Entry
        Entry<K,V> before, after;
		//自帶
		TreeNode<K,V> parent;  // red-black tree links
        TreeNode<K,V> left;
        TreeNode<K,V> right;
        TreeNode<K,V> prev;    // needed to unlink next upon deletion
複製代碼

可見,採用TreeNode在使用通常64位JVM(引用大小爲8位),則TreeNode的大小是Node的2.7倍。不過如今服務器內存愈來愈大,用內存換時間(從O(n)->O(logN))仍是划得來的。




後記

關於利用HashMap中留下的鉤子函數,實際上是給LinkedHashMap用於實現LRUCache的,如何實現請看這篇文章:如何設計實現一個LRU Cache?



參考資料

  1. 阿里二面準備(Java 研發)
  2. 基礎知識(一) HashMap 源碼詳解
  3. HashMap的擴容及樹化過程
  4. HashMap.comparableClassFor(Object x)方法解讀





歡迎訪問個人 我的網站(主要), Github, CSDN(主要), 博客園, 簡書, 掘金, 知乎, 微信公衆號:HelloVant(主要)

本文采用 知識共享 署名-非商業性使用-禁止演繹(CC by-nc-nd) 4.0 國際 許可協議 受權

相關文章
相關標籤/搜索