jdk1.8扩容 resize() 笔记

 

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K, V>[] tab;
        Node<K, V> p;
        int n, i;//设置变量tab,p,n,i
        if ((tab = table) == null || (n = tab.length) == 0) // 第一进来默认为null
            n = (tab = resize()).length;//初始化HashMap 容量16
        if ((p = tab[i = (n - 1) & hash]) == null)// 获取tab下标,若不为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; // key值碰撞,新的值覆盖旧的值,返回旧值
            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) { // 链表的下一节点等于null创建链表
                        p.next = newNode(hash, key, value, null); // binCount:0-7 8个节点 后在进行newNode最大有9个节点,进行红黑树操作
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st  如果数组中某一个链表 >= 8 并且大于MIN_TREEIFY_CAPACITY 需要转化为红黑树(当链表长度大于8并且数组长度大于64时,才会转换为红黑树。如果链表长度大于8,但是数组长度小于64时,还是会进行扩容操作,不会转换为红黑树。)
                            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; // key值碰撞,新的值覆盖旧的值,返回旧值
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)// 阀值12
            resize();
        afterNodeInsertion(evict);
        return null;
    }
 final Node<K,V>[] resize() {
        Node<K,V>[] oldTab = table; // 初始化节点,第一次为null,第二次16
        int oldCap = (oldTab == null) ? 0 : oldTab.length; // 第二次16
        int oldThr = threshold; // 第一次12
        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) // 阀值扩容 *2
                newThr = oldThr << 1; // double threshold 左移1位(*2)
        }
        else if (oldThr > 0) // initial capacity was placed in threshold
            newCap = oldThr;
        else {               // zero initial threshold signifies using defaults
            newCap = DEFAULT_INITIAL_CAPACITY;// 第一次初始化默认容量16
            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; // gc
                    if (e.next == null) // 若下个节点等于null直接存入数组
                        newTab[e.hash & (newCap - 1)] = e; // 放入新的数组中 若不减1 超过数组最大容量  高位
                    else if (e instanceof TreeNode) // 红黑树
                        ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
                    else { // preserve order 双向列表
                        Node<K,V> loHead = null, loTail = null; // 低位:loHead头链 loTail尾链
                        Node<K,V> hiHead = null, hiTail = null; // 高位
                        Node<K,V> next;
                        do {
                            next = e.next;
                            if ((e.hash & oldCap) == 0) { // 原下标位置
                                if (loTail == null) // 低位尾节点为null
                                    loHead = e; // 元素放在头节点
                                else
                                    loTail.next = e; // 放在尾节点的下个节点 12->13
                                loTail = e; // 首次头节点=尾节点
                            }
                            else {
                                if (hiTail == null) // 高位尾节点不存在,首次进来尾节点=首节点
                                    hiHead = e; // 放在尾节点  hiHead=1 hiTail=1
                                else
                                    hiTail.next = e; // 1->2
                                hiTail = e;
                            }
                        } while ((e = next) != null);
                        if (loTail != null) { // 低位尾节点不为null
                            loTail.next = null; // 首次头节点=尾节点 尾节点gc回收
                            newTab[j] = loHead; // 放入数组
                        }
                        if (hiTail != null) { // 高位插入在原下标+j的位置
                            hiTail.next = null; // 第一次进来是 1-value
                            newTab[j + oldCap] = hiHead;
                        }
                    }
                }
            }
        }
        return newTab;
/**
         * Tree version of putVal.
         */
        final TreeNode<K,V> putTreeVal(HashMap<K,V> map, Node<K,V>[] tab,
                                       int h, K k, V v) {
            Class<?> kc = null;
            boolean searched = false;
            TreeNode<K,V> root = (parent != null) ? root() : this;
            for (TreeNode<K,V> p = root;;) { // 校验是左子树还是右子树,-1左1右
                int dir, ph; K pk;
                if ((ph = p.hash) > h)
                    dir = -1;
                else if (ph < h)
                    dir = 1;
                else if ((pk = p.key) == k || (k != null && k.equals(pk)))
                    return p;
                else if ((kc == null &&
                          (kc = comparableClassFor(k)) == null) ||
                         (dir = compareComparables(kc, k, pk)) == 0) {
                    if (!searched) {
                        TreeNode<K,V> q, ch;
                        searched = true;
                        if (((ch = p.left) != null &&
                             (q = ch.find(h, k, kc)) != null) ||
                            ((ch = p.right) != null &&
                             (q = ch.find(h, k, kc)) != null))
                            return q;
                    }
                    dir = tieBreakOrder(k, pk);
                }

                TreeNode<K,V> xp = p;
                if ((p = (dir <= 0) ? p.left : p.right) == null) {
                    Node<K,V> xpn = xp.next;
                    TreeNode<K,V> x = map.newTreeNode(h, k, v, xpn);// 创建树节点
                    if (dir <= 0)
                        xp.left = x;
                    else
                        xp.right = x;
                    xp.next = x;
                    x.parent = x.prev = xp;
                    if (xpn != null)
                        ((TreeNode<K,V>)xpn).prev = x;
                    moveRootToFront(tab, balanceInsertion(root, x));//balanceInsertion 旋转树节点
                    return null;
                }
            }
        }

 

记:2021.04.03 10:40 周六

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值