hashMap get put resize方法源码解析

hashMap源码学习

简单介绍一下hashMap,hashMap的顶级父类接口为Map为key-value存贮,在在根据key查找单个元素时时间复杂度为ON(1),但是不能保证元素顺序,即元素存进去和取出来的顺序不一致,在jdk1.7采用数组+链表实现线程不安全,但是在大量存贮元素时可能会出现某种极端情况,链表过长(或元素全部存贮到一条链表上),查找元素变慢;在jdk1.8时为了解决这个问题,hashMap底层使用了数组+链表+红黑树的方式实现,当链表元素过长时jdk将会把链表转化为红黑树来增加查找速率,但1.8的hashMap仍然不是线程安全的。

为什么jdk1.8采用红黑树而不采用其他的树:

而二叉树在有种极端情况,所有元素全部存储到树的一边上,这样的话树在查找某个元素时就和链表类似,没有体现出树的优势,而二叉树是一种平衡二叉树,树的左右子树高度相差不超过一,超过时通过左旋或右旋调整。

参考https://juejin.cn/post/6844903519632228365#comment

下面我们来看一下具体jdk1.8底层是怎么实现的:

静态变量

静态变量来定制hashMpa中一些规范如:初始容量、扩容阈值等


    /**
     * The default initial capacity - MUST be a power of two.
     * 默认初始容量-必须是2的幂。
     * 初始容量
     */
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
    /**
     * The maximum capacity, used if a higher value is implicitly specified
     * by either of the constructors with arguments.
     * MUST be a power of two <= 1<<30.
     * 如果隐式指定了更高的值,则使用最大容量由带有参数的构造函数之一执行。
     * 必须是2的幂<=1<<30。
     * HashMap的最大容量
     */
    static final int MAXIMUM_CAPACITY = 1 << 30;
    /**
     * The load factor used when none specified in constructor.
     * 构造函数中未指定时使用的负载系数
     * 容量到达容器的0.75时HashMap将会进行扩容操作
     */
    static final float DEFAULT_LOAD_FACTOR = 0.75f;
    /**
     * The bin count threshold for using a tree rather than list for a
     * bin.  Bins are converted to trees when adding an element to a
     * bin with at least this many nodes. The value must be greater
     * than 2 and should be at least 8 to mesh with assumptions in
     * tree removal about conversion back to plain bins upon
     * shrinkage.
     * 对于bin使用树而不是列表的bin计数阈值。将元素添加到具有至少这么多节点的bin时,
     * bin将转换为树。该值必须大于2,并且至少应为8,
     * 以便与树木移除中关于收缩时转换回普通箱的假设相匹配
     * 
     * 当链表长度超过8时HashMap会将链表转换为红黑树
     * HashMap会优先将数组扩容到64时才会进行红黑树的转化
     */
    static final int TREEIFY_THRESHOLD = 8;
    /**
     * The bin count threshold for untreeifying a (split) bin during a
     * resize operation. Should be less than TREEIFY_THRESHOLD, and at
     * most 6 to mesh with shrinkage detection under removal.
     * 在调整大小操作过程中取消筛选(拆分)垃圾箱的垃圾箱计数阈值。
     * 应小于TREEIFY_THRESHOLD,最多为6,以便在移除时进行收缩检测
     * 
     */
    static final int UNTREEIFY_THRESHOLD = 6;
    /**
     * The smallest table capacity for which bins may be treeified.
     * (Otherwise the table is resized if too many nodes in a bin.)
     * Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts
     * between resizing and treeification thresholds.
	 * 可将箱子树化的最小工作台容量。
	 *(否则,如果bin中的节点过多,将调整表的大小。)
	 * 应至少为4*TREEIFY_THRESHOLD以避免冲突
	 * 在调整大小和树化阈值之间。
	 * 
	 * 当数组长度到达64时且链表长度大于等于8时链表转换为红黑树
     */
    static final int MIN_TREEIFY_CAPACITY = 64;

