HashMap底層原理java
背景:由於我不知道下一生仍是否能碰見你 因此我此生纔會那麼努力把最好的給你。HashMap底層原理和源碼擼一遍面試不慌。node
1、HashMap簡介面試
1. HashMap是用於存儲Key-Value鍵值對的集合;算法
2. HashMap根據鍵的hashCode值存儲數據,大多數狀況下能夠直接定位到它的值,So具備很快的訪問速度,但遍歷順序不肯定;bootstrap
3. HashMap中鍵key爲null的記錄至多只容許一條,值value爲null的記錄能夠有多條;數組
4. HashMap非線程安全,即任一時刻容許多個線程同時寫HashMap,可能會致使數據的不一致。安全
圖1. HashMap的繼承數據結構
2、HashMap底層存儲結構併發
從總體結構上看HashMap是由數組+鏈表+紅黑樹(JDK1.8後增長了紅黑樹部分)實現的。app
圖2. HashMap總體存儲結構
數組:
HashMap是一個用於存儲Key-Value鍵值對的集合,每個鍵值對也叫作一個Entry;這些Entry分散的存儲在一個數組當中,該數組就是HashMap的主幹。
圖3. HashMap存儲Entry的數組
鏈表:
由於數組Table的長度是有限的,使用hash函數計算時可能會出現index衝突的狀況,因此咱們須要鏈表來解決衝突;數組Table的每個元素不單純只是一個Entry對象,它仍是一個鏈表的頭節點,每個Entry對象經過Next指針指向下一個Entry節點;當新來的Entry映射到衝突數組位置時,只須要插入對應的鏈表位置便可。
圖4. HashMap鏈表
index衝突例子以下:
好比調用 hashMap.put("China", 0) ,插入一個Key爲「China"的元素;這時候咱們須要利用一個哈希函數來肯定Entry的具體插入位置(index):經過index = Hash("China"),假定最後計算出的index是2,那麼Entry的插入結果以下:
圖5. index衝突-1
可是,由於HashMap的長度是有限的,當插入的Entry愈來愈多時,再完美的Hash函數也不免會出現index衝突的狀況。好比下面這樣:
圖6. index衝突-2
通過hash函數計算髮現即將插入的Entry的index值也爲2,這樣就會與以前插入的Key爲「China」的Entry起衝突;這時就能夠用鏈表來解決衝突,當新來的Entry映射到衝突的數組位置時,只須要插入到對應的鏈表便可;此外,新來的Entry節點插入鏈表時使用的是「頭插法」,即會插在鏈表的頭部,由於HashMap的發明者認爲後插入的Entry被查找的機率更大。
圖7. index衝突-3
紅黑樹:
當鏈表長度超過閾值(8)時,會將鏈表轉換爲紅黑樹,使HashMap的性能獲得進一步提高。
圖8. HashMap紅黑樹
HashMap底層存儲結構源碼:
Node<K,V>類用來實現數組及鏈表的數據結構:
1 /** 數組及鏈表的數據結構
2 * Basic hash bin node, used for most entries. (See below for 3 * TreeNode subclass, and in LinkedHashMap for its Entry subclass.) 4 */
5 static class Node<K,V> implements Map.Entry<K,V> { 6 final int hash; //保存節點的hash值
7 final K key; //保存節點的key值
8 V value; //保存節點的value值 9 //next是指向鏈表結構下當前節點的next節點,紅黑樹TreeNode節點中也用到next
10 Node<K,V> next; 11
12 Node(int hash, K key, V value, Node<K,V> next) { 13 this.hash = hash; 14 this.key = key; 15 this.value = value; 16 this.next = next; 17 } 18
19 public final K getKey() { return key; } 20 public final V getValue() { return value; } 21 public final String toString() { return key + "=" + value; } 22
23 public final int hashCode() { 24 return Objects.hashCode(key) ^ Objects.hashCode(value); 25 } 26
27 public final V setValue(V newValue) { 28 V oldValue = value; 29 value = newValue; 30 return oldValue; 31 } 32
33 public final boolean equals(Object o) { 34 if (o == this) 35 return true; 36 if (o instanceof Map.Entry) { 37 Map.Entry<?,?> e = (Map.Entry<?,?>)o; 38 if (Objects.equals(key, e.getKey()) &&
39 Objects.equals(value, e.getValue())) 40 return true; 41 } 42 return false; 43 } 44 }
TreeNode<K,V>用來實現紅黑樹相關的存儲結構:
1 /** 繼承LinkedHashMap.Entry<K,V>,紅黑樹相關存儲結構
2 * Entry for Tree bins. Extends LinkedHashMap.Entry (which in turn 3 * extends Node) so can be used as extension of either regular or 4 * linked node. 5 */
6 static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> { 7 TreeNode<K,V> parent; //存儲當前節點的父節點
8 TreeNode<K,V> left; //存儲當前節點的左孩子
9 TreeNode<K,V> right; //存儲當前節點的右孩子
10 TreeNode<K,V> prev; //存儲當前節點的前一個節點
11 boolean red; //存儲當前節點的顏色(紅、黑)
12 TreeNode(int hash, K key, V val, Node<K,V> next) { 13 super(hash, key, val, next); 14 } 15
16 public class LinkedHashMap<K,V>
17 extends HashMap<K,V>
18 implements Map<K,V>
19 { 20
21 /**
22 * HashMap.Node subclass for normal LinkedHashMap entries. 23 */
24 static class Entry<K,V> extends HashMap.Node<K,V> { 25 Entry<K,V> before, after; 26 Entry(int hash, K key, V value, Node<K,V> next) { 27 super(hash, key, value, next); 28 } 29 }
3、HashMap各常量及成員變量的做用
HashMap相關常量:
1 /** 建立HashMap時未指定初始容量狀況下的默認容量 2 * The default initial capacity - MUST be a power of two. 3 */
4 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 1 << 4 = 16
5
6 /** HashMap的最大容量 7 * The maximum capacity, used if a higher value is implicitly specified 8 * by either of the constructors with arguments. 9 * MUST be a power of two <= 1<<30. 10 */
11 static final int MAXIMUM_CAPACITY = 1 << 30; // 1 << 30 = 1073741824 12
13 /** HashMap默認的裝載因子,當HashMap中元素數量超過 容量*裝載因子 時,則進行resize()擴容操做 14 * The load factor used when none specified in constructor. 15 */
16 static final float DEFAULT_LOAD_FACTOR = 0.75f; 17
18 /** 用來肯定什麼時候解決hash衝突的,鏈表轉爲紅黑樹 19 * The bin count threshold for using a tree rather than list for a 20 * bin. Bins are converted to trees when adding an element to a 21 * bin with at least this many nodes. The value must be greater 22 * than 2 and should be at least 8 to mesh with assumptions in 23 * tree removal about conversion back to plain bins upon 24 * shrinkage. 25 */
26 static final int TREEIFY_THRESHOLD = 8; 27
28 /** 用來肯定什麼時候解決hash衝突的,紅黑樹轉變爲鏈表 29 * The bin count threshold for untreeifying a (split) bin during a 30 * resize operation. Should be less than TREEIFY_THRESHOLD, and at 31 * most 6 to mesh with shrinkage detection under removal. 32 */
33 static final int UNTREEIFY_THRESHOLD = 6; 34
35 /** 當想要將解決hash衝突的鏈表轉變爲紅黑樹時,須要判斷下此時數組的容量,如果因爲數組容量過小(小於MIN_TREEIFY_CAPACITY)而致使hash衝突,則不進行鏈表轉爲紅黑樹的操做,而是利用resize()函數對HashMap擴容 36 * The smallest table capacity for which bins may be treeified. 37 * (Otherwise the table is resized if too many nodes in a bin.) 38 * Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts 39 * between resizing and treeification thresholds. 40 */
41 static final int MIN_TREEIFY_CAPACITY = 64;
HashMap相關成員變量:
1 /* ---------------- Fields -------------- */
2
3 /** 保存Node<K,V>節點的數組 4 * The table, initialized on first use, and resized as 5 * necessary. When allocated, length is always a power of two. 6 * (We also tolerate length zero in some operations to allow 7 * bootstrapping mechanics that are currently not needed.) 8 */
9 transient Node<K,V>[] table; 10
11 /** 由HashMap中Node<K,V>節點構成的set 12 * Holds cached entrySet(). Note that AbstractMap fields are used 13 * for keySet() and values(). 14 */
15 transient Set<Map.Entry<K,V>> entrySet; 16
17 /** 記錄HashMap當前存儲的元素的數量 18 * The number of key-value mappings contained in this map. 19 */
20 transient int size; 21
22 /** 記錄HashMap發生結構性變化的次數(value值的覆蓋不屬於結構性變化) 23 * The number of times this HashMap has been structurally modified 24 * Structural modifications are those that change the number of mappings in 25 * the HashMap or otherwise modify its internal structure (e.g., 26 * rehash). This field is used to make iterators on Collection-views of 27 * the HashMap fail-fast. (See ConcurrentModificationException). 28 */
29 transient int modCount; 30
31 /** threshold的值應等於table.length*loadFactor,size超過這個值時會進行resize()擴容 32 * The next size value at which to resize (capacity * load factor). 33 * 34 * @serial
35 */
36 // (The javadoc description is true upon serialization. 37 // Additionally, if the table array has not been allocated, this 38 // field holds the initial array capacity, or zero signifying 39 // DEFAULT_INITIAL_CAPACITY.)
40 int threshold; 41
42 /** 記錄HashMap的裝載因子 43 * The load factor for the hash table. 44 * 45 * @serial
46 */
47 final float loadFactor; 48
49 /* ---------------- Public operations -------------- */
4、HashMap的四種構造方法
HashMap提供了四個構造方法,四個構造方法中方法一、二、3都沒有進行數組的初始化操做,即便調用了構造方法此時存放HaspMap的數組中元素的table表長度依舊爲0 ;在第四個構造方法中調用了putMapEntries()方法完成了table的初始化操做,並將m中的元素添加到HashMap中。
HashMap四個構造方法:
1 /* ---------------- Public operations -------------- */
2
3 /** 構造方法1,指定初始容量及裝載因子 4 * Constructs an empty <tt>HashMap</tt> with the specified initial 5 * capacity and load factor. 6 * 7 * @param initialCapacity the initial capacity 8 * @param loadFactor the load factor 9 * @throws IllegalArgumentException if the initial capacity is negative 10 * or the load factor is nonpositive 11 */
12 public HashMap(int initialCapacity, float loadFactor) { 13 if (initialCapacity < 0) 14 throw new IllegalArgumentException("Illegal initial capacity: " +
15 initialCapacity); 16 if (initialCapacity > MAXIMUM_CAPACITY) 17 initialCapacity = MAXIMUM_CAPACITY; 18 if (loadFactor <= 0 || Float.isNaN(loadFactor)) 19 throw new IllegalArgumentException("Illegal load factor: " +
20 loadFactor); 21 this.loadFactor = loadFactor; 22 //tableSize(initialCapacity)方法返回的值最接近initialCapacity的2的冪,若設定初始容量爲9,則HashMap的實際容量爲16 23 //另外,經過HashMap(int initialCapacity, float loadFactor)該方法建立的HashMap初始容量的值存在threshold中
24 this.threshold = tableSizeFor(initialCapacity); 25 } 26
27
28 /** tableSizeFor(initialCapacity)方法返回的值是最接近initialCapacity的2的冪次方 29 * Returns a power of two size for the given target capacity. 30 */
31 static final int tableSizeFor(int cap) { 32 int n = cap - 1; 33 n |= n >>> 1; 34 n |= n >>> 2; 35 n |= n >>> 4; 36 n |= n >>> 8; 37 n |= n >>> 16; 38 return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; 39 } 40
41 /** 構造方法2,僅指定初始容量,裝載因子的值採用默認的0.75 42 * Constructs an empty <tt>HashMap</tt> with the specified initial 43 * capacity and the default load factor (0.75). 44 * 45 * @param initialCapacity the initial capacity. 46 * @throws IllegalArgumentException if the initial capacity is negative. 47 */
48 public HashMap(int initialCapacity) { 49 this(initialCapacity, DEFAULT_LOAD_FACTOR); 50 } 51
52 /** 構造方法3,全部參數均採用默認值 53 * Constructs an empty <tt>HashMap</tt> with the default initial capacity 54 * (16) and the default load factor (0.75). 55 */
56 public HashMap() { 57 this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
58 } 59
60 /** 構造方法4,指定集合轉爲HashMap 61 * Constructs a new <tt>HashMap</tt> with the same mappings as the 62 * specified <tt>Map</tt>. The <tt>HashMap</tt> is created with 63 * default load factor (0.75) and an initial capacity sufficient to 64 * hold the mappings in the specified <tt>Map</tt>. 65 * 66 * @param m the map whose mappings are to be placed in this map 67 * @throws NullPointerException if the specified map is null 68 */
69 public HashMap(Map<? extends K, ? extends V> m) { 70 this.loadFactor = DEFAULT_LOAD_FACTOR; 71 putMapEntries(m, false); 72 } 73
74 /** 把Map<? extends K, ? extends V> m中的元素插入HashMap 75 * Implements Map.putAll and Map constructor 76 * 77 * @param m the map 78 * @param evict false when initially constructing this map, else 79 * true (relayed to method afterNodeInsertion). 80 */
81 final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) { 82 int s = m.size(); 83 if (s > 0) { 84 //在建立HashMap時調用putMapEntries()函數,則table必定爲空
85 if (table == null) { // pre-size 86 //根據待插入map的size計算出要建立的HashMap的容量
87 float ft = ((float)s / loadFactor) + 1.0F; 88 int t = ((ft < (float)MAXIMUM_CAPACITY) ?
89 (int)ft : MAXIMUM_CAPACITY); 90 //把要建立的HashMap的容量存在threshold中
91 if (t > threshold) 92 threshold = tableSizeFor(t); 93 } 94 //若是待插入map的size大於threshold,則進行resize()
95 else if (s > threshold) 96 resize(); 97 for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) { 98 K key = e.getKey(); 99 V value = e.getValue(); 100 //最終實際上一樣也是調用了putVal()函數進行元素的插入
101 putVal(hash(key), key, value, false, evict); 102 } 103 } 104 }
5、HashMap的put方法
假如調用hashMap.put("apple",0)方法,將會在HashMap的table數組中插入一個Key爲「apple」的元素;這時須要經過
hash()
函數來肯定該Entry的具體插入位置,而hash()方法內部會調用hashCode()函數獲得「apple」的hashCode;而後putVal()方法通過必定計算獲得最終的插入位置index,最後將這個Entry插入到table的index位置。
put函數:
1 /** 指定key和value,向HashMap中插入節點 2 * Associates the specified value with the specified key in this map. 3 * If the map previously contained a mapping for the key, the old 4 * value is replaced. 5 * 6 * @param key key with which the specified value is to be associated 7 * @param value value to be associated with the specified key 8 * @return the previous value associated with <tt>key</tt>, or 9 * <tt>null</tt> if there was no mapping for <tt>key</tt>. 10 * (A <tt>null</tt> return can also indicate that the map 11 * previously associated <tt>null</tt> with <tt>key</tt>.) 12 */
13 public V put(K key, V value) { 14 //插入節點,hash值的計算調用hash(key)函數,實際調用putVal()插入節點
15 return putVal(hash(key), key, value, false, true); 16 } 17
18 /** key的hash值計算是經過hashCode()的高16位異或低16位實現的:h = key.hashCode()) ^ (h >>> 16),使用位運算替代了取模運算,在table的長度比較小的狀況下,也能保證hashcode的高位參與到地址映射的計算當中,同時不會有太大的開銷。 19 * Computes key.hashCode() and spreads (XORs) higher bits of hash 20 * to lower. Because the table uses power-of-two masking, sets of 21 * hashes that vary only in bits above the current mask will 22 * always collide. (Among known examples are sets of Float keys 23 * holding consecutive whole numbers in small tables.) So we 24 * apply a transform that spreads the impact of higher bits 25 * downward. There is a tradeoff between speed, utility, and 26 * quality of bit-spreading. Because many common sets of hashes 27 * are already reasonably distributed (so don't benefit from 28 * spreading), and because we use trees to handle large sets of 29 * collisions in bins, we just XOR some shifted bits in the 30 * cheapest possible way to reduce systematic lossage, as well as 31 * to incorporate impact of the highest bits that would otherwise 32 * never be used in index calculations because of table bounds. 33 */
34 static final int hash(Object key) { 35 int h; 36 return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); 37 }
putVal()函數:
1 /** 實際將元素插入HashMap中的方法 2 * Implements Map.put and related methods 3 * 4 * @param hash hash for key 5 * @param key the key 6 * @param value the value to put 7 * @param onlyIfAbsent if true, don't change existing value 8 * @param evict if false, the table is in creation mode. 9 * @return previous value, or null if none 10 */
11 final V putVal(int hash, K key, V value, boolean onlyIfAbsent, 12 boolean evict) { 13 Node<K,V>[] tab; Node<K,V> p; int n, i; 14 //判斷table是否已初始化,不然進行初始化table操做
15 if ((tab = table) == null || (n = tab.length) == 0) 16 n = (tab = resize()).length; 17 //根據hash值肯定節點在數組中的插入的位置,即計算索引存儲的位置,若該位置無元素則直接進行插入
18 if ((p = tab[i = (n - 1) & hash]) == null) 19 tab[i] = newNode(hash, key, value, null); 20 else { 21 //節點若已經存在元素,即待插入位置存在元素
22 Node<K,V> e; K k; 23 //對比已經存在的元素與待插入元素的hash值和key值,執行賦值操做
24 if (p.hash == hash &&
25 ((k = p.key) == key || (key != null && key.equals(k)))) 26 e = p; 27 //判斷該元素是否爲紅黑樹節點
28 else if (p instanceof TreeNode) 29 //紅黑樹節點則調用putTreeVal()函數進行插入
30 e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); 31 else { 32 //若該元素是鏈表,且爲鏈表頭節點,則今後節點開始向後尋找合適的插入位置
33 for (int binCount = 0; ; ++binCount) { 34 if ((e = p.next) == null) { 35 //找到插入位置後,新建節點插入
36 p.next = newNode(hash, key, value, null); 37 //若鏈表上節點超過TREEIFY_THRESHOLD - 1,即鏈表長度爲8,將鏈表轉變爲紅黑樹
38 if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
39 treeifyBin(tab, hash); 40 break; 41 } 42 //若待插入元素在HashMap中已存在,key存在了則直接覆蓋
43 if (e.hash == hash &&
44 ((k = e.key) == key || (key != null && key.equals(k)))) 45 break; 46 p = e; 47 } 48 } 49 if (e != null) { // existing mapping for key
50 V oldValue = e.value; 51 if (!onlyIfAbsent || oldValue == null) 52 e.value = value; 53 afterNodeAccess(e); 54 //若存在key節點,則返回舊的key值
55 return oldValue; 56 } 57 } 58 //記錄修改次數
59 ++modCount; 60 //判斷是否須要擴容
61 if (++size > threshold) 62 resize(); 63 //空操做
64 afterNodeInsertion(evict); 65 //若不存在key節點,則返回null
66 return null; 67 }
鏈表轉紅黑樹的putTreeVal()函數:
1 /** 鏈表轉紅黑樹
2 * Tree version of putVal. 3 */
4 final TreeNode<K,V> putTreeVal(HashMap<K,V> map, Node<K,V>[] tab, 5 int h, K k, V v) { 6 Class<?> kc = null; 7 boolean searched = false; 8 TreeNode<K,V> root = (parent != null) ? root() : this; 9 //從根節點開始查找合適的插入位置
10 for (TreeNode<K,V> p = root;;) { 11 int dir, ph; K pk; 12 if ((ph = p.hash) > h) 13 //若dir<0,則查找當前節點的左孩子
14 dir = -1; 15 else if (ph < h) 16 //若dir>0,則查找當前節點的右孩子
17 dir = 1; 18 //hash值或是key值相同
19 else if ((pk = p.key) == k || (k != null && k.equals(pk))) 20 return p; 21 //1.當前節點與待插入節點key不一樣,hash值相同 22 //2.k是不可比較的,即k未實現comparable<K>接口,或者compareComparables(kc,k,pk)的返回值爲0
23 else if ((kc == null &&
24 (kc = comparableClassFor(k)) == null) ||
25 (dir = compareComparables(kc, k, pk)) == 0) { 26 //在以當前節點爲根節點的整個樹上搜索是否存在待插入節點(只搜索一次)
27 if (!searched) { 28 TreeNode<K,V> q, ch; 29 searched = true; 30 if (((ch = p.left) != null &&
31 (q = ch.find(h, k, kc)) != null) ||
32 ((ch = p.right) != null &&
33 (q = ch.find(h, k, kc)) != null)) 34 //若搜索發現樹中存在待插入節點,則直接返回
35 return q; 36 } 37 //指定了一個k的比較方式 tieBreakOrder
38 dir = tieBreakOrder(k, pk); 39 } 40
41 TreeNode<K,V> xp = p; 42 if ((p = (dir <= 0) ? p.left : p.right) == null) { 43 //找到了待插入位置,xp爲待插入位置的父節點,TreeNode節點中既存在樹狀關係,又存在鏈式關係,並且仍是雙端鏈表
44 Node<K,V> xpn = xp.next; 45 TreeNode<K,V> x = map.newTreeNode(h, k, v, xpn); 46 if (dir <= 0) 47 xp.left = x; 48 else
49 xp.right = x; 50 xp.next = x; 51 x.parent = x.prev = xp; 52 if (xpn != null) 53 ((TreeNode<K,V>)xpn).prev = x; 54 //插入節點後進行二叉樹平衡操做
55 moveRootToFront(tab, balanceInsertion(root, x)); 56 return null; 57 } 58 } 59 } 60
61 /** 定義了一個k的比較方法 62 * Tie-breaking utility for ordering insertions when equal 63 * hashCodes and non-comparable. We don't require a total 64 * order, just a consistent insertion rule to maintain 65 * equivalence across rebalancings. Tie-breaking further than 66 * necessary simplifies testing a bit. 67 */
68 static int tieBreakOrder(Object a, Object b) { 69 int d; 70 if (a == null || b == null ||
71 (d = a.getClass().getName(). 72 compareTo(b.getClass().getName())) == 0) 73 //System.identityHashCode()實際是比較對象a,b的內存地址
74 d = (System.identityHashCode(a) <= System.identityHashCode(b) ?
75 -1 : 1); 76 return d; 77 }
圖9. hashCode計算獲得table索引的過程
圖10. put添加方法執行過程
上圖的HashMap的put方法執行流程圖,能夠總結爲以下主要步驟:
1. 判斷數組table是否爲null,若爲null則執行resize()擴容操做。
2. 根據鍵key的值計算hash值獲得插入的數組索引i,若table[i] == nulll,則直接新建節點插入,進入步驟6;若table[i]非null,則繼續執行下一步。
3. 判斷table[i]的首個元素key是否和當前key相同(hashCode和equals均相同),若相同則直接覆蓋value,進入步驟6,反之繼續執行下一步。
4. 判斷table[i]是否爲treeNode,如果紅黑樹,則直接在樹中插入鍵值對並進入步驟6,反之繼續執行下一步。
5. 遍歷table[i],判斷鏈表長度是否大於8,若>8,則把鏈表轉換爲紅黑樹,在紅黑樹中執行插入操做;若<8,則進行鏈表的插入操做;遍歷過程當中若發現key已存在則會直接覆蓋該key的value值。
6. 插入成功後,判斷實際存在的鍵值對數量size是否超過了最大容量threshold,若超過則進行擴容。
6、HashMap的get方法
get()和getNode()函數:
1 /**
2 * Returns the value to which the specified key is mapped, 3 * or {@code null} if this map contains no mapping for the key. 4 * 5 * <p>More formally, if this map contains a mapping from a key 6 * {@code k} to a value {@code v} such that {@code (key==null ? k==null : 7 * key.equals(k))}, then this method returns {@code v}; otherwise 8 * it returns {@code null}. (There can be at most one such mapping.) 9 * 10 * <p>A return value of {@code null} does not <i>necessarily</i> 11 * indicate that the map contains no mapping for the key; it's also 12 * possible that the map explicitly maps the key to {@code null}. 13 * The {@link #containsKey containsKey} operation may be used to 14 * distinguish these two cases. 15 * 16 * @see #put(Object, Object) 17 */
18 public V get(Object key) { 19 Node<K,V> e; 20 //其實是根據輸入節點的hash值和key值,利用getNode方法進行查找
21 return (e = getNode(hash(key), key)) == null ? null : e.value; 22 } 23
24 /**
25 * Implements Map.get and related methods 26 * 27 * @param hash hash for key 28 * @param key the key 29 * @return the node, or null if none 30 */
31 final Node<K,V> getNode(int hash, Object key) { 32 Node<K,V>[] tab; Node<K,V> first, e; int n; K k; 33 if ((tab = table) != null && (n = tab.length) > 0 &&
34 (first = tab[(n - 1) & hash]) != null) { 35 if (first.hash == hash && // always check first node
36 ((k = first.key) == key || (key != null && key.equals(k)))) 37 return first; 38 if ((e = first.next) != null) { 39 if (first instanceof TreeNode) 40 //若定位到的節點是TreeNode節點,則在樹中進行查找
41 return ((TreeNode<K,V>)first).getTreeNode(hash, key); 42 do { 43 //反之,在鏈表中查找
44 if (e.hash == hash &&
45 ((k = e.key) == key || (key != null && key.equals(k)))) 46 return e; 47 } while ((e = e.next) != null); 48 } 49 } 50 return null; 51 }
getTreeNode()和find()函數:
1 /** 從根節點開始,調用find()方法進行查找 2 * Calls find for root node. 3 */
4 final TreeNode<K,V> getTreeNode(int h, Object k) { 5 return ((parent != null) ? root() : this).find(h, k, null); 6 } 7
8 /**
9 * Finds the node starting at root p with the given hash and key. 10 * The kc argument caches comparableClassFor(key) upon first use 11 * comparing keys. 12 */
13 final TreeNode<K,V> find(int h, Object k, Class<?> kc) { 14 TreeNode<K,V> p = this; 15 do { 16 int ph, dir; K pk; 17 TreeNode<K,V> pl = p.left, pr = p.right, q; 18 //首先進行hash值的比較,若不一樣則令當前節點變爲它的左孩子or右孩子
19 if ((ph = p.hash) > h) 20 p = pl; 21 else if (ph < h) 22 p = pr; 23 //若hash值相同,進行key值的比較
24 else if ((pk = p.key) == k || (k != null && k.equals(pk))) 25 return p; 26 else if (pl == null) 27 p = pr; 28 else if (pr == null) 29 p = pl; 30 //執行到這裏,說明了hash值是相同的,key值不一樣 31 //若k是可比較的而且k.compareTo(pk)的返回結果不爲0,則進入下面的else if
32 else if ((kc != null ||
33 (kc = comparableClassFor(k)) != null) &&
34 (dir = compareComparables(kc, k, pk)) != 0) 35 p = (dir < 0) ? pl : pr; 36 //若k是不可比較的,或者k.compareTo(pk)返回結果爲0,則在整棵樹中查找,先找右子樹,沒找到則再到左子樹找
37 else if ((q = pr.find(h, k, kc)) != null) 38 return q; 39 else
40 p = pl; 41 } while (p != null); 42 return null; 43 }
圖11. get方法執行流程
上圖爲HashMap get方法執行流程圖,HashMap的查找操做相對簡單,能夠總結爲以下主要步驟:
1. 首先定位到鍵所在的數組的下標,並獲取對應節點n。
2. 判斷n是否爲null,若n爲null,則返回null並結束;反之,繼續下一步。
3. 判斷n的key和要查找的key是否相同(key相同指的是hashCode和equals均相同),若相同則返回n並結束;反之,繼續下一步。
4. 判斷是否有後續節點m,若沒有則結束;反之,繼續下一步。
5. 判斷m是否爲紅黑樹,若爲紅黑樹則遍歷紅黑樹,在遍歷過程當中若是存在某一個節點的key與要找的key相同,則返回該節點;反之,返回null;若非紅黑樹則繼續下一步。
6. 遍歷鏈表,若存在某一個節點的key與要找的key相同,則返回該節點;反之,返回null。
7、HashMap的remove方法
HashMap根據鍵值刪除指定節點,其刪除操做實際上是一個「查找+刪除」的過程,核心的方法是removeNode。
remove和removeNode()函數:
1 /**
2 * Removes the mapping for the specified key from this map if present. 3 * 4 * @param key key whose mapping is to be removed from the map 5 * @return the previous value associated with <tt>key</tt>, or 6 * <tt>null</tt> if there was no mapping for <tt>key</tt>. 7 * (A <tt>null</tt> return can also indicate that the map 8 * previously associated <tt>null</tt> with <tt>key</tt>.) 9 */
10 public V remove(Object key) { 11 Node<K,V> e; 12 //計算出hash值,調用removeNode()方法根據鍵值刪除指定節點
13 return (e = removeNode(hash(key), key, null, false, true)) == null ?
14 null : e.value; 15 } 16
17 /**
18 * Implements Map.remove and related methods 19 * 20 * @param hash hash for key 21 * @param key the key 22 * @param value the value to match if matchValue, else ignored 23 * @param matchValue if true only remove if value is equal 24 * @param movable if false do not move other nodes while removing 25 * @return the node, or null if none 26 */
27 final Node<K,V> removeNode(int hash, Object key, Object value, 28 boolean matchValue, boolean movable) { 29 Node<K,V>[] tab; Node<K,V> p; int n, index; 30 //判斷表是否爲空,以及p節點根據鍵的hash值對應到數組的索引初是否有節點 31 //刪除操做須要保證在表不爲空的狀況下進行,而且p節點根據鍵的hash值對應到數組的索引在該索引下必需要有節點;若爲null,則說明此鍵所對應的節點不存在HashMap中
32 if ((tab = table) != null && (n = tab.length) > 0 &&
33 (p = tab[index = (n - 1) & hash]) != null) { 34 Node<K,V> node = null, e; K k; V v; 35 //如果須要刪除的節點就是該頭節點,則讓node引用指向它;不然什麼待刪除的結點在當前p所指向的頭節點的鏈表或紅黑樹中,則須要遍歷查找
36 if (p.hash == hash &&
37 ((k = p.key) == key || (key != null && key.equals(k)))) 38 node = p; 39 else if ((e = p.next) != null) { 40 //若頭節點是紅黑樹節點,則調用紅黑樹自己的遍歷方法getTreeNode,獲取待刪除的結點
41 if (p instanceof TreeNode) 42 node = ((TreeNode<K,V>)p).getTreeNode(hash, key); 43 else { 44 //不然就是普通鏈表,則使用do while循環遍歷查找待刪除結點
45 do { 46 if (e.hash == hash &&
47 ((k = e.key) == key ||
48 (key != null && key.equals(k)))) { 49 node = e; 50 break; 51 } 52 p = e; 53 } while ((e = e.next) != null); 54 } 55 } 56 if (node != null && (!matchValue || (v = node.value) == value ||
57 (value != null && value.equals(v)))) { 58 //如果紅黑樹結點的刪除,則直接調用紅黑樹的removeTreeNode方法進行刪除
59 if (node instanceof TreeNode) 60 ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable); 61 //若待刪除結點是一個頭節點,則用它的next節點頂替它做爲頭節點存放在table[index]中,以此達到刪除的目的
62 else if (node == p) 63 tab[index] = node.next; 64 //若待刪除結點爲普通鏈表中的一個結點,則用該節點的前一個節點直接跳過該待刪除節點,指向它的next結點(鏈表經過next獲取下一個結點信息)
65 else
66 p.next = node.next; 67 //記錄修改次數
68 ++modCount; 69 --size; 70 afterNodeRemoval(node); 71 //若removeNode方法刪除成功則返回被刪除的結點
72 return node; 73 } 74 } 75 //若沒有刪除成功則返回null
76 return null; 77 }
8、HashMap的擴容機制
擴容是爲了防止HashMap中的元素個數超過了閥值,從而影響性能所服務的。而數組是沒法自動擴容的,HashMap的擴容是申請一個容量爲原數組大小兩倍的新數組,而後遍歷舊數組,從新計算每一個元素的索引位置,並複製到新數組中;又由於HashMap的哈希桶數組大小老是爲2的冪次方,So從新計算後的索引位置要麼在原來位置不變,要麼就是「原位置+舊數組長度」。
其中,threshold和loadFactor兩個屬性決定着是否擴容。threshold=Length*loadFactor,Length表示table數組的長度(默認值爲16),loadFactor爲負載因子(默認值爲0.75);閥值threshold表示當table數組中存儲的元素個數超過該閥值時,即須要擴容;如數組默認長度爲16,負載因子默認0.75,此時threshold=16*0.75=12,即當table數組中存儲的元素個數超過12個時,table數組就該進行擴容了。
HashMap的擴容使用新的數組代替舊數組,而後將舊數組中的元素從新計算索引位置並放到新數組中,對舊數組中的元素如何從新映射到新數組中?因爲HashMap擴容時使用的是2的冪次方擴展的,即數組長度擴大爲原來的2倍、4倍、8倍、16倍...,所以在擴容時(Length-1)這部分就至關於在高位新增一個或多個1位(bit);以下圖12,HashMap擴大爲原數組的兩倍爲例。
圖12. HashMap的哈希算法數組擴容
如上圖12所示,(a)爲擴容前,key1和key2兩個key肯定索引的位置;(b)爲擴容後,key1和key2兩個key肯定索引的位置;hash1和hash2分別是key1與key2對應的哈希「與高位運算」結果。
(a)中數組的高位bit爲「1111」,1*20 + 1*21 + 1*22 + 1*23 = 15,而 n-1 =15,因此擴容前table的長度n爲16;
(b)中n擴大爲原來的兩倍,其數組大小的高位bit爲「1 1111」,1*20 + 1*21 + 1*22 + 1*23 + 1*24 = 15+16=31,而 n-1=31,因此擴容後table的長度n爲32;
(a)中的n爲16,(b)中擴大兩倍n爲32,至關於(n-1)這部分的高位多了一個1,而後和原hash碼做與操做,最後元素在新數組中映射的位置要麼不變,要麼向後移動16個位置,以下圖13所示。
圖13. HashMap中數組擴容兩倍後位置的變化
KEY | hash | 原數組高位bit | 原下標 | 新數組高位bit | 新下標 |
key1 | 0 0101 | 1111 | 0 0101 | 1 1111 | 0 0101 = 1*20+0*21+1*22+0*23+0*24= 5 |
key2 | 1 0101 | 1111 | 0 0101 | 1 1111 | 1 0101 = 1*20 + 0*21 + 1*22 + 0*23+0*24= 5+16 |
所以,咱們在擴充HashMap,複製數組元素及肯定索引位置時不須要從新計算hash值,只須要判斷原來的hash值新增的那個bit是1,仍是0;若爲0,則索引未改變;若爲1,則索引變爲「原索引+oldCap」;如圖14,HashMap中數組從16擴容爲32的resize圖。
圖14. HashMap中數組16擴容至32
這樣設計有以下幾點好處:
1. 省去了從新計算hash值的時間(因爲位運算直接對內存數據進行操做,不須要轉成十進制,所以處理速度很是快),只需判斷新增的一位是0或1;
2. 因爲新增的1位能夠認爲是隨機的0或1,所以擴容過程當中會均勻的把以前有衝突的節點分散到新的位置(bucket槽),而且位置的前後順序不會顛倒;
3. JDK1.7中擴容時,舊鏈表遷移到新鏈表的時候,若出如今新鏈表的數組索引位置相同狀況,則鏈表元素會倒置,但從圖14中看出JKD1.8的擴容並不會顛倒相同索引的鏈表元素。
HashMap擴容resize函數:
1 /**
2 * Initializes or doubles table size. If null, allocates in 3 * accord with initial capacity target held in field threshold. 4 * Otherwise, because we are using power-of-two expansion, the 5 * elements from each bin must either stay at same index, or move 6 * with a power of two offset in the new table. 7 * 8 * @return the table 9 */
10 final Node<K,V>[] resize() { 11 Node<K,V>[] oldTab = table; 12 int oldCap = (oldTab == null) ? 0 : oldTab.length; 13 int oldThr = threshold; 14 int newCap, newThr = 0; 15 //當哈希桶不爲空時,擴容走該支路A
16 if (oldCap > 0) { 17 //若容量超過最大值,則沒法進行擴容,需擴大閥值
18 if (oldCap >= MAXIMUM_CAPACITY) { 19 threshold = Integer.MAX_VALUE; 20 return oldTab; 21 } 22 //若哈希桶擴容爲原來的2倍,閥值也變爲原來的兩倍
23 else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
24 oldCap >= DEFAULT_INITIAL_CAPACITY) 25 newThr = oldThr << 1; // double threshold
26 } 27 //當調用非空函數時,走此分支B
28 else if (oldThr > 0) // initial capacity was placed in threshold
29 newCap = oldThr; 30 //調用空的構造函數時走此分支C,使用默認大小和閥值初始化哈希桶
31 else { // zero initial threshold signifies using defaults
32 newCap = DEFAULT_INITIAL_CAPACITY; 33 newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); 34 } 35 //int newCap, newThr = 0; 當走分支B時 newThr 爲0
36 if (newThr == 0) { 37 float ft = (float)newCap * loadFactor; 38 //走分支B調用的是非空函數,直接把容量大小賦值給閥值,須要計算新的閥值threshold
39 newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
40 (int)ft : Integer.MAX_VALUE); 41 } 42 threshold = newThr; 43 @SuppressWarnings({"rawtypes","unchecked"}) 44 //new一個新的哈希桶
45 Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap]; 46 table = newTab; 47 //擴容分支
48 if (oldTab != null) { 49 //for循環把oldTab中的每一個節點node,reHash操做並移動到新的數組newTab中
50 for (int j = 0; j < oldCap; ++j) { 51 Node<K,V> e; 52 if ((e = oldTab[j]) != null) { 53 oldTab[j] = null; 54 //e.next == null,如果單個節點,即沒有後繼next節點,則直接在newTab在進行重定位
55 if (e.next == null) 56 newTab[e.hash & (newCap - 1)] = e; 57 //若節點爲TreeNode,則須要進行紅黑樹的rehash操做
58 else if (e instanceof TreeNode) 59 ((TreeNode<K,V>)e).split(this, newTab, j, oldCap); 60 //else則節點爲鏈表,需進行鏈表的rehash操做,鏈表重組並保持原有順序
61 else { // preserve order
62 Node<K,V> loHead = null, loTail = null; 63 Node<K,V> hiHead = null, hiTail = null; 64 Node<K,V> next; 65 do { 66 next = e.next; 67 //經過與位運算&,判斷rehash後節點位置是否發生改變 68 //(e.hash & oldCap) == 0,則爲原位置
69 if ((e.hash & oldCap) == 0) { 70 if (loTail == null) 71 //loHead 指向新的 hash 在原位置的頭節點
72 loHead = e; 73 else
74 //loTail 指向新的 hash 在原位置的尾節點
75 loTail.next = e; 76 loTail = e; 77 } 78 //else則rehash後節點位置變爲:原位置+oldCap位置
79 else { 80 if (hiTail == null) 81 //hiHead 指向新的 hash 在原位置 + oldCap 位置的頭節點
82 hiHead = e; 83 else
84 // hiTail 指向新的 hash 在原位置 + oldCap 位置的尾節點
85 hiTail.next = e; 86 hiTail = e; 87 } 88 } while ((e = next) != null); 89 //loTail非null,新的hash在原位置的頭節點放入哈希桶
90 if (loTail != null) { 91 loTail.next = null; 92 newTab[j] = loHead; 93 } 94 //hiTail非null,新的hash在 原位置+oldCap位置 的頭節點放入哈希桶
95 if (hiTail != null) { 96 hiTail.next = null; 97 // rehash 後節點新的位置必定爲原位置加上 oldCap
98 newTab[j + oldCap] = hiHead; 99 } 100 } 101 } 102 } 103 } 104 return newTab; 105 }
HashMap對紅黑樹進行rehash操做的split函數:
1 /**
2 * Splits nodes in a tree bin into lower and upper tree bins, 3 * or untreeifies if now too small. Called only from resize; 4 * see above discussion about split bits and indices. 5 * 6 * @param map the map 7 * @param tab the table for recording bin heads 8 * @param index the index of the table being split 9 * @param bit the bit of hash to split on 10 */
11 final void split(HashMap<K,V> map, Node<K,V>[] tab, int index, int bit) { 12 TreeNode<K,V> b = this; 13 /**
14 * loHead 指向新的 hash 在原位置的頭節點 15 * loTail 指向新的 hash 在原位置的尾節點 16 * hiHead 指向新的 hash 在原位置 + oldCap 位置的頭節點 17 * hiTail 指向新的 hash 在原位置 + oldCap 位置的尾節點 18 */
19 // Relink into lo and hi lists, preserving order
20 TreeNode<K,V> loHead = null, loTail = null; 21 TreeNode<K,V> hiHead = null, hiTail = null; 22 int lc = 0, hc = 0; 23 //因爲TreeNode節點之間存在着雙端鏈表的關係,可利用鏈表關係進行rehash
24 for (TreeNode<K,V> e = b, next; e != null; e = next) { 25 next = (TreeNode<K,V>)e.next; 26 e.next = null; 27 //原位置
28 if ((e.hash & bit) == 0) { 29 if ((e.prev = loTail) == null) 30 loHead = e; 31 else
32 loTail.next = e; 33 loTail = e; 34 ++lc; 35 } 36 //else則爲原位置 + oldCap
37 else { 38 if ((e.prev = hiTail) == null) 39 hiHead = e; 40 else
41 hiTail.next = e; 42 hiTail = e; 43 ++hc; 44 } 45 } 46 //rehash操做後,根據鏈表長度進行untreeify解除樹形化或treeify樹形化操做
47 if (loHead != null) { 48 //當鏈表的節點個數小於等於解除樹形化閥值UNTREEIFY_THRESHOLD時,將紅黑樹轉爲普通鏈表
49 if (lc <= UNTREEIFY_THRESHOLD) 50 tab[index] = loHead.untreeify(map); 51 else { 52 //新的hash在原位置的頭節點放入哈希桶
53 tab[index] = loHead; 54 if (hiHead != null) // (else is already treeified)
55 loHead.treeify(tab); 56 } 57 } 58 if (hiHead != null) { 59 //當鏈表的節點個數小於等於解除樹形化閥值UNTREEIFY_THRESHOLD時,將紅黑樹轉爲普通鏈表
60 if (hc <= UNTREEIFY_THRESHOLD) 61 tab[index + bit] = hiHead.untreeify(map); 62 else { 63 //新的hash在原位置 + oldCap位置的頭節點放入哈希桶
64 tab[index + bit] = hiHead; 65 if (loHead != null) 66 hiHead.treeify(tab); 67 } 68 } 69 }
9、總結
1. HashMap的哈希桶初始長度Length默認爲16,負載因子默loadFactor認值爲0.75,threshold閥值是HashMap能容納的最大數據量的Node節點個數,threshold=Length*loadFactor。
2. 當HashMap中存儲的元素個數超過了threshold閥值時,則會進行reseize擴容操做,擴容後的數組容量爲以前的兩倍;但擴容是個特別消耗性能的操做,So當咱們在使用HashMap的時候,能夠估算下Map的大小,在初始化時指定一個大體的數值,這樣能夠減小Map頻繁擴容的次數。
3. HashMap中實際存儲的鍵值對的數量經過size表示,table數組的長度爲Length。
4. modCount是用來記錄HashMap內部結構發生變化的次數,put方法覆蓋HashMap中的某個key對應的value不屬於結構變化。
5. HashMap哈希桶的大小必須爲2的冪次方。
6. JDK1.8引入紅黑樹操做,大幅度優化了HashMap的性能。
7. HashMap是非線程安全的,在併發環境中同時操做HashMap時最好使用線程安全的ConcurrentHashMap。
8. 由於我不知道下一生仍是否能碰見你 因此我此生纔會那麼努力把最好的給你。