只会一点java

java持续学习者,每月一篇博客。罗列出技术栈,慢慢完善,持续学习,总有一天,你会追上甚至超越曾经的大神。
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

jdk源码剖析四:JDK1.7升级1.8 HashMap原理的变化

Posted on 2017-04-21 20:03  只会一点java  阅读(5142)  评论(0编辑  收藏  举报

一、hashMap数据结构

如上图所示,JDK7之前hashmap又叫散列链表:基于一个数组以及多个链表的实现,hash值冲突的时候,就将对应节点以链表的形式存储。

JDK8中,当同一个hash值(Table上元素)的链表节点数不小于8时,将不再以单链表的形式存储了,会被调整成一颗红黑树。这就是JDK7与JDK8中HashMap实现的最大区别。

二、put插入元素

源代码如下:

 

 1      /**
 2      * Implements Map.put and related methods
 3      * 实现Map的put和相关操作
 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数组节点    p当前需要插入的节点                        
14         Node<K,V>[] tab; Node<K,V> p; int n, i;
15         //如果当前map中无数据,执行resize方法。并且返回n
16         if ((tab = table) == null || (n = tab.length) == 0)
17             n = (tab = resize()).length;
18         //如果要插入的键值对在tab上刚好没有元素,那么把他封装成Node对象,放在这个位置上结束。
19         if ((p = tab[i = (n - 1) & hash]) == null)
20             tab[i] = newNode(hash, key, value, null);
21         //有元素,在table的i位置发生碰撞
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.如果当前节点是TreeNode类型的数据,插入红黑树
29             else if (p instanceof TreeNode)
30                 e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
31             //3.不是TreeNode,遍历这条链子上的节点,跟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个,将链表转换成红黑树(有条件,具体看下图)。
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     }
treeifyBin方法源码如下:
 1     final void treeifyBin(Node<K,V>[] tab, int hash) {
 2         int n, index; Node<K,V> e;
 3         //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     }

 

现在我们来看一下为什么需要扩容:

 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;

 

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

如上图:转化红黑树的table容量最小容量64(JDK8定义的是64),至少是4*TREEIFY_THRESHOLD(单链表节点个数阀值,用以判断是否需要树形化)=32。用以避免 在扩容和树形结构化的阀值 可能产生的冲突。所以如果小于64必须要扩容。

 三、get查找

可见外部查找和JDK7差别不大,只是原本是entry[]查询,JDK8编程Node[]查询.都是 hash(key)找到table中元素位置,再遍历找到链表(或者是红黑树)元素的key。根据元素类型判断到底是查询哪个类型

 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     }    

 四、resize扩容

第二部put的时候,有可能需要resize。 因为newCap是oldCap的两倍所以原节点的索引值要么和原来一样,要么就是原(索引+oldCap)和JDK 1.7中实现不同这里不存在rehash

 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                             //把节点移动新的位置j+oldCap,这种情况不适用与链表的节点数大于8的情况
76                             //链表节点大于8的情况会转换为红黑树存储
77                             newTab[j + oldCap] = hiHead;
78                         }
79                     }
80                 }
81             }
82         }
83         return newTab;
84     }

五.HashMap节点红黑树存储

好了终于到treeify了,大部分内容都在注解中

 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,把节点x插入到红黑树中,执行先插入,然后进行红黑树修正
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                             //退出循环
37                             break;
38                         }
39                     }
40                 }
41             }
42             moveRootToFront(tab, root);
43         }

上面主要做的是红黑树的insert,我们知道红黑树insert后是需要修复的,为了保持红黑树的平衡,我们来看下红黑树平衡的几条性质:
1.节点是红色或黑色。
2.根是黑色。
3.所有叶子都是黑色(叶子是NIL节点)。
4.每个红色节点必须有两个黑色的子节点。(从每个叶子到根的所有路径上不能有两个连续的红色节点。)
5.从任一节点到其每个叶子的所有简单路径都包含相同数目的黑色节点。

当insert一个节点之后为了达到平衡,我们可能需要对节点进行旋转和颜色翻转(上面的balanceInsertion方法)。

 1 static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
 2                                                     TreeNode<K,V> x) {
 3             //插入的节点必须是红色的,除非是根节点                                            
 4             x.red = true;
 5             //遍历到x节点为黑色,整个过程是一个上滤的过程
 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的父节点是x父节点的左节点
16                 if (xp == (xppl = xpp.left)) {
17                     //x的父亲节点的兄弟是红色的(需要颜色翻转)
18                     if ((xppr = xpp.right) != null && xppr.red) {
19                         //x父亲节点的兄弟节点置成黑色
20                         xppr.red = false;
21                         //父几点和其兄弟节点一样是黑色
22                         xp.red = false;
23                         //祖父节点置成红色
24                         xpp.red = true;
25                         //然后上滤(就是不断的重复上面的操作)
26                         x = xpp;
27                     }
28                     else {
29                         //如果x是xp的右节点整个要进行两次旋转,先左旋转再右旋转
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                 //以左节点镜像对称就不做具体分析了 
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         }

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

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