构造方法及相关属性


	//初始无参构造
	//Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).
	//使用默认初始容量(16)和默认负载因子(0.75)构建一个空的HashMap
    public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    }
    /**
     * Constructs an empty <tt>HashMap</tt> with the specified initial
     * capacity and the default load factor (0.75).
     *
     * @param  initialCapacity the initial capacity.
     * @throws IllegalArgumentException if the initial capacity is negative.
     */
	//使用指定的初始容量和默认负载因子(0.75)构造空HashMap。 
	//参数: initialCapacity–初始容量。 
	//抛出: IllegalArgumentException–如果初始容量为负数
	//构造一个含有初始容量的HashMap
    public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }
    /**
     * Constructs an empty <tt>HashMap</tt> with the specified initial
     * capacity and load factor.
     *
     * @param  initialCapacity the initial capacity 初始化容量
     * @param  loadFactor      the load factor      负载因子
     * @throws IllegalArgumentException if the initial capacity is negative
     *         or the load factor is nonpositive
     */
	//使用外部传入的HashMap的初始化容量和负载因子
    public HashMap(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0) //判断外部传入的初始化容量是否小于0,小于0则抛出异常
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        if (initialCapacity > MAXIMUM_CAPACITY) //判断初始化容量是否超过最大容量
            initialCapacity = MAXIMUM_CAPACITY; //如果大于最大容量则使用最大容量
        if (loadFactor <= 0 || Float.isNaN(loadFactor)) //判断负载因子是否小于0或者不是数字
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor); //抛出异常
        this.loadFactor = loadFactor;     			    //负载因子赋值
        this.threshold = tableSizeFor(initialCapacity); //将初始化容量先赋给容器界限
    }
	//Returns a power of two size for the given target capacity.
	//返回给定目标容量的两次幂。
	//得到一个大于获等于他的2的幂并返回
	static final int tableSizeFor(int cap) {
        // >>>为无符号右移 将数字转换为二进制向右移动 高位补0
        // |为或运算,两个数转化为二进制,有1则为1,全0则为0
        // |= 与+=类似 a|=b 等价于 a=a|b
        //防止传进来的数本身就是2^n,如果不减一则传出的值为2^n+1
        int n = cap - 1;
        //右移一位并与自己进行或运算,则原来是1则变成11。
        //例如传进来数为100001001,右移一位得到0110000100,与自己进行或运算得到110001101
        //一位1变成两位1,所以下面各右移1、2、4、8、16位
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }

