继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

JDK1.8 不一样的HashMap

拉丁的传说
关注TA
已关注
手记 448
粉丝 126
获赞 788
前言

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 的源码提前透露下核心方法是 resize 和 putVal

预备知识
  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) 是未扩容时 key1 和 key2 得出的 hash & (n - 1) 均为 5。
(b) 是扩容之后key1 计算出的 newTab 角标依旧为 5但是 key2 由于 扩容 得出的角标 加了 16即21 16是oldTab的length再来看e.hash & oldCapoldCap.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一样还是modCount expectedModCount 的问题expectedModCountiterator构造时赋的值等于当时的modCount所以如果已经生成了iterator如果擅自使用map.put()等操作会使modCount变化导致expectedModCount != modCount会抛出ConcurrentModificationException 

结尾

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

原文链接http://www.apkbus.com/blog-487165-76822.html

打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP