HashMap的putVal函数源码解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26346457/article/details/83146121
    /**
     * The default initial capacity - MUST be a power of two.
     */
    // 默认容量16
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

    /**
     * The load factor used when none specified in constructor.
     */
    // 默认加载因子
    // 如果size大于加载因子*容量,将扩容resize()
    static final float DEFAULT_LOAD_FACTOR = 0.75f;

put调用的是putVal

    /**
     * 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);
    }

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) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        // 判断HashMap的Node数组是否为空
        if ((tab = table) == null || (n = tab.length) == 0)
            // 如果为空,则初始化数组
            n = (tab = resize()).length;
        // 判断HashMap的Node数组的hash位置是否为空
        if ((p = tab[i = (n - 1) & hash]) == null)
            // 如果为空直接插入一个节点
            tab[i] = newNode(hash, key, value, null);
        // 如果不为空
        else {
            Node<K,V> e; K k;
            // 当前Node的Key和新插入的Key是否相等
            if (p.hash == hash &&
                    ((k = p.key) == key || (key != null && key.equals(k))))
                // 直接覆盖
                e = p;
            // 当前Node是否为红黑树的TreeNode
            else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            // 当前Node是否为单向链表的Node
            else {
                // 遍历单向链表
                for (int binCount = 0; ; ++binCount) {
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        // 如果哈希冲突(哈希碰撞)的数量大于等于8,将单向链表转换为红黑树
                        // 当执行treeifyBin(tab, hash);的时候,也不一定必须转换成红黑树
                        // 如果一个Node的单向链表的长度小于64,扩容
                        // 如果一个Node的单向链表的长度大于等于64,才将此单向链表转换成红黑树
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    // 当前Node的Key和新插入的Key是否相等
                    if (e.hash == hash &&
                            ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            // 判断新插入的Node是否已存在,如果已存在根据onlyIfAbsent是否用新值覆盖旧值
            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;
    }

猜你喜欢

转载自blog.csdn.net/qq_26346457/article/details/83146121
今日推荐