HashMap 源码分析(JDK1.8)

作者: Jacky_Y | 来源:发表于2018-01-05 15:14 被阅读97次
    前言

    HashMap想必大家都很熟悉,JDK1.8 的 HashMap 随便一搜都是一大片一大片的,那为什么还要写呢,我会把它精简一下,一方面有利于自己的学习,另一方面希望让大家更好理解核心内容。本篇主要讲解HashMap源码的主要流程。本篇借鉴了 美团的HashMap源码解析 ,我们一起来看下JDK1.8做了哪些优化~

    JDK1.7 VS JDK1.8 源码比较

    优化概述,之后会一一细说:

    1. resize 扩容优化
    2. 引入了红黑树,目的是避免单条链表过长而影响查询效率,红黑树算法请参考 http://blog.csdn.net/v_july_v/article/details/6105630, HashMap整体结构如图:
      image.png
    3. 解决了多线程死循环问题,但仍是非线程安全的,多线程时可能会造成数据丢失问题。
    JDK1.7 VS JDK1.8 性能比较:
    • Hash较均匀的情况: image.png
    • Hash不均匀的情况: image.png

    JDK1.8 中的 HashMap 是不是666的飞起,性能碾压JDK1.7中的HashMap~ 但是源码可比JDK1.7难读一些了,接下来一起来学习下 HashMap 的源码,提前透露下,核心方法是 resizeputVal

    预备知识
    1. HashMap 中 table 角标计算及table.length 始终为2的幂,即 2 ^ n,对应的代码是 :
       /**
         * Returns a power of two size for the given target capacity.
         */
        static final int tableSizeFor(int cap) {
            int n = cap - 1;
            n |= n >>> 1;
            n |= n >>> 2;
            n |= n >>> 4;
            n |= n >>> 8;
            n |= n >>> 16;
            return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
        }
    
        // 取key的hashCode值、高位运算、取模运算
        // 在JDK1.8的实现中,优化了高位运算的算法,
        // 通过hashCode()的高16位异或低16位实现的:(h = k.hashCode()) ^ (h >>> 16),
        // 主要是从速度、功效、质量来考虑的,这么做可以在数组table的length比较小的时候,
        // 也能保证考虑到高低Bit都参与到Hash的计算中,同时不会有太大的开销。
        static final int hash(Object key) {
            int h;
            return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
        }
    

    我们在代码中经常会看到这样计算table索引:

    image.png
    这就是 table.length 为何是 2 ^ n 的原因了,图中 n 为 table的长度:
    image.png
    这样计算之后, 在 n 为 2 ^ n 时, 其实相当于 hash % n,& 当然比 % 效率高,这也是HashMap 计算角标时的巧妙之处。
    1. capacity、threshold和loadFactor之间的关系:
    • capacity table的容量,默认容量是16
    • threshold table扩容的临界值
    • loadFactor 负载因子,一般 threshold = capacity * loadFactor,默认的负载因子0.75是对空间和时间效率的一个平衡选择,建议大家不要修改。
    基本元素(原 Entity)
       static class Node<K,V> implements Map.Entry<K,V> {
            final int hash;  // node的hash值
            final K key; // node的key
            V value; // node的value
            Node<K,V> next; // node指向下一个node的引用
    
            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;
            }
        }
    
    resize()方法

    我们将resize()方法分为两部分,第一部分是生成newTable的过程,第二部分是迁移数据。

    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);
            }
            threshold = newThr;
            @SuppressWarnings({"rawtypes","unchecked"})
                Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
            table = newTab;
            ...
            return newTab;
        }
    

    以上代码展示了 newTable 的创建过程,由于 table、capacity、threshold等是懒加载,所以会有一系列的判断及对应的初始化,这些不是特别重要,重点在下边,注释标在代码块上(红黑树较为复杂,这里不做讲解,后续会考虑单讲红黑树):

    if (oldTab != null) {
        for (int j = 0; j < oldCap; ++j) {  // 遍历 oldTab
            Node<K,V> e;
            if ((e = oldTab[j]) != null) { 
                oldTab[j] = null;
                if (e.next == null)   // 如果只有table[j]中有元素
                    newTab[e.hash & (newCap - 1)] = e;
                else if (e instanceof TreeNode)  // 如果e是红黑树节点,走红黑树替换方式
                    ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
                else { // 如果 table[j] 后是一个链表 ,将原链表拆分为两条链,分别放到newTab中 
                    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) {
                            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;
                    }
                }
            }
        }
    }
    

    重点就在这个 链表拆分,首次看到 e.hash & oldCap 我是懵逼的。。。其实这样是一个取巧的办法,性能上优于rehash的过程,我们用图解方式去解释如何进行链表拆分:

    image.png (a) 是未扩容时, key1key2 得出的 hash & (n - 1) 均为 5。
    (b) 是扩容之后,key1 计算出的 newTab 角标依旧为 5,但是 key2 由于 扩容, 得出的角标 加了 16,即21, 16是oldTab的length,再来看e.hash & oldCap,oldCap.length即n 本身为 0000 0000 0000 0000 0000 0000 0001 0000 ,这个位与运算可以得出扩容后哪些key 在 扩容新增位时1,哪些是0,一个位运算替换了rehash过程,是不是得给100个👍👍👍...👍,大概扩容的过程如下:
    image.png
    线程安全问题

    JDK1.7 HashMap在多线程的扩容时确实会出现循环引用,导致下次get时死循环的问题,具体可以参考HashMap死循环问题。很多文章在说到死循环时都以JDK1.7来举例,其实JDK1.8的优化已经避免了死循环这个问题,但是会造成数据丢失问题,下面我举个例子(需要对应上边resize的下半部分代码):

    创建 thread1 和 thread2 去添加数据,此时都在resize,两个线程分别创建了两个newTable,并且thread1在table = newTab;处调度到thread2(没有给table赋值),等待thread2扩容之后再调度回thread1,注意,扩容时oldTab[j] = null; 也就将 oldTable中都清掉了,当回到thread1时,将table指向thread1的newTable,但访问oldTable中的元素全部为null,所以造成了数据丢失。

    putVal()方法

    put方法其实调用了putVal(参数onlyIfAbsent表示如果为true,若put的位置已经有value,则不修改,putIfAbsent方法中传true),这个方法的重点在于 TREEIFY_THRESHOLD 这个变量,如果链表长度 >= TREEIFY_THRESHOLD - 1 ,则调用 treeifyBin 方法, 从它的注释上可以看出,这个方法会把这条链所有的Node变为红黑树结构。

    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)
            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
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }
    
    /**
     * Replaces all linked nodes in bin at index for given hash unless
     * table is too small, in which case resizes instead.
     */
    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)  // 如果 tab.length 小于 64, 则只进行 resize
                resize();
            else if ((e = tab[index = (n - 1) & hash]) != null) {
                TreeNode<K,V> hd = null, tl = null;
                do {
                    TreeNode<K,V> p = replacementTreeNode(e, null);  // Node 替换为 TreeNode
                    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);   // 红黑树转换过程
            }
    }
    
    entrySet()遍历

    我们在遍历HashMap的时候都会使用 map.entrySet().iterator(),看下这个 iterator 是什么:

    public Set<Map.Entry<K,V>> entrySet() {
            Set<Map.Entry<K,V>> es;
            return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
        }
    
        final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
            ...
            public final Iterator<Map.Entry<K,V>> iterator() {
                return new EntryIterator();
            }
            ...
        }
    
        final class EntryIterator extends HashIterator
            implements Iterator<Map.Entry<K,V>> {
            public final Map.Entry<K,V> next() { return nextNode(); }
        }
    
        abstract class HashIterator {
            Node<K,V> next;        // next entry to return
            Node<K,V> current;     // current entry
            int expectedModCount;  // for fast-fail
            int index;             // current slot
    
            HashIterator() {
                expectedModCount = modCount;
                Node<K,V>[] t = table;
                current = next = null;
                index = 0;
                if (t != null && size > 0) { // advance to first entry
                    do {} while (index < t.length && (next = t[index++]) == null);
                }
            }
    
            public final boolean hasNext() {
                return next != null;
            }
    
            final Node<K,V> nextNode() {
                Node<K,V>[] t;
                Node<K,V> e = next;
                if (modCount != expectedModCount)
                    throw new ConcurrentModificationException();
                if (e == null)
                    throw new NoSuchElementException();
                if ((next = (current = e).next) == null && (t = table) != null) {
                    do {} while (index < t.length && (next = t[index++]) == null);
                }
                return e;
            }
    
            public final void remove() {
                Node<K,V> p = current;
                if (p == null)
                    throw new IllegalStateException();
                if (modCount != expectedModCount)
                    throw new ConcurrentModificationException();
                current = null;
                K key = p.key;
                removeNode(hash(key), key, null, false, false);
                expectedModCount = modCount;
            }
        }
    

    ArrayListLinkedList一样,还是modCountexpectedModCount的问题,expectedModCountiterator构造时赋的值,等于当时的modCount,所以如果已经生成了iterator,如果擅自使用map.put()等操作,会使modCount变化,导致expectedModCount != modCount,会抛出ConcurrentModificationException

    结尾

    好了,以上除了红黑树,HashMap中的我认为的核心内容至此就说完了,可以看出JDK一直在许多细节上不断地在做优化,作为我们,还是需要不断地修炼,去发现这些代码中的惊艳之处!

    参考

    https://tech.meituan.com/java-hashmap.html
    http://blog.csdn.net/xuefeng0707/article/details/40797085

    相关文章

      网友评论

        本文标题:HashMap 源码分析(JDK1.8)

        本文链接:https://www.haomeiwen.com/subject/jwejnxtx.html