HashMap的putVal函數源碼解析

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章