HashMap源码(三)

HashMap红黑树转换条件是什么?

以下为HashMap的put方法

    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);
                        // TREEIFY_THRESHOLD=8
                        // 条件一:  判断如果当前bucket的位置链表长度是否大于8
                        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;
    }
  • 以上方法可以看见,若put完节点后,当前链表长度达到8则开始尝试执行treeifyBin方法来转化红黑树。接下来我们看下treeifyBin方法做了什么
    final void treeifyBin(Node<K,V>[] tab, int hash) {
        int n, index; Node<K,V> e;
        // MIN_TREEIFY_CAPACITY=64
        // 条件二: 判断当前数组长度是否大于64,若不大于则进行resize操作
        if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
            resize();
        else if ((e = tab[index = (n - 1) & hash]) != null) {
            // 若满足条件一二,则转化为红黑树结构
            TreeNode<K,V> hd = null, tl = null;
            do {
                // 将所有节点都转化为TreeNode
                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);
        }
    }

后续看到此博客,写的更详细:https://www.cnblogs.com/wangflower/p/12235595.html

posted @ 2020-07-31 16:47  faylinn  阅读(217)  评论(0编辑  收藏  举报
、、、