JDK1.8_HashMap源码之resize

2019-08-09 00:00:00 jdk1
    final Node<K,V>[] resize() {
        //创建一个Node数组用于存放table中的元素,
        Node<K,V>[] oldTab = table;
        //获取旧table的长度
        int oldCap = (oldTab == null) ? 0 : oldTab.length;
        //获取旧的扩容阈值
        int oldThr = threshold;
        int newCap, newThr = 0;
        //如果旧的table中有元素
        if (oldCap > 0) {
            //如果旧table长度>=最大容量限制时不进行扩容,并将扩容阈值赋值为Integer.MAX_VALUE
            if (oldCap >= MAXIMUM_CAPACITY) {
                threshold = Integer.MAX_VALUE;
                return oldTab;
            }
            // 将新table长度赋值为旧table的2倍,
            // 判断旧table长度的二倍是否小于最大容量,且旧容量大于等于初始容量,
            // 以上判断成立则将新的扩容阀值赋值为旧的扩容阈值的二倍
            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"})
        //将旧table中的元素放到扩容后的newTable中
        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;
                    //如果数组对应下标位置只有一个元素,对hashCade取余并根据结果直接放到newTable相应的位置
                    if (e.next == null)
                        newTab[e.hash & (newCap - 1)] = e;
                        //如果数组对应下标位置的元素是一个红黑树,则拆分红黑树放到newTable中
                        // 如果拆分后的红黑树元素小于6,则转化为链表
                    else if (e instanceof TreeNode)
                        ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
                    else { // preserve order
                    //数组对应下标位置的元素是一个链表的情况
                    //根据(e.hash & oldCap)条件对链表进行拆分并放到newTable
                        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;
    }

 

相关文章