日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

【Java深入研究】9、HashMap源码解析(jdk 1.8)

發(fā)布時(shí)間:2024/4/17 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Java深入研究】9、HashMap源码解析(jdk 1.8) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、HashMap概述

  HashMap是常用的Java集合之一,是基于哈希表的Map接口的實(shí)現(xiàn)。與HashTable主要區(qū)別為不支持同步和允許null作為key和value。由于HashMap不是線程安全的,如果想要線程安全,可以使用ConcurrentHashMap代替。

二、HashMap數(shù)據(jù)結(jié)構(gòu)

  HashMap的底層是哈希數(shù)組,數(shù)組元素為Entry。HashMap通過key的hashCode來計(jì)算hash值,當(dāng)hashCode相同時(shí),通過“拉鏈法”解決沖突

  相比于之前的版本,jdk1.8在解決哈希沖突時(shí)有了較大的變化,當(dāng)鏈表長度大于閾值(默認(rèn)為8)時(shí),將鏈表轉(zhuǎn)化為紅黑樹,以減少搜索時(shí)間。原本Map.Entry接口的實(shí)現(xiàn)類Entry改名為了Node。轉(zhuǎn)化為紅黑樹時(shí)改用另一種實(shí)現(xiàn)TreeNode。?

Node類

static class Node<K,V> implements Map.Entry<K,V> {final int hash; // 哈希值final K key;V value;Node<K,V> next; // 指向下一個(gè)節(jié)點(diǎn) 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;}}

TreeNode類

static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {TreeNode<K,V> parent; // red-black tree linksTreeNode<K,V> left;TreeNode<K,V> right;TreeNode<K,V> prev; // needed to unlink next upon deletionboolean red;TreeNode(int hash, K key, V val, Node<K,V> next) {super(hash, key, val, next);}}

HashMap就是這樣一個(gè)Entry(包括Node和TreeNode)數(shù)組,Node對象中包含鍵、值和hash值,next指向下一個(gè)Entry,用來處理哈希沖突。TreeNode對象包含指向父節(jié)點(diǎn)、子節(jié)點(diǎn)和前一個(gè)節(jié)點(diǎn)(移除對象時(shí)使用)的指針,以及表示紅黑節(jié)點(diǎn)的boolean標(biāo)識。

三、HashMap源碼分析

1. 主要屬性

transient Node<K,V>[] table; // 哈希數(shù)組transient Set<Map.Entry<K,V>> entrySet; // entry緩存Settransient int size; // 元素個(gè)數(shù)transient int modCount; // 修改次數(shù)int threshold; // 閾值,等于加載因子*容量,當(dāng)實(shí)際大小超過閾值則進(jìn)行擴(kuò)容final float loadFactor; // 加載因子,默認(rèn)值為0.75

2. 構(gòu)造方法

  以下是HashMap的幾個(gè)構(gòu)造方法。

/*** 根據(jù)初始化容量和加載因子構(gòu)建一個(gè)空的HashMap.*/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);}/*** 使用初始化容量和默認(rèn)加載因子(0.75).*/public HashMap(int initialCapacity) {this(initialCapacity, DEFAULT_LOAD_FACTOR);}/*** 使用默認(rèn)初始化大小(16)和默認(rèn)加載因子(0.75).*/public HashMap() {this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted }/*** 用已有的Map構(gòu)造一個(gè)新的HashMap.*/public HashMap(Map<? extends K, ? extends V> m) {this.loadFactor = DEFAULT_LOAD_FACTOR;putMapEntries(m, false);}

3. 數(shù)據(jù)存取

putAll方法

