美文网首页
2021-11-19 jdk8中HashMap的resize方

2021-11-19 jdk8中HashMap的resize方

作者: 归去来ming | 来源:发表于2021-11-19 16:54 被阅读0次
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
    }
    /* 没有初始化,并且在使用时通过构造函数指定了initialCapacity, 则table大小为threshold, 即大于指定initialCapacity的最小的2的整数次幂(可以通过构造函数得出)
    比如new HashMap(13);
    */
    else if (oldThr > 0) // initial capacity was placed in threshold
        newCap = oldThr;
    // 没有初始化,并且没有通过构造函数指定initialCapacity,则赋予默认值(数组大小为16,加载因子为0.75)比如:new HashMap();        
    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"})
    /* 真正给HashMap设置容量值的时候,也就是数组长度 
        new HashMap时并未给数组设置值,包括new HashMap(15)这个构造函数也没有设置
    */
    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;
}

只看第三个if:链表处理


Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
    next = e.next;
    /*
        根据e.hash & oldCap是不是0将原链表拆分成2个
        因为容量是2的n次幂,二进制表示中只有一个1,其余都是0
        例如oldCap为8,二进制形式:0000 1000          
    */
    if ((e.hash & oldCap) == 0) {
        // 还没有结点
        if (loTail == null)
            loHead = e;
               // 有节点,则把e放在尾部
        else
            loTail.next = e;
        // 添加了新节点e,现在e是尾节点,而loTail还指向原来的尾节点,所以把loTail往后移至e
        loTail = e;
    }
    else {
        // 还没有节点
        if (hiTail == null)
            hiHead = e;
        // 有节点,则把e放在下一个节点
        else
            hiTail.next = e;
        // 添加了新节点e,现在e是尾节点,而hiTail还指向原来的尾节点,所以把hiTail往后移至e
        hiTail = e;
    }
} while ((e = next) != null);
if (loTail != null) {
    // 尾节点不为空,则把下一个节点设为null,表示loTail是尾节点
    loTail.next = null;
    // 把loHead这条链表挂在新数组的j处
    newTab[j] = loHead;
}
if (hiTail != null) {
    // 尾节点不为空,则把下一个节点设为null,表示loTail是尾节点
    hiTail.next = null;
    // 把hiHead这条链表挂在新数组的j + oldCap处
    newTab[j + oldCap] = hiHead;
}

相关文章

网友评论

      本文标题:2021-11-19 jdk8中HashMap的resize方

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