數據結構算法 - HashMap 源碼深度解析

  • equals 和 == 的區別,hashCode 與它們之間的聯繫?
  • HashMap 的長度爲什麼是 2 的冪次?
  • 五個線程同時往 HashMap 中 put 數據會發生什麼?
  • ConcurrentHashMap 是怎麼保證線程安全的?

上面是一些常見的面試題,本文旨在分析 HashMap 的源碼實現思想,並不會去細講這些問題,在我們看完源碼之後不妨自己做一些思考。本文也不會細講 JDK 1.8 的紅黑樹和分段鎖,這部分內容等我們分析完二叉樹之後,再來做一些鞏固分析。

我們現在不妨思考一下,假設讓我們自己來設計一個類似 HashMap 的類,我估計大部分能想到的是,用一個數組或者用一個 ArrayList 直接來存放一個 Entry 對象,Entry 對象存放 put 的 key 和 value 。因爲 HashMap 是不允許鍵值重複的,如果我們直接用數組來作爲存儲結構,在不考慮數組擴容的情況下,其查詢和插入的複雜度都是 O(n) 級別的。

HashMap 採用數組 + 鏈表的實現就很好的優化了我們上面所講的問題,大致的原理是當我們 put 一個 key 和 value 時,我們首先會對 key 進行二次 hash 處理, 然後根據 hash 值找到其所在的數組的角標位置,再去遍歷鏈表判斷是否有 key 重複,如果有則覆蓋沒有則添加。這種實現方式在理想的情況下,查詢和添加的時間複雜度是 O(1),請看下面這張圖(長度應該是 2 的冪次):

圖片來源於網絡

上面有一步操作是獲取 key 的 hashCode() , 然後進行二次 hash ,那麼 hashCode() 到底返回的是什麼呢?我們不妨來看看源碼:

    public int hashCode() {
        return identityHashCode(this);
    }

    // Android-changed: add a local helper for identityHashCode.
    // Package-private to be used by j.l.System. We do the implementation here
    // to avoid Object.hashCode doing a clinit check on j.l.System, and also
    // to avoid leaking shadow$_monitor_ outside of this class.
    /* package-private */ static int identityHashCode(Object obj) {
        int lockWord = obj.shadow$_monitor_;
        final int lockWordStateMask = 0xC0000000;  // Top 2 bits.
        final int lockWordStateHash = 0x80000000;  // Top 2 bits are value 2 (kStateHash).
        final int lockWordHashMask = 0x0FFFFFFF;  // Low 28 bits.
        if ((lockWord & lockWordStateMask) == lockWordStateHash) {
            return lockWord & lockWordHashMask;
        }
        return identityHashCodeNative(obj);
    }

    @FastNative
    private static native int identityHashCodeNative(Object obj);

hashCode 最終調用的是 identityHashCodeNative 的 native 方法,之前學的 NDK 現在就可以派上用場了,我們不妨跟蹤到 JNI 層去看看裏面具體的實現,目錄在 jdk\src\share\native\java\lang\Object.c 我們挑一些關鍵代碼:

markOop mark = ReadStableMark (obj);
// 如果當前對象沒有鎖
if (mark->is_neutral()) {
    hash = mark->hash();              // this is a normal header
    if (hash) {                       // if it has hash, just return it
      return hash;
    }
    hash = get_next_hash(Self, obj);  // allocate a new hash code
    temp = mark->copy_set_hash(hash); // merge the hash code into header
    // use (machine word version) atomic operation to install the hash
    test = (markOop) Atomic::cmpxchg_ptr(temp, obj->mark_addr(), mark);
    if (test == mark) {
      return hash;
    }
    // If atomic operation failed, we must inflate the header
    // into heavy weight monitor. We could add more code here
    // for fast path, but it does not worth the complexity.
  } else if (mark->has_monitor()) {
    monitor = mark->monitor();
    temp = monitor->header();
    assert (temp->is_neutral(), "invariant") ;
    hash = temp->hash();
    if (hash) {
      return hash;
    }
    // Skip to the following code to reduce code size
  } else if (Self->is_lock_owned((address)mark->locker())) {
    //如果重入
    temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned
    assert (temp->is_neutral(), "invariant") ;
    hash = temp->hash();              // by current thread, check if the displaced
    if (hash) {                       // header contains hash code
      return hash;
    }
    // WARNING:
    //   The displaced header is strictly immutable.
    // It can NOT be changed in ANY cases. So we have
    // to inflate the header into heavyweight monitor
    // even the current thread owns the lock. The reason
    // is the BasicLock (stack slot) will be asynchronously
    // read by other threads during the inflate() function.
    // Any change to stack may not propagate to other threads
    // correctly.
  }

  // Inflate the monitor to set hash code
  monitor = ObjectSynchronizer::inflate(Self, obj);
  // Load displaced header and check it has hash code
  mark = monitor->header();
  assert (mark->is_neutral(), "invariant") ;
  hash = mark->hash();
  if (hash == 0) {
    hash = get_next_hash(Self, obj);
    temp = mark->copy_set_hash(hash); // merge hash code into header
    assert (temp->is_neutral(), "invariant") ;
    test = (markOop) Atomic::cmpxchg_ptr(temp, monitor, mark);
    if (test != mark) {
      // The only update to the header in the monitor (outside GC)
      // is install the hash code. If someone add new usage of
      // displaced header, please update this code
      hash = test->hash();
      assert (test->is_neutral(), "invariant") ;
      assert (hash != 0, "Trivial unexpected object/monitor header usage.");
    }
  }
  // We finally get the hash
  return hash;

  // hash operations
  intptr_t hash() const {
    // value() 是地址
    // age_bits                 = 4,
    // cms_shift                = age_shift + age_bits,
    // hash_shift               = cms_shift + cms_bits,
    // if 64 位
    // hash_mask = right_n_bits(hash_bits);
    return mask_bits(value() >> hash_shift, hash_mask);
  }