public void putAll(Map<? extends K, ? extends V> m) {putMapEntries(m, true);}/*** Implements Map.putAll and Map constructor** @param m the map* @param evict false when initially constructing this map, else* true (relayed to method afterNodeInsertion).*/final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {int s = m.size();if (s > 0) {if (table == null) { // pre-sizefloat 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)resize();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); // put核心方法 }}}

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) // table為空或length為0n = (tab = resize()).length; // 初始化if ((p = tab[i = (n - 1) & hash]) == null) // 如果hash所在位置為null,直接puttab[i] = newNode(hash, key, value, null);else { // tab[i]有元素,遍歷節(jié)點(diǎn)后添加Node<K,V> e; K k;// 如果hash、key都相等,直接覆蓋if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k))))e = p;else if (p instanceof TreeNode) // 紅黑樹添加節(jié)點(diǎn)e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);else { // 鏈表for (int binCount = 0; ; ++binCount) {if ((e = p.next) == null) { // 找到鏈表最后一個(gè)節(jié)點(diǎn),插入新節(jié)點(diǎn)p.next = newNode(hash, key, value, null);// 鏈表節(jié)點(diǎn)大于閾值8,調(diào)用treeifyBin方法,當(dāng)tab.length大于64將鏈表改為紅黑樹// 如果tab.length < 64或tab為null,則調(diào)用resize方法重構(gòu)鏈表.if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st treeifyBin(tab, hash);break;}// hash、key都相等,此時(shí)節(jié)點(diǎn)即要更新節(jié)點(diǎn)if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))break;p = e;}}// 當(dāng)前節(jié)點(diǎn)e = p.next不為null,表示鏈表中原本存在相同的key,則返回oldValueif (e != null) { // existing mapping for keyV oldValue = e.value;// onlyIfAbsent值為false,參數(shù)主要決定存在相同key時(shí)是否執(zhí)行替換if (!onlyIfAbsent || oldValue == null)e.value = value;afterNodeAccess(e);return oldValue;}}++modCount;if (++size > threshold) // 檢查是否超過閾值 resize();afterNodeInsertion(evict);return null; // 原HashMap中不存在相同的key,插入鍵值對后返回null} treeifyBin方法源碼如下: 1 final void treeifyBin(Node<K,V>[] tab, int hash) {2 int n, index; Node<K,V> e;3 //當(dāng)tab為null或tab.length<64需要進(jìn)行擴(kuò)容4 if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)5 resize();6 else if ((e = tab[index = (n - 1) & hash]) != null) {7 TreeNode<K,V> hd = null, tl = null;8 do {9 TreeNode<K,V> p = replacementTreeNode(e, null); 10 if (tl == null) 11 hd = p; 12 else { 13 p.prev = tl; 14 tl.next = p; 15 } 16 tl = p; 17 } while ((e = e.next) != null); 18 if ((tab[index] = hd) != null) 19 //存儲在紅黑樹 20 hd.treeify(tab); 21 } 22 }

?

現(xiàn)在我們來看一下為什么需要擴(kuò)容:

1 /**2 * The bin count threshold for using a tree rather than list for a3 * bin. Bins are converted to trees when adding an element to a4 * bin with at least this many nodes. The value must be greater5 * than 2 and should be at least 8 to mesh with assumptions in6 * tree removal about conversion back to plain bins upon7 * shrinkage.8 */9 static final int TREEIFY_THRESHOLD = 8; 10 11 /** 12 * The smallest table capacity for which bins may be treeified. 13 * (Otherwise the table is resized if too many nodes in a bin.) 14 * Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts 15 * between resizing and treeification thresholds. 16 */ 17 static final int MIN_TREEIFY_CAPACITY = 64;

?

MIN_TREEIFY_CAPACITY=64 > 4 * TREEIFY_THRESHOLD=4*8=32

如上圖:轉(zhuǎn)化紅黑樹的table容量最小容量64(JDK8定義的是64),至少是4*TREEIFY_THRESHOLD(單鏈表節(jié)點(diǎn)個(gè)數(shù)閥值,用以判斷是否需要樹形化)=32。用以避免 在擴(kuò)容和樹形結(jié)構(gòu)化的閥值 可能產(chǎn)生的沖突。所以如果小于64必須要擴(kuò)容。

?三、get查找

可見外部查找和JDK7差別不大,只是原本是entry[]查詢,JDK8編程N(yùn)ode[]查詢.都是 hash(key)找到table中元素位置,再遍歷找到鏈表(或者是紅黑樹)元素的key。根據(jù)元素類型判斷到底是查詢哪個(gè)類型

1 public V get(Object key) {2 Node<K,V> e;3 return (e = getNode(hash(key), key)) == null ? null : e.value;4 }5 6 /**7 * Implements Map.get and related methods8 *9 * @param hash hash for key 10 * @param key the key 11 * @return the node, or null if none 12 */ 13 final Node<K,V> getNode(int hash, Object key) { 14 Node<K,V>[] tab; Node<K,V> first, e; int n; K k; 15 if ((tab = table) != null && (n = tab.length) > 0 && 16 (first = tab[(n - 1) & hash]) != null) { 17 if (first.hash == hash && // always check first node 18 ((k = first.key) == key || (key != null && key.equals(k)))) 19 return first; 20 if ((e = first.next) != null) { 21 if (first instanceof TreeNode) 22 return ((TreeNode<K,V>)first).getTreeNode(hash, key); 23 do { 24 if (e.hash == hash && 25 ((k = e.key) == key || (key != null && key.equals(k)))) 26 return e; 27 } while ((e = e.next) != null); 28 } 29 } 30 return null; 31 }

?四、resize擴(kuò)容

第二部put的時(shí)候,有可能需要resize。 因?yàn)閚ewCap是oldCap的兩倍所以原節(jié)點(diǎn)的索引值要么和原來一樣,要么就是原(索引+oldCap)和JDK 1.7中實(shí)現(xiàn)不同這里不存在rehash

1 /**2 * Initializes or doubles table size. If null, allocates in3 * accord with initial capacity target held in field threshold.4 * Otherwise, because we are using power-of-two expansion, the5 * elements from each bin must either stay at same index, or move6 * with a power of two offset in the new table.7 *8 * @return the table9 */ 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 if (oldCap > 0) { 16 if (oldCap >= MAXIMUM_CAPACITY) { 17 threshold = Integer.MAX_VALUE; 18 return oldTab; 19 } 20 else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && 21 oldCap >= DEFAULT_INITIAL_CAPACITY) 22 newThr = oldThr << 1; // double threshold 23 } 24 else if (oldThr > 0) // initial capacity was placed in threshold 25 newCap = oldThr; 26 else { // zero initial threshold signifies using defaults 27 newCap = DEFAULT_INITIAL_CAPACITY; 28 newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); 29 } 30 if (newThr == 0) { 31 float ft = (float)newCap * loadFactor; 32 newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ? 33 (int)ft : Integer.MAX_VALUE); 34 } 35 threshold = newThr; 36 @SuppressWarnings({"rawtypes","unchecked"}) 37 Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap]; 38 table = newTab; 39 if (oldTab != null) { 40 for (int j = 0; j < oldCap; ++j) { 41 Node<K,V> e; 42 if ((e = oldTab[j]) != null) { 43 oldTab[j] = null; 44 if (e.next == null) 45 newTab[e.hash & (newCap - 1)] = e; 46 else if (e instanceof TreeNode) 47 ((TreeNode<K,V>)e).split(this, newTab, j, oldCap); 48 else { // preserve order 49 Node<K,V> loHead = null, loTail = null; 50 Node<K,V> hiHead = null, hiTail = null; 51 Node<K,V> next; 52 do { 53 next = e.next; 54 if ((e.hash & oldCap) == 0) { 55 if (loTail == null) 56 loHead = e; 57 else 58 loTail.next = e; 59 loTail = e; 60 } 61 else { 62 if (hiTail == null) 63 hiHead = e; 64 else 65 hiTail.next = e; 66 hiTail = e; 67 } 68 } while ((e = next) != null); 69 if (loTail != null) { 70 loTail.next = null; 71 newTab[j] = loHead; 72 } 73 if (hiTail != null) { 74 hiTail.next = null; 75 //把節(jié)點(diǎn)移動新的位置j+oldCap,這種情況不適用與鏈表的節(jié)點(diǎn)數(shù)大于8的情況 76 //鏈表節(jié)點(diǎn)大于8的情況會轉(zhuǎn)換為紅黑樹存儲 77 newTab[j + oldCap] = hiHead; 78 } 79 } 80 } 81 } 82 } 83 return newTab; 84 }

五.HashMap節(jié)點(diǎn)紅黑樹存儲

好了終于到treeify了,大部分內(nèi)容都在注解中

1 final void treeify(Node<K,V>[] tab) {2 TreeNode<K,V> root = null;3 for (TreeNode<K,V> x = this, next; x != null; x = next) {4 next = (TreeNode<K,V>)x.next;5 x.left = x.right = null;6 if (root == null) {7 x.parent = null;8 x.red = false;9 root = x; 10 } 11 else { 12 K k = x.key; 13 int h = x.hash; 14 Class<?> kc = null; 15 //遍歷root,把節(jié)點(diǎn)x插入到紅黑樹中,執(zhí)行先插入,然后進(jìn)行紅黑樹修正 16 for (TreeNode<K,V> p = root;;) { 17 int dir, ph; 18 K pk = p.key; 19 if ((ph = p.hash) > h) 20 dir = -1; 21 else if (ph < h) 22 dir = 1; 23 else if ((kc == null && 24 (kc = comparableClassFor(k)) == null) || 25 (dir = compareComparables(kc, k, pk)) == 0) 26 dir = tieBreakOrder(k, pk);//比較k和pk的值,用于判斷是遍歷左子樹還是右子樹 27 TreeNode<K,V> xp = p; 28 if ((p = (dir <= 0) ? p.left : p.right) == null) { 29 x.parent = xp; 30 if (dir <= 0) 31 xp.left = x; 32 else 33 xp.right = x; 34 //修正紅黑樹 35 root = balanceInsertion(root, x); 36 //退出循環(huán) 37 break; 38 } 39 } 40 } 41 } 42 moveRootToFront(tab, root); 43 }

上面主要做的是紅黑樹的insert,我們知道紅黑樹insert后是需要修復(fù)的,為了保持紅黑樹的平衡,我們來看下紅黑樹平衡的幾條性質(zhì):
1.節(jié)點(diǎn)是紅色或黑色。
2.根是黑色。
3.所有葉子都是黑色(葉子是NIL節(jié)點(diǎn))。
4.每個(gè)紅色節(jié)點(diǎn)必須有兩個(gè)黑色的子節(jié)點(diǎn)。(從每個(gè)葉子到根的所有路徑上不能有兩個(gè)連續(xù)的紅色節(jié)點(diǎn)。)
5.從任一節(jié)點(diǎn)到其每個(gè)葉子的所有簡單路徑都包含相同數(shù)目的黑色節(jié)點(diǎn)。

當(dāng)insert一個(gè)節(jié)點(diǎn)之后為了達(dá)到平衡,我們可能需要對節(jié)點(diǎn)進(jìn)行旋轉(zhuǎn)和顏色翻轉(zhuǎn)(上面的balanceInsertion方法)。

1 static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,2 TreeNode<K,V> x) {3 //插入的節(jié)點(diǎn)必須是紅色的,除非是根節(jié)點(diǎn) 4 x.red = true;5 //遍歷到x節(jié)點(diǎn)為黑色,整個(gè)過程是一個(gè)上濾的過程6 //xp=x.parent;xpp=xp.parent;xppl=xpp.left;xppr=xpp.right;7 for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {8 if ((xp = x.parent) == null) {9 x.red = false; 10 return x; 11 } 12 //如果xp的黑色就直接完成,最簡單的情況 13 else if (!xp.red || (xpp = xp.parent) == null) 14 return root; 15 //如果x的父節(jié)點(diǎn)是x父節(jié)點(diǎn)的左節(jié)點(diǎn) 16 if (xp == (xppl = xpp.left)) { 17 //x的父親節(jié)點(diǎn)的兄弟是紅色的(需要顏色翻轉(zhuǎn)) 18 if ((xppr = xpp.right) != null && xppr.red) { 19 //x父親節(jié)點(diǎn)的兄弟節(jié)點(diǎn)置成黑色 20 xppr.red = false; 21 //父幾點(diǎn)和其兄弟節(jié)點(diǎn)一樣是黑色 22 xp.red = false; 23 //祖父節(jié)點(diǎn)置成紅色 24 xpp.red = true; 25 //然后上濾(就是不斷的重復(fù)上面的操作) 26 x = xpp; 27 } 28 else { 29 //如果x是xp的右節(jié)點(diǎn)整個(gè)要進(jìn)行兩次旋轉(zhuǎn),先左旋轉(zhuǎn)再右旋轉(zhuǎn) 30 if (x == xp.right) { 31 root = rotateLeft(root, x = xp); 32 xpp = (xp = x.parent) == null ? null : xp.parent; 33 } 34 if (xp != null) { 35 xp.red = false; 36 if (xpp != null) { 37 xpp.red = true; 38 root = rotateRight(root, xpp); 39 } 40 } 41 } 42 } 43 //以左節(jié)點(diǎn)鏡像對稱就不做具體分析了 44 else { 45 if (xppl != null && xppl.red) { 46 xppl.red = false; 47 xp.red = false; 48 xpp.red = true; 49 x = xpp; 50 } 51 else { 52 if (x == xp.left) { 53 root = rotateRight(root, x = xp); 54 xpp = (xp = x.parent) == null ? null : xp.parent; 55 } 56 if (xp != null) { 57 xp.red = false; 58 if (xpp != null) { 59 xpp.red = true; 60 root = rotateLeft(root, xpp); 61 } 62 } 63 } 64 } 65 } 66 }

出處:

https://www.cnblogs.com/dennyzhangdd/p/6745282.html

http://blog.csdn.net/u014026363/article/details/56342142

http://www.cnblogs.com/skywang12345/p/3245399.html

總結(jié)

以上是生活随笔為你收集整理的【Java深入研究】9、HashMap源码解析(jdk 1.8)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。