get相关方法和属性


    /**
     * Returns the value to which the specified key is mapped,
     * or {@code null} if this map contains no mapping for the key.
     *
     * <p>More formally, if this map contains a mapping from a key
     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
     * key.equals(k))}, then this method returns {@code v}; otherwise
     * it returns {@code null}.  (There can be at most one such mapping.)
     *
     * <p>A return value of {@code null} does not <i>necessarily</i>
     * indicate that the map contains no mapping for the key; it's also
     * possible that the map explicitly maps the key to {@code null}.
     * The {@link #containsKey containsKey} operation may be used to
     * distinguish these two cases.
     *
     * @see #put(Object, Object)
     */
	//返回指定键映射到的值,如果此映射不包含该键的映射,则返回null。 
	//更正式地说,如果这个映射包含从键k到值v的映射,使得(key==null k==null:key.equals(k))那么这个方法返回v;否则返回null。(最多可以有一个这样的映射。)
	//返回值null不一定表示映射不包含键的映射;映射也可能将键显式映射为null。containsKey操作可用于区分这两种情况。 请参阅: put(对象,对象
	//在HashMpa中可以存贮最多一个value值为null的键值对,但是如果在HashMap中没有找到对应键的时候也会返回null,可以使用containsKey方法来判断此键是否存在于HashMap中
    public V get(Object key) {
        Node<K,V> e;
        return (e = getNode(hash(key), key)) == null ? null : e.value;
    }
    /**
     * Computes key.hashCode() and spreads (XORs) higher bits of hash
     * to lower.  Because the table uses power-of-two masking, sets of
     * hashes that vary only in bits above the current mask will
     * always collide. (Among known examples are sets of Float keys
     * holding consecutive whole numbers in small tables.)  So we
     * apply a transform that spreads the impact of higher bits
     * downward. There is a tradeoff between speed, utility, and
     * quality of bit-spreading. Because many common sets of hashes
     * are already reasonably distributed (so don't benefit from
     * spreading), and because we use trees to handle large sets of
     * collisions in bins, we just XOR some shifted bits in the
     * cheapest possible way to reduce systematic lossage, as well as
     * to incorporate impact of the highest bits that would otherwise
     * never be used in index calculations because of table bounds.
     */
	//计算键。hashCode()并将哈希的高位扩展(XOR)到低位。因为该表使用两个掩码的幂,所以仅在当前掩码之上的位中变化的哈希集将始终发生冲突。(在已知的例子中,有一组浮点键在小表中保持连续的整数。)因此,我们应用了一种将高位的影响向下扩散的变换。比特扩展的速度、效用和质量之间存在权衡。因为许多常见的散列集已经被合理地分布(因此不会从扩展中受益),并且因为我们使用树来处理bin中的大量冲突,我们只需以最便宜的方式对一些移位的位进行异或,以减少系统损失,并合并由于表边界而永远不会用于索引计算的最高位的影响。
	// 获取键的hash值,HashMap中键的hash值都是经过处理的,使得平均分布在数组上而不会集中分布在某一位置,jdk1.8使用的是高低位异或(16位)
    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }
    /**
     * Implements Map.get and related methods.
     *
     * @param hash hash for key
     * @param key the key
     * @return the node, or null if none
     */
	//实施映射。get和相关方法。 参数: hash–密钥的哈希 key–钥匙 返回值: 节点,如果没有,则为null
	// hash为键的hash值,key为外部传入的键
	// 因为初始获得的Hash的值太长了无法直接在数组中使用,所以HashMap使用HashMap使用hash确定元素下标的方式为:hash值与数组长度取余,又因为 任何数 & 2的n次幂 -1 = 任何数 % 2的n次幂,而&运算要比%快,所以HashMap确定数组下标的公式为:hash & n-1,也所以HashMpa中数组长度也必须为2的n次幂.
	//
    final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
        if ((tab = table) != null && (n = tab.length) > 0 && //判断HashMpa是否为空
            (first = tab[(n - 1) & hash]) != null) {		 //为空则直接返回null
            //找到对应数组下标下的链表,判断头节点是否就是要找的元素,是则直接返回头节点
            if (first.hash == hash && //always check first node
                ((k = first.key) == key || (key != null && key.equals(k))))
                return first;
            //判断链表中是否只有一个元素,是则直接返回null,否则继续向下查询
            if ((e = first.next) != null) { 
                // 判断是否为红黑树,是则使用红黑树方式查找
                if (first instanceof TreeNode)
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key); //不会嘿嘿
                // 遍历链表,并挨个对比是否是要找的元素
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
    }