看不懂先不必去深究,根據源碼我們就能發現,hashCode() 返回的並不是地址那麼簡單,而是經過了一系列的計算得到的,有兩個概念我們需要了解:兩個不同的對象 hashCode 值可能會相等,hashCode 值不相等的兩個對象肯定不是同一對象。

簡單分析最後兩行關鍵代碼,在 C++ 中兩個不同的對象的地址肯定是不同的,但是 value() >> 16 就有可能相等,源碼原理就這麼簡單。接下來我們回到我們的重點去分析 HashMap 的源碼(JDK 1.7)

    // 確保數組 tab 的長度是 2 的冪次,上次已講,不再解釋
    static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }

    public V put(K key, V value) {
        // 如果是空的創建一個新的數組
        if (table == EMPTY_TABLE) {
            inflateTable(threshold);
        }
        // 如果 key 是 null ,單獨存放
        if (key == null)
            return putForNullKey(value);
        // 二次 hash 獲取 hash 值
        int hash = hash(key);
        // 獲取應當存放的 tab 位置
        int i = indexFor(hash, table.length);
        // 遍歷 i 上面的鏈表看有沒有存在,如果有覆蓋
        for (HashMapEntry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }
        // key 不存在添加新的
        modCount++;
        addEntry(hash, key, value, i);
        return null;
    }
    
    void addEntry(int hash, K key, V value, int bucketIndex) {
        // 是否需要擴容
        if ((size >= threshold) && (null != table[bucketIndex])) {
            resize(2 * table.length);
            hash = (null != key) ? hash(key) : 0;
            bucketIndex = indexFor(hash, table.length);
        }
        // 創建新的節點
        createEntry(hash, key, value, bucketIndex);
    }
    
    void resize(int newCapacity) {
        HashMapEntry[] oldTable = table;
        int oldCapacity = oldTable.length;
        if (oldCapacity == MAXIMUM_CAPACITY) {
            threshold = Integer.MAX_VALUE;
            return;
        }

        HashMapEntry[] newTable = new HashMapEntry[newCapacity];
        transfer(newTable);
        table = newTable;
        threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
    }    
    
    // 擴容需要重新擺放裏面的所有元素(最耗時的一個操作)
    void transfer(HashMapEntry[] newTable) {
        int newCapacity = newTable.length;
        for (HashMapEntry<K,V> e : table) {
            while(null != e) {
                HashMapEntry<K,V> next = e.next;
                int i = indexFor(e.hash, newCapacity);
                e.next = newTable[i];
                newTable[i] = e;
                e = next;
            }
        }
    }

    void createEntry(int hash, K key, V value, int bucketIndex) {
        // 添加在列表最前面
        HashMapEntry<K,V> e = table[bucketIndex];
        table[bucketIndex] = new HashMapEntry<>(hash, key, value, e);
        size++;
    }
    
    
    final Entry<K,V> getEntry(Object key) {
        if (size == 0) {
            return null;
        }

        int hash = (key == null) ? 0 : hash(key);
        // 獲取所在的位置,遍歷循環返回
        for (HashMapEntry<K,V> e = table[indexFor(hash, table.length)];
             e != null;
             e = e.next) {
            Object k;
            if (e.hash == hash &&
                ((k = e.key) == key || (key != null && key.equals(k))))
                return e;
        }
        return null;
    }

瞭解了思想,源碼是非常容易理解的,至於 JDK 1.8 的紅黑樹知識,我們必須先要了解二叉樹;ConcurrentHashMap 分段鎖我們都會在後面的文章陸陸續續的做一些分析。

視頻鏈接:https://pan.baidu.com/s/1dYd-4UG0VY1UhezE1p9s9A
視頻密碼:b8zd

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