三分钟读懂HashMap.put()方法源码

写这篇博客原因是在看HashMap源码的时候,发现里面使用了很多简易的写法

代码量少了也导致理解难度变大了,所以梳理了一下,写了一个简单的伪代码,整体思路应该是差不多的。

代码中的table为HashMap中已初始化的node数组,也就是实际储存k-v的数组。

        // 初始化node数组
        Node<K, V>[] tab = (table == null || table.length == 0) ? resize() : table;
        n = tab.length;
        // hash与长度-1进行&运算 得到对象在node数组中的坐标i与node节点p
        int i = (n - 1) & hash;
        Node<K, V> p = tab[i];
        if (p == null) {
            // node节点为空则初始化
            tab[i] = newNode();
        } else {
            // 与链表首节点的hash相等且key相等
            boolean hashAndKeyEquals = p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)));
            if (hashAndKeyEquals) {
                // 替换首节点的value值
            } else if (p instanceof HashMap.TreeNode) {
                // 插入树节点
            } else {
                // 循环链表,若与链表中某个数据的hash相等且key相等则替换value,否则尾插新的k-v数据
                // 尾插后,若链表长度>=8,当node数组长度>=64则重构成树结构,<=32时,resize
            }
        }
        // key-value的数量大于阈值,则resize(),默认阈值为16 * 0.75 = 12
        if (size > threshold) {
            resize();
        }
        // 给子类实现的回调方法,如:linkedHashMap为移除head(第一个)数据
        afterNodeInsertion();

源码本来的样子

    final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        // node数组
        HashMap.Node<K, V>[] tab;
        HashMap.Node<K, V> p;
        int n, i;
        // tab = 已初始化的node数组,若为空则resize()初始化
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        // 将hash值与(长度-1)与运算
        // i为node数组中的位置
        // p为该node节点
        if ((p = tab[i = (n - 1) & hash]) == null)
            // 如果节点为空,则直接new
            tab[i] = newNode(hash, key, value, null);
        else {
            HashMap.Node<K, V> e;
            K k;
            // 如果与首节点的hash值与key相等
            if (p.hash == hash &&
                    ((k = p.key) == key || (key != null && key.equals(k))))
                // e=p后,会在后续方法替换首节点的value
                e = p;
            else if (p instanceof HashMap.TreeNode)
                // 如果为树结构,则插入树节点
                // 和树节点相关的逻辑均在该方法内部
                e = ((HashMap.TreeNode<K, V>) p).putTreeVal(this, tab, hash, key, value);
            else {
                // 循环链表节点
                for (int binCount = 0; ; ++binCount) {
                    // 该方法执行到最后e==null,不会走下一步
                    if ((e = p.next) == null) {
                        // 链表尾插
                        p.next = newNode(hash, key, value, null);
                        // 如果大于等于7(条件为7,方法++binCount结束后后为8)
                        if (binCount >= TREEIFY_THRESHOLD - 1)
                            // 方法内有个逻辑,tab长度如果<64会调用resize()而不是构造树,
                            // 所以实际构造树的条件是 (单node链表长度>=8且node数量>=64)
                            treeifyBin(tab, hash);
                        break;
                    }
                    if (e.hash == hash &&
                            ((k = e.key) == key || (key != null && key.equals(k))))
                        // 如果与链表中某一节点的的hash值与key相等,替换数据后break
                        break;
                    // 循环下一个链表
                    p = e;
                }
            }
            // 替换e指向节点的value值,这个方法提取出来放到上面会更好理解
            if (e != null) {
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        // key-value的数量大于阈值,则resize(),默认阈值为16 * 0.75 = 12
        if (++size > threshold)
            resize();
        // 给子类实现的回调方法,如:linkedHashMap为移除head(第一个)数据
        afterNodeInsertion(evict);
        return null;
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值