put添加相关方法和属性


    /**
     * Associates the specified value with the specified key in this map.
     * If the map previously contained a mapping for the key, the old
     * value is replaced.
     *
     * @param key key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with <tt>key</tt>, or
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
     *         (A <tt>null</tt> return can also indicate that the map
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
     */
	//将指定值与此映射中的指定键相关联。如果映射之前包含键的映射,则替换旧值。 
	//参数: key–与指定值关联的键 value–与指定键关联的值 
	//返回值: 与键关联的前一个值,如果没有键的映射,则为null。(null返回还可以指示映射先前将null与键关联。)
    public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }
    /**
     * Implements Map.put and related methods.
     *
     * @param hash hash for key
     * @param key the key
     * @param value the value to put
     * @param onlyIfAbsent if true, don't change existing value
     * @param evict if false, the table is in creation mode.
     * @return previous value, or null if none
     */
	//实施映射。put和相关方法。 
	//参数: hash–密钥的哈希 key–钥匙 value–要放置的值 onlyIfAbsent–如果为true,则不更改现有值 evict-如果为false,则表处于创建模式。 
	//返回值: 上一个值,如果没有则为null
    final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        //检查HashMap中是否为空,为空则使用resize初始化,并给n赋值
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        //检查数组下标处是否为空,是则直接赋值((n - 1) & hash 为下标)
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;
            //检查数组下标处key是否与新值重复,是则e的值为 key
            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 {
                // 循环遍历链表,如果找到相同key则替换,没有找到则插入到链表最后并检查是否超过8,是则将链表转化为红黑树
                for (int binCount = 0; ; ++binCount) {
                    // 检查p是否为最后一个元素,此时结束循环e为 null
                    if ((e = p.next) == null) {
                        //新元素尾插到末尾
                        p.next = newNode(hash, key, value, null);
                        //检查链表长度是否超过八,是则转化为红黑树,
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            //构造树的具体方法
                            treeifyBin(tab, hash);
                        break;
                    }
                    // 检查元素是否与新元素相同,是则退出循环,此时结束循环e值为 key
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            // 这句话的意思其实是判断上面的循环走完了吗?如果走完链表则e应该是null,如果e不为空则表示新值还没有被替换,再这里替换
            if (e != null) { // existing mapping for key
                // 将e的值赋给oldValue做中转
                V oldValue = e.value;
               	// 判断条件是 是否改变现有值 新值是否为null
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                // 返回替换前的值
                return oldValue;
            }
        }
        ++modCount;
        // 判断是否到达扩容界限 threshold = 容量 * 负载因子(默认0.75)
        if (++size > threshold)
            // 扩容
            resize();
        afterNodeInsertion(evict);
        return null;
    }
    /**
     * Initializes or doubles table size.  If null, allocates in
     * accord with initial capacity target held in field threshold.
     * Otherwise, because we are using power-of-two expansion, the
     * elements from each bin must either stay at same index, or move
     * with a power of two offset in the new table.
     *
     * @return the table
     */
	//初始化或加倍表大小。如果为空,则根据字段阈值中保持的初始容量目标进行分配。
	//因为我们使用的是二次幂展开,所以每个bin中的元素必须保持在同一索引中,或者在新表中以二次幂偏移量移动。
	//返回值: table
	//HashMpa的扩容方法
	final Node<K,V>[] resize() {
        Node<K,V>[] oldTab = table;
        // 判断HahsMap是否为空给oldCap(旧容量)赋值
        int oldCap = (oldTab == null) ? 0 : oldTab.length;
        // 旧的容量界限
        int oldThr = threshold;
        // 新的容量和新的容量界限
        int newCap, newThr = 0;
        // 判断是否HashMap第一次初始化
        if (oldCap > 0) {
            // 判断旧容量是否大于等于最大,是则提升界限,并将容器直接返回
            if (oldCap >= MAXIMUM_CAPACITY) {
                // 将界限提升到2的31次方 - 1 
                threshold = Integer.MAX_VALUE;
                // 返回原容器
                return oldTab;
            }
            //判断旧容量扩大2倍后是否超过最大容量,判断旧容量界限是否大于等于初始容量(保证不是第一次初始化容器)
            // << 为有符号左移 每移动一位数字扩大2倍
            else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
                     oldCap >= DEFAULT_INITIAL_CAPACITY)
                // 新容器界限等于旧容器界限扩大二倍
                newThr = oldThr << 1; // double threshold
        }
        // 构造函数外部传入初始容量或负载因子,将之前赋给容量界限的值赋给新容量
        else if (oldThr > 0) // initial capacity was placed in threshold
            newCap = oldThr;
        else {               // zero initial threshold signifies using defaults
            // oldCap = 0 且 oldThr <=0 就是容器第一次初始化(即无参构造生成)
            // 将初始(默认)容量赋给新容量
            newCap = DEFAULT_INITIAL_CAPACITY;
             // 默认界限 = 默认容量 * 默认负载因子 = 16 * 0.75
            newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
        }
        // 针对构造函数外部传入初始容量或负载因子情况,
        //将界限赋值为:新容量 * 外部传入负载因子(新容量也是外部传入且经过处理的 tableSizeFor方法)
        if (newThr == 0) {
            //新容量界限 = 容量 * 负载因子
            float ft = (float)newCap * loadFactor;
            //判断是否超过最大容量
            newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
                      (int)ft : Integer.MAX_VALUE);
        }
        // 将处理好的界限赋值给threshold
        threshold = newThr;
        @SuppressWarnings({"rawtypes","unchecked"})
        //初始化node数组
        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;
                    //判断链表是否只有头结点,是则把此节点放在扩容后下标处即hash&新容量-1
                    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;
                        //循环链表,并转移
                        // e.hash & oldCap 根据这个条件来判断改元素在新数组中的偏移量,只有两种情况:当e.hash & oldCap = 0时则 e.hash & newCap-1 = e.hash & oldCap-1即该元素在新数组中下标不变;当e.hash & oldCap != 0时则 e.hash & newCap-1 = (e.hash & oldCap-1)+ oldCap即该元素在新数组下标等于 原下标 + 旧数组容量
                        // 参考https://blog.csdn.net/u010425839/article/details/106620440#comments_23442640
                        do {
                            // 循环链表
                            next = e.next;
                            // 判断下标偏移量,为0则不偏移,不为零则偏移oldCap(原下标+oldCap)
                            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;
    }

