JDK1.8——HashMap,ConCurrentHashMap——put(),get()源码解读

博客主要对HashMap的put和get方法进行解读,作者因有事暂时写到这里,并表示明天会写ConCurrentHashMap的相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HashMap——put解读

public V put(K key, V value) {
        // 对key进行hash取值传入putVal()方法
        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
     */
    final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        // 如果存放Node的数组是空的,将会对数组进行初始化
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        // 如果数组对应key的hash值位置为空,将会new 一个新的Node对象,该对象相当于JDK1.7的Entry对象
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            // 当数组对应key的hash值位置不为空进入else
            Node<K,V> e; K k;
            // 直接对数组上对应位置的Node取key和传进来的key做比较,如果相等直接将当前Node赋值给e,在后面将返回e里面的OldValue
            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循环去遍历链表
                for (int binCount = 0; ; ++binCount) {
                    //如果链表上的元素没到8个,会直接将新加入的值放到链表的next位置,此处与1.7不同,因为已经遍历了所有,
                    // 所以此处放在链表最后,恰当好处
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        // TREEIFY_THRESHOLD为8,当链表上的元素>=8个时,会将链表转换成树
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    // 此处是对p进行复制方便下一次循环来取p的next
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            // 在想数组上的链表插入数据完成后,此处将会将OldValue返回回去
            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;
    }

HashMap——get解读

public V get(Object key) {
        Node<K,V> e;
        // 通过getNode获取到链表上的Node,如果获取的值为null,则返回null,如果不为null,返回value
        return (e = getNode(hash(key), key)) == null ? null : e.value;
    }

    /**
     * Implements Map.get and related methods
     *
     * @param hash hash for key
     * @param key the key
     * @return the node, or null if none
     */
    final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
        // 首先数组和数组上的链表上的第一个Node判定是否为null,是null则返回null
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {
            // 对链表上的第一个Node进行判断是不是我们要取的Node,是就返回first
            if (first.hash == hash && // always check first node
                ((k = first.key) == key || (key != null && key.equals(k))))
                return first;
            //循环链表
            if ((e = first.next) != null) {
                //如果链表为红黑树通过getTreeNode()来取Node
                if (first instanceof TreeNode)
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                do {
                    //遍历进行比较,知道找出key相等的再返回
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                    // 循环条件,并对e赋值
                } while ((e = e.next) != null);
            }
        }
        // 以上都没找到或者为null,返回null
        return null;
    }

今天还有事先写到这儿, ConCurrentHashMap的明天写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值