HashMap浅析三:扩容

前面分析了为什么HashMap的容量是2的n次幂,以及HashMap的容量初始化。在分析容量初始化的时候有提到一个装载因子。

那么这个装载因子是什么呢?还是通过源码来看看。


    /**
     * The load factor used when none specified in constructor.
     */
    static final float DEFAULT_LOAD_FACTOR = 0.75f;

默认值时0.75,一般也不建议修改。这个是什么意思呢?就是当HashMap的当前容量达到总容量乘以装载因子时,对HashMap进行扩容。HashMap里有个成员变量threshold,该变量即表示HashMap能允许的最大容量。


    /**
     * The next size value at which to resize (capacity * load factor).
     *
     * @serial
     */
    int threshold;

从改变量的注释也能看出,当容量达到总容量乘以装载因子时,对HashMap进行扩容。那么HashMap扩容是怎么做的呢?

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

从这里也能看出,扩容就是newThr = oldThr << 1; // double threshold,即扩容为原来的两倍,这样也保证了容量是2的指数幂。然后对原来的K-V键值对进行重新Hash放入扩容后的HashMap。因此HashMap的扩容是非常消耗性能的。这也是为什么HashMap初始化的时候要指定合适的容量,这样可以尽可能的避免扩容的发生从而提高性能。

上一篇:jdk源码-hashMap源码解读


下一篇:Java8 HashMap 扩容之后旧元素存放位置