HashMap难吗? 看完这篇so easy!

前言

hashmap一直是面试重灾区,虽然已经烂大街了,但是还是有必要去了解一下的。

本文将基于JDK1.8源码来解答以下几个问题:

  1. 为什么hashmap的大小是2的n次方呢?
  2. 扩容在什么时候发生?
  3. 什么时候会变成红黑树呢?
  4. 什么时候又会退回到链表呢?

构造方法解析

先看下构造方法有没有将大小初始化

    /**
    * 这里将根据传入的大小进行调整,设定真正的初始大小
    */
    public HashMap(int initialCapacity, float loadFactor) {
        // 初始小于0 直接异常
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        // 初始大于最大了 就取最大值
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        // 负载因子不能小于0 也不能非数字
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);
        this.loadFactor = loadFactor;
        this.threshold = tableSizeFor(initialCapacity);  // 这里tableSizeFor确定了threshold的大小
    }

    /**
     * 指定初始大小,但其实还是走的上面的构造方法
     */
    public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }
        
    /**
     * 无参构造方法,只会确认默认负载因子 这里是0.75
     */
    public HashMap() { 
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    }
复制代码

接着看下tableSizeFor(initialCapacity)的源码:

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;
}

看到这大概是很蒙的,我们一步一步来分析。

假如我们传入的初始大小为10,那么通过这个方法计算后会得到什么值呢? 会得到16。

n = cap - 1 // 此时n = 9 

1. 9向右移动一位 其实就是缩小一半
二进制       0000 1001  = 9
或上右移一位  0000 0100  = 4
结果         0000 1101  = 13

2. 13向右移20000 1101 = 130000 0011 = 3
结果        0000 1111 = 15

3.4位不用考虑了,还是原来的值

最后返回的就是 n + 1 = 16;

通过不同的值演算, 不论初始大小是多少,最后的值都是2的n次方,但这里还没有进行容量的初始化,在put的时候才会真正初始化大小。

我们接着看put方法。
复制代码

put方法-初始化大小的真凶


public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
}

// 计算出一个分布比较均匀的hash值
static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}


final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
            
        // 如果初始大小为0 那么设置一下大小 这里我们的table是个Null所以会先走resize()方法
        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) // TREEIFY_THRESHOLD 为8 在大与等于8的时候需要进行树化
                        treeifyBin(tab, hash);
                    break;
                }
                
                if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))
                    break;
                
                p = e;
            }
        }
        if (e != null) { // 存在相同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;
}

复制代码

resize() 扩容的奥秘

由于我们传入了一个默认大小10进去,经过构造方法计算后将 threshold 属性设置为了16,我们看看resize()内部发生了什么


 这里我们只关注第一次进来的情况, 我们的table此时是一个Null,threshold=16
 
 final Node<K,V>[] resize() {
        Node<K,V>[] oldTab = table;
        int oldCap = (oldTab == null) ? 0 : oldTab.length; // 为0
        int oldThr = threshold; // 为16
        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; // 这里 oldCap << 1 左移一位就是扩大一倍赋值给 newCap 这里就完成了扩容的大小
        }
        else if (oldThr > 0) // oldThr = 16 符合逻辑 赋值给 newCap = 16
            newCap = oldThr;
        else {               // zero initial threshold signifies using defaults
            newCap = DEFAULT_INITIAL_CAPACITY;
            newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
        }
        if (newThr == 0) { // 此时 newThr 还是等于 0 符合条件
            float ft = (float)newCap * loadFactor; // 计算出下一次的扩容阈值 16 * 0.75 = 12 
            newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
                      (int)ft : Integer.MAX_VALUE);  // 将12赋值给newThr
        }
        threshold = newThr; // 将最新的  newThr 赋值给  threshold也就是12
        @SuppressWarnings({"rawtypes","unchecked"})
        Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap]; // 初始化  newCap = 16 
        table = newTab; // 赋值给容器 table
        if (oldTab != null) { // 这里不满足条件
            for (int j = 0; j < oldCap; ++j) {
                Node<K,V> e;
                if ((e = oldTab[j]) != null) {
                    oldTab[j] = null;
                    if (e.next == null)
                        newTab[e.hash & (newCap - 1)] = e;
                    else if (e instanceof TreeNode)
                        ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
                    else { // preserve order
                        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;
                        }
                    }
                }
            }
        }
        // 直接返回新的 newTab 也就是 (Node<K,V>[])new Node[newCap]  16的大小
        return newTab;
    }

复制代码

可以看到在经过第一次Put的时候会发生resize(),它的目的就是初始化容器的大小,计算出下一次扩容的阈值, 可以看到负载因子在这的作用。

如果OldCap不是一个null的话,会将oldCap << 1 也就是左移一位赋值给newCap,这就是hashMap扩容的秘密,翻倍扩容。

回到putVal()

接着看第二个if判断

if ((p = tab[i = (n - 1) & hash]) == null)
    tab[i] = newNode(hash, key, value, null);
复制代码

这里是通过hash确认此次put的位置,如果这个位置没有值就直接赋值。

然后直接看到最后一个判断

if (++size > threshold)
    resize();
复制代码

这里使用size和负载因子计算出来的值进行判断看是否要扩容。

到这应该明白,如果你想要一个可以存放16大小的hashmap,那么你的初始化大小就应该是32,为什么是32呢,因为Hashmap的大小永远是2的n次方,又由于负载因子的存在, 32*0.75 = 24 ,也就是说设置32的大小,在元素超过24的时候才会发生扩容。这也就提高了程序的一点性能。

这里推荐使用 com.google.common.collect 下的 Maps.newHashMapWithExpectedSize() 这样可以自动计算出你需要的大小。

