特殊场景下的HashMap扩容、树化机制

问题:

  • 1、HashMap的扩容机制是什么?
  • 2、链表红黑树互转的机制是怎样的?

扩容机制

HashMap初始化数组长度是2^4=16,扩容因子是0.75,当HashMap元素个数超过数组长度的3/4,则会进行扩容。

互转机制

HashMap定义了两个(链表深度 / 红黑树节点数量)阈值:

  • TREEIFY_THRESHOLD = 8;      ——表示链表转红黑树的阈值
  • UNTREEIFY_THRESHOLD = 6; ——表示红黑树转链表的阈值

正常情况下,当链表深度大于8,则进行转红黑树操作;当红黑树节点数量小于6,则进行转链操作;以上,都是在正常情况下的操作,但是,从源码看,HashMap存在特殊情况并不是按照上面的条件进行扩容和互转。

特别场景

如果所有的元素,key对象的hashcode都相同,那么所有的元素都会落到数组同一个位置,此时,扩容和转树机制将有所不同,有源码为证:

在putVal中,当新增加节点后链表元素数量大于等于阈值8,会调用treeifyBin方法进行转树操作

    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);
                        // 关键点
                        // 当新增加节点后的链表元素数量大于等于阈值8
                        // 就会调用treeifyBin方法进行转树操作
                        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;
    }

但是转树操作并不一定真的去转树,假如数组长度小于64的话,即便链表元素数量大于8也不会转树,而是会去做扩容。 

    final void treeifyBin(Node<K,V>[] tab, int hash) {
        int n, index; Node<K,V> e;
        // 数组长度小于64的化,即便链表元素数量大于8也不会转树,而是会去做扩容
        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<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);
        }
    }

演变过程图

  1. 放8个元素到HashMap;
  2. 放第9个元素进去,做树化操作,由于数组数量16<64,只能做扩容;
  3. 放第10个元素进去,做树化操作,由于数组数量32<64,还是只能做扩容;
  4. 放第11个元素进去,做树化操作,由于数组数量达到64,进行树化操作;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值