hashMap怎么添加元素的


HashMap的存取過程,當執行putVal的操作的時候,

1.首先檢查大小,看是否需要擴容(默認元素超過最大值的0.75時擴容),如果需要擴容就進行擴容

2.然后計算出key的hashcode,根據hashcode定位數值所在的bucketIndex

3.如果該位置上沒有元素,就直接插入,結束

4.如果該位置上有元素就使用equal比較是否相同

5.如果key相同就把新的value替換舊的value,結束

6.如果key不同,就繼續遍歷,找到根節點,如果沒找到key的話,就構造一個新的節點,然后把節點插入到鏈表尾部,表示put成功(jdk 1.8 之后鏈表長度超過閾值就會轉化為紅黑樹)

 

 HashMap JDK1.8添加元素的部分源碼如下:

    public V put(K key, V value) {
        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 為 true 時不改變已經存在的值
     * @param 為 false 時表示哈希表正在創建
     * @return previous value, or null if none
     */
    final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        /**
         * tab:哈希表數組
         * p:桶位置上的頭節點
         * n:哈希表數組大小
         * i:下標(槽位置)
         */
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        // 當哈希表數組為 null 或者長度為 0 時,初始化哈希表數組
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        // 頭節點為空直接插入(無哈希碰撞)
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        // 哈希碰撞
        else {
            Node<K,V> e; K k;
            // 與頭節點發生哈希沖突,進行記錄
            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 (int binCount = 0; ; ++binCount) {
                    // 在鏈表尾部插入新節點,注意 jdk1.8 中在鏈表尾部插入新節點
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        // 如果當前鏈表中的元素大於樹化的閾值,進行鏈表轉樹的操作
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    // 如果 key(非頭節點)已經存在,直接結束循環
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    // 重置 p 用於遍歷
                    p = e;
                }
            }
            // 如果 key 已經存在則更新 value 值
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                // 更新當前 key 值
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        // 如果鍵值對個數大於閾值時(capacity * load factor),進行擴容操作
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM