小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

jdk源碼剖析四:JDK1.7升級1.8 HashMap原理的變化

 Baruch 2017-10-03

目錄:

1.數(shù)據(jù)結(jié)構(gòu)

2.put插入

3.get查找

4.resize擴容

5.HashMap節(jié)點紅黑樹存儲

==============

一、hashMap數(shù)據(jù)結(jié)構(gòu)

如上圖所示,JDK7之前hashmap又叫散列鏈表:基于一個數(shù)組以及多個鏈表的實現(xiàn),hash值沖突的時候,就將對應(yīng)節(jié)點以鏈表的形式存儲。

JDK8中,當(dāng)同一個hash值(Table上元素)的鏈表節(jié)點數(shù)不小于8時,將不再以單鏈表的形式存儲了,會被調(diào)整成一顆紅黑樹。這就是JDK7與JDK8中HashMap實現(xiàn)的最大區(qū)別。

二、put插入元素

源代碼如下:

 

復(fù)制代碼
 1      /**
 2      * Implements Map.put and related methods
 3      * 實現(xiàn)Map的put和相關(guān)操作
 4      * @param hash hash for key  計算出來的key的hash值
 5      * @param key the key    鍵
 6      * @param value the value to put  值
 7      * @param onlyIfAbsent if true, don't change existing value 為true則不覆蓋已存在值
 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         //tab數(shù)組節(jié)點    p當(dāng)前需要插入的節(jié)點                        
14         Node<K,V>[] tab; Node<K,V> p; int n, i;
15         //如果當(dāng)前map中無數(shù)據(jù),執(zhí)行resize方法。并且返回n
16         if ((tab = table) == null || (n = tab.length) == 0)
17             n = (tab = resize()).length;
18         //如果要插入的鍵值對在tab上剛好沒有元素,那么把他封裝成Node對象,放在這個位置上結(jié)束。
19         if ((p = tab[i = (n - 1) & hash]) == null)
20             tab[i] = newNode(hash, key, value, null);
21         //有元素,在table的i位置發(fā)生碰撞
22         else {
23             Node<K,V> e; K k;
24             //1.hash值一樣,key一樣,替換
25             if (p.hash == hash &&
26                 ((k = p.key) == key || (key != null && key.equals(k))))
27                 e = p;
28             //2.如果當(dāng)前節(jié)點是TreeNode類型的數(shù)據(jù),插入紅黑樹
29             else if (p instanceof TreeNode)
30                 e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
31             //3.不是TreeNode,遍歷這條鏈子上的節(jié)點,跟jdk7一樣
32             else {
33                 for (int binCount = 0; ; ++binCount) {
34                     if ((e = p.next) == null) {
35                         p.next = newNode(hash, key, value, null);
36                         //如果大于等于7,即準備插入第8個,將鏈表轉(zhuǎn)換成紅黑樹(有條件,具體看下圖)。
37                         if (binCount >= TREEIFY_THRESHOLD - 1) 
38                             treeifyBin(tab, hash);
39                         break;
40                     }
41                     if (e.hash == hash &&
42                         ((k = e.key) == key || (key != null && key.equals(k))))
43                         break;
44                     p = e;
45                 }
46             }
47             if (e != null) { // existing mapping for key
48                 V oldValue = e.value;
49                 if (!onlyIfAbsent || oldValue == null)
50                     e.value = value;
51                 afterNodeAccess(e);
52                 return oldValue;
53             }
54         }
55         ++modCount;
56         //判斷閾值,決定是否擴容
57         if (++size > threshold)
58             resize();
59         afterNodeInsertion(evict);
60         return null;
61     }
復(fù)制代碼
treeifyBin方法源碼如下:
復(fù)制代碼
 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需要進行擴容
 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     }
復(fù)制代碼

 

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

復(fù)制代碼
 1     /**
 2      * The bin count threshold for using a tree rather than list for a
 3      * bin.  Bins are converted to trees when adding an element to a
 4      * bin with at least this many nodes. The value must be greater
 5      * than 2 and should be at least 8 to mesh with assumptions in
 6      * tree removal about conversion back to plain bins upon
 7      * 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;
復(fù)制代碼

 

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

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

 三、get查找

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

復(fù)制代碼
 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 methods
 8      *
 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     }    
復(fù)制代碼

 四、resize擴容

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

復(fù)制代碼
 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         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é)點移動新的位置j+oldCap,這種情況不適用與鏈表的節(jié)點數(shù)大于8的情況
76                             //鏈表節(jié)點大于8的情況會轉(zhuǎn)換為紅黑樹存儲
77                             newTab[j + oldCap] = hiHead;
78                         }
79                     }
80                 }
81             }
82         }
83         return newTab;
84     }
復(fù)制代碼

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

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

復(fù)制代碼
 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é)點x插入到紅黑樹中,執(zhí)行先插入,然后進行紅黑樹修正
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         }
復(fù)制代碼

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

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

復(fù)制代碼
 1 static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
 2                                                     TreeNode<K,V> x) {
 3             //插入的節(jié)點必須是紅色的,除非是根節(jié)點                                            
 4             x.red = true;
 5             //遍歷到x節(jié)點為黑色,整個過程是一個上濾的過程
 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é)點是x父節(jié)點的左節(jié)點
16                 if (xp == (xppl = xpp.left)) {
17                     //x的父親節(jié)點的兄弟是紅色的(需要顏色翻轉(zhuǎn))
18                     if ((xppr = xpp.right) != null && xppr.red) {
19                         //x父親節(jié)點的兄弟節(jié)點置成黑色
20                         xppr.red = false;
21                         //父幾點和其兄弟節(jié)點一樣是黑色
22                         xp.red = false;
23                         //祖父節(jié)點置成紅色
24                         xpp.red = true;
25                         //然后上濾(就是不斷的重復(fù)上面的操作)
26                         x = xpp;
27                     }
28                     else {
29                         //如果x是xp的右節(jié)點整個要進行兩次旋轉(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é)點鏡像對稱就不做具體分析了 
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         }
復(fù)制代碼

============================

參考:http://www.cnblogs.com/huaizuo/p/5371099.html

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多