1.8 HashMap源码阅读带详细注释(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>.)
     */
    public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);  //处理hash值后调putVal插入
    }

    /**
     * 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) {
        HashMap.Node<K,V>[] tab; HashMap.Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)       //hash表是否为空
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)              //put的key是否已经在hash表数组部分中存在
            tab[i] = newNode(hash, key, value, null);           //不存在加到hash表的数组部分(也就是链表的头节点)
        else {                                                  //否则就是已存在,已存在分为下面3种情况
            HashMap.Node<K,V> e; K k;
            if (p.hash == hash &&
                    ((k = p.key) == key || (key != null && key.equals(k))))   // 1、put的key与hash表数组部分(链表的头节点)的已存在的key一样,后面将替换成新的value值
                e = p;
            else if (p instanceof HashMap.TreeNode)                          //2、不是hash表数组部分上的key,那就是以hash表数组部分上node为头节点的链表上或者红黑树上的节点,这里判断是否是树节点
                e = ((HashMap.TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);   //返回值写死的是null
            else {                                                          //3、否则,就是链表上的节点了
                for (int binCount = 0; ; ++binCount) {                     //遍历链表,插到链表后面
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);         //是否到了链尾(p指向当前判断的节点,e指向当前判断节点的下一个节点)
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st       // 链表的节点数是否 >=7 了
                            treeifyBin(tab, hash);                        //将链表树化成红黑树
                        break;
                    }
                    if (e.hash == hash &&
                            ((k = e.key) == key || (key != null && key.equals(k))))   //判断put的key是否已经在整个链表中是否存在
                        break;
                    p = e;                                           //后移
                }
            }
            if (e != null) { // existing mapping for key       //如果就整个链表来说(包含头节点),如果有重复的key出现,那么e就不为null
                V oldValue = e.value;                         //这里也就是上面1、说的value值将被相同key的新value值替换掉
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;        //hash表结构变化加1
        if (++size > threshold)     //集合当前插入的数量是否大于   当前容量*0.75  的阈值
            resize();            //容量翻倍扩容
        afterNodeInsertion(evict);    //允许LinkedHashMap后动作的回调
        return null;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值