到这我们能回答出第一个和第二个问题了。你能总结出来吗?

  1. 扩容算法是向左移1位计算的,也就是翻倍扩容,又因为初始大小会被构造方法重新计算,值为2的n次方,所以后面的扩容都是2的n+1次方。
  2. 扩容发生在数量大于扩容阈值时。

树化

其实仔细看源码也能知道在同一个位置元素超过8个时会进入 treeifyBin(tab, hash);

if (binCount >= TREEIFY_THRESHOLD - 1) // TREEIFY_THRESHOLD 为8在大与等于8的时候需要进行树化
    treeifyBin(tab, hash);
复制代码

这个也就是树化了,我们进去看一下扒一下它的外衣。


    /**
     * The bin count threshold for using a tree rather than list for a
     * bin.  Bins are converted to trees when adding an element to a
     * bin with at least this many nodes. The value must be greater
     * than 2 and should be at least 8 to mesh with assumptions in
     * tree removal about conversion back to plain bins upon
     * shrinkage.
     */
    static final int TREEIFY_THRESHOLD = 8;

    /**
     * The bin count threshold for untreeifying a (split) bin during a
     * resize operation. Should be less than TREEIFY_THRESHOLD, and at
     * most 6 to mesh with shrinkage detection under removal.
     */
    static final int UNTREEIFY_THRESHOLD = 6;

    /**
     * The smallest table capacity for which bins may be treeified.
     * (Otherwise the table is resized if too many nodes in a bin.)
     * Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts
     * between resizing and treeification thresholds.
     */
    static final int MIN_TREEIFY_CAPACITY = 64;


    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 ) // 判断一下目前的大小是否满足树化 这里MIN_TREEIFY_CAPACITY 也就是最小树化大小 此时为64。
            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);
                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);
        }
    }

复制代码

到这也知道了树化的条件为 hashmap大小超过64并且同一位置的元素超过8个,这也就解答了第三个问题。这里我们不深究具体的树化逻辑。

树退化的时机

树退化其实也发生在resize的时期,如果扩容了,那么hash冲突的情况就会变少,此时最好就是将树退为链表,来看一下代码。

        if (oldTab != null) {
            for (int j = 0; j < oldCap; ++j) {
                Node<K,V> e;
                if ((e = oldTab[j]) != null) {
                    oldTab[j] = null;
                    if (e.next == null)
                        newTab[e.hash & (newCap - 1)] = e;
                    else if (e instanceof TreeNode)
                        ((TreeNode<K,V>)e).split(this, newTab, j, oldCap); // 树退化
                    else { // preserve order
                        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;
                        }
                    }
                }
            }
        }
        

可以看到在老的容器不为空的时候,如果节点时一个树节点,那么就会进入((TreeNode<K,V>)e).split(this, newTab, j, oldCap); 


    /**
     * The bin count threshold for untreeifying a (split) bin during a
     * resize operation. Should be less than TREEIFY_THRESHOLD, and at
     * most 6 to mesh with shrinkage detection under removal.
     */
    static final int UNTREEIFY_THRESHOLD = 6;


 final void split(HashMap<K,V> map, Node<K,V>[] tab, int index, int bit) {
            TreeNode<K,V> b = this;
            // Relink into lo and hi lists, preserving order
            TreeNode<K,V> loHead = null, loTail = null;
            TreeNode<K,V> hiHead = null, hiTail = null;
            int lc = 0, hc = 0;
            for (TreeNode<K,V> e = b, next; e != null; e = next) {
                next = (TreeNode<K,V>)e.next;
                e.next = null;
                if ((e.hash & bit) == 0) {
                    if ((e.prev = loTail) == null)
                        loHead = e;
                    else
                        loTail.next = e;
                    loTail = e;
                    ++lc;
                }
                else {
                    if ((e.prev = hiTail) == null)
                        hiHead = e;
                    else
                        hiTail.next = e;
                    hiTail = e;
                    ++hc;
                }
            }

            if (loHead != null) {
                if (lc <= UNTREEIFY_THRESHOLD)  // 发生退化
                    tab[index] = loHead.untreeify(map);
                else {
                    tab[index] = loHead;
                    if (hiHead != null) // (else is already treeified)
                        loHead.treeify(tab);
                }
            }
            if (hiHead != null) {
                if (hc <= UNTREEIFY_THRESHOLD) // 发生退化
                    tab[index + bit] = hiHead.untreeify(map);
                else {
                    tab[index + bit] = hiHead;
                    if (loHead != null)
                        hiHead.treeify(tab);
                }
            }
        }

到这看到这2个判断也就明白了,当树的数量小于等于 UNTREEIFY_THRESHOLD 也就是6的时候会退化成链表。

if (lc <= UNTREEIFY_THRESHOLD)  // 发生退化
    tab[index] = loHead.untreeify(map);
                    
if (hc <= UNTREEIFY_THRESHOLD) // 发生退化
    tab[index + bit] = hiHead.untreeify(map);
    
复制代码

总结

hashmap初始是一个连续的数组,每个元素是一个node,当某个位置的node数量大于8个 并且大小超过64的时候就会发生树化。 当put的数量超过负载因子计算出的数值后就会发生扩容。扩容的时候如果发现树的元素个数小于6了就会退化成链表。

到这我们完全能回答出上述的4个问题了,你能再总结出来吗?

看完有帮助的化麻烦点赞一下哟,或者可以关注一下作者,查看其他内容。

我的其他源码分析文章:

一步一步源码探索-ArrayList#add() - 掘金 (juejin.cn)

CopyOnWriteArrayList核心源码解读 - 掘金 (juejin.cn)

我的专栏: 一步一步源码探索 - 三海的专栏 - 掘金 (juejin.cn)

おすすめ

転載: juejin.im/post/7049985985848803365