天天看點

HashMap源碼解讀(二) putVal方法

/**
     *
     * @param key 需要存入數組中的元素鍵
     * @param value 需要存入數組中的元素值
     * @return 傳回被存入的元素值
     */
    public V put(K key, V value) {
        return this.putVal(hash(key), key, value, false, true);
    }

    final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {
        //臨時變量table用于存儲Hashtable中的table元素數組
        HashMap.Node[] tab;
        //table數組的長度
        int n;
        //當數組沒有被初始化時
        if ((tab = this.table) == null || (n = tab.length) == 0) {
            //第一次調用resize函數将數組初始化,并傳回table的長度
            n = (tab = this.resize()).length;
        }
        //當數組已經被初始化
        Object p;
        int i;
        //如果目前元素的hash位置上不存在元素
        //則将該元素構造為node對象直接存入table的目前位置
        if ((p = tab[i = n - 1 & hash]) == null) {
            tab[i] = this.newNode(hash, key, value, (HashMap.Node)null);
        } else {
            //如果目前位置存在元素,則表示發生hash沖突
            Object e;
            Object k;
            //如果目前元素的hash值與key值均與需要被存入的元素相同,則直接傳回該元素
            if (((HashMap.Node)p).hash == hash && ((k = ((HashMap.Node)p).key) == key || key != null && key.equals(k))) {
                e = p;
            }
            //目前table位置為紅黑樹,則以紅黑樹的方式插入元素.
            else if (p instanceof HashMap.TreeNode) {
                e = ((HashMap.TreeNode)p).putTreeVal(this, tab, hash, key, value);
            }
            //目前table位置為連結清單.
            else {
                //臨時變量儲存該連結清單的長度
                int binCount = 0;
                //循環後移
                while(true) {
                    //目前元素是尾元素
                    if ((e = ((HashMap.Node)p).next) == null) {
                        //将該元素插傳入連結表尾部
                        ((HashMap.Node)p).next = this.newNode(hash, key, value, (HashMap.Node)null);
                        //當該連結清單長度大于8時,将連結清單樹化
                        if (binCount >= 7) {
                            this.treeifyBin(tab, hash);
                        }
                        break;
                    }
                    //當循環途中發現了與需要插入的元素相同的元素,直接退出循環,并傳回目前元素
                    if (((HashMap.Node)e).hash == hash && ((k = ((HashMap.Node)e).key) == key || key != null && key.equals(k))) {
                        break;
                    }

                    p = e;
                    //将連結清單的元素個數自增,用于判斷是否需要樹化
                    ++binCount;
                }
            }
            //當查找到元素時,傳回查找到的元素
            if (e != null) {
                //擷取強轉後的value值,并傳回
                V oldValue = ((HashMap.Node)e).value;
                //如果原來的value不為空,則覆寫原來的value
                if (!onlyIfAbsent || oldValue == null) {
                    ((HashMap.Node)e).value = value;
                }
                this.afterNodeAccess((HashMap.Node)e);
                return oldValue;
            }
        }
        //将table數組儲存的元素個數自增
        ++this.modCount;
        //當元素個數大于最大負載容量時,重新将table初始化,配置設定元素
        if (++this.size > this.threshold) {
            this.resize();
        }

        this.afterNodeInsertion(evict);
        return null;
    }