remove删除方法


    /**
     * Removes the mapping for the specified key from this map if present.
     *
     * @param  key key whose mapping is to be removed from the map
     * @return the previous value associated with <tt>key</tt>, or
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
     *         (A <tt>null</tt> return can also indicate that the map
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
     */
	//从该映射中删除指定键的映射(如果存在)。
	//参数: key–要从映射中删除其映射的键 
	//返回值: 与键关联的前一个值,如果没有键的映射,则为null。(null返回还可以指示映射先前将null与键关联。)
    public V remove(Object key) {
        Node<K,V> e;
        return (e = removeNode(hash(key), key, null, false, true)) == null ?
            null : e.value;
    }
	//实施映射。移除和相关方法。 
	//参数: hash–密钥的哈希 key–钥匙 
	//value 如果matchValue为ture要对比的值,为matchValue为false则忽略
	//matchValue 如果为true,则仅在值相等时删除
	//可移动–如果为false,则在删除时不要移动其他节点 
	//返回值: 节点,如果没有,则为null
    final Node<K,V> removeNode(int hash, Object key, Object value,
                               boolean matchValue, boolean movable) {
        Node<K,V>[] tab; Node<K,V> p; int n, index;
        // 判断容器是否为空或者数组对应下标处没有元素,并将元素所对数组下标处元素赋值给p
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (p = tab[index = (n - 1) & hash]) != null) {
            // node就是目标要删除的元素
            Node<K,V> node = null, e; K k; V v;
            // 判断要删除的元素是否就是头节点,并赋值给node
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                node = p;
            // 判断数组下标处是否只有头节点,是则表明链表只有一个元素且不是要删除的元素
            else if ((e = p.next) != null) {
                // 判断此时是不是红黑树
                if (p instanceof TreeNode)
                   	// 使用红黑树方式查找到要删除的元素并赋值给node
                    node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
                else {
                    // 遍历链表并找到要删除的元素赋值给node
                    do {
                        // 检查是否当前元素是否是要删除的元素
                        if (e.hash == hash &&
                            ((k = e.key) == key ||
                             (key != null && key.equals(k)))) {
                            // 赋值给node并退出循环
                            node = e;
                            break;
                        }
                        p = e;
                    } while ((e = e.next) != null);
                }
            }
            // 判断容器是否包含要删除的元素,判断是否采用值相等时才删除的模式,判断外部传入的值是否与容器内值相等
            if (node != null && (!matchValue || (v = node.value) == value ||
                                 (value != null && value.equals(v)))) {
                // 判断是否是红黑树,是则采用红黑树方式删除
                if (node instanceof TreeNode)
                    ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
                // 判断是否为链表头节点
                else if (node == p)
                    tab[index] = node.next;
                // 链表普通元素,直接删除
                else
                    p.next = node.next;
                ++modCount;
                --size;
                afterNodeRemoval(node);
                // 返回删除的元素
                return node;
            }
        }
        return null;
    }

参考:
https://pdai.tech/md/java/collection/java-map-HashMap&HashSet.html
https://blog.csdn.net/u010425839/article/details/106620440#comments_23442640
https://juejin.cn/post/6844903519632228365#comment

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。