JAVA常用數據結構

  • // Additionally, if the table array has not been allocated, this
  • // field holds the initial array capacity, or zero signifying
  • // DEFAULT_INITIAL_CAPACITY.)
  • int threshold;
  • table屬性即為分桶。

    size屬性表示HashMap中存儲的Key-Value對兒的個數。

    threshold為判斷是否需要進行擴容的門限值。

    構造函數


    1. /**
    2. * Constructs an empty HashMap with the specified initial
    3. * capacity and load factor.
    4. *
    5. * @param initialCapacity the initial capacity
    6. * @param loadFactor the load factor
    7. * @throws IllegalArgumentException if the initial capacity is negative
    8. * or the load factor is nonpositive
    9. */
    10. public HashMap(int initialCapacity, float loadFactor) {
    11. if (initialCapacity < 0)
    12. throw new IllegalArgumentException("Illegal initial capacity: " +
    13. initialCapacity);
    14. if (initialCapacity > MAXIMUM_CAPACITY)
    15. initialCapacity = MAXIMUM_CAPACITY;
    16. if (loadFactor <= 0 || Float.isNaN(loadFactor))
    17. throw new IllegalArgumentException("Illegal load factor: " +
    18. loadFactor);
    19. this.loadFactor = loadFactor;
    20. this.threshold = tableSizeFor(initialCapacity);
    21. }
    22. /**
    23. * Constructs an empty HashMap with the specified initial
    24. * capacity and the default load factor (0.75).
    25. *
    26. * @param initialCapacity the initial capacity.
    27. * @throws IllegalArgumentException if the initial capacity is negative.
    28. */
    29. public HashMap(int initialCapacity) {
    30. this(initialCapacity, DEFAULT_LOAD_FACTOR);
    31. }
    32. /**
    33. * Constructs an empty HashMap with the default initial capacity
    34. * (16) and the default load factor (0.75).
    35. */
    36. public HashMap() {
    37. this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    38. }
    39. /**
    40. * Constructs a new HashMap with the same mappings as the
    41. * specified Map. The HashMap is created with
    42. * default load factor (0.75) and an initial capacity sufficient to
    43. * hold the mappings in the specified Map.
    44. *
    45. * @param m the map whose mappings are to be placed in this map
    46. * @throws NullPointerException if the specified map is null
    47. */
    48. public HashMap(Map extends K, ? extends V> m) {
    49. this.loadFactor = DEFAULT_LOAD_FACTOR;
    50. putMapEntries(m, false);
    51. }
    52. /**
    53. * Implements Map.putAll and Map constructor
    54. *
    55. * @param m the map
    56. * @param evict false when initially constructing this map, else
    57. * true (relayed to method afterNodeInsertion).
    58. */
    59. final void putMapEntries(Map extends K, ? extends V> m, boolean evict) {
    60. int s = m.size();
    61. if (s > 0) {
    62. if (table == null) { // pre-size
    63. float ft = ((float)s / loadFactor) + 1.0F;
    64. int t = ((ft < (float)MAXIMUM_CAPACITY) ?
    65. (int)ft : MAXIMUM_CAPACITY);
    66. if (t > threshold)
    67. threshold = tableSizeFor(t);
    68. }
    69. else if (s > threshold)
    70. resize();
    71. for (Map.Entry extends K, ? extends V> e : m.entrySet()) {
    72. K key = e.getKey();
    73. V value = e.getValue();
    74. putVal(hash(key), key, value, false, evict);
    75. }
    76. }
    77. }

    其中最為重要的就是根據傳入的initialCapacity計算threshold的方法,如下所示:


    1. /**
    2. * Returns a power of two size for the given target capacity.
    3. */
    4. static final int tableSizeFor(int cap) {
    5. int n = cap - 1;
    6. n |= n >>> 1;
    7. n |= n >>> 2;
    8. n |= n >>> 4;
    9. n |= n >>> 8;
    10. n |= n >>> 16;
    11. return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    12. }

    其功能在於計算得到大於等於initialCapacity的最小的2的正整數冪。

    添加KV

    添加KV方法算是HashMap中最為重要的幾個方法之一了。

    先上源碼:


    1. /**
    2. * Associates the specified value with the specified key in this map.
    3. * If the map previously contained a mapping for the key, the old
    4. * value is replaced.
    5. *
    6. * @param key key with which the specified value is to be associated
    7. * @param value value to be associated with the specified key
    8. * @return the previous value associated with key, or
    9. * null if there was no mapping for key.
    10. * (A null return can also indicate that the map
    11. * previously associated null with key.)
    12. */
    13. public V put(K key, V value) {
    14. return putVal(hash(key), key, value, false, true);
    15. }
    16. static final int hash(Object key) {
    17. int h;
    18. return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    19. }
    20. final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
    21. boolean evict) {
    22. Node[] tab; Node p; int n, i;
    23. if ((tab = table) == null || (n = tab.length) == 0)
    24. n = (tab = resize()).length;
    25. if ((p = tab[i = (n - 1) & hash]) == null)
    26. tab[i] = newNode(hash, key, value, null);
    27. else {
    28. Node e; K k;
    29. if (p.hash == hash &&
    30. ((k = p.key) == key || (key != null && key.equals(k))))
    31. e = p;
    32. else if (p instanceof TreeNode)
    33. e = ((TreeNode)p).putTreeVal(this, tab, hash, key, value);
    34. else {
    35. for (int binCount = 0; ; ++binCount) {
    36. if ((e = p.next) == null) {
    37. p.next = newNode(hash, key, value, null);
    38. if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
    39. treeifyBin(tab, hash);
    40. break;
    41. }
    42. if (e.hash == hash &&
    43. ((k = e.key) == key || (key != null && key.equals(k))))
    44. break;
    45. p = e;
    46. }
    47. }
    48. if (e != null) { // existing mapping for key
    49. V oldValue = e.value;
    50. if (!onlyIfAbsent || oldValue == null)
    51. e.value = value;
    52. afterNodeAccess(e);
    53. return oldValue;
    54. }
    55. }
    56. ++modCount;
    57. if (++size > threshold)
    58. resize();
    59. afterNodeInsertion(evict);
    60. return null;
    61. }

    個人感覺該方法中的信息量還是比較大的,該方法在1.8版本中,在處理落入同一個分桶的數據時,較歷史版本有所更新。

    之前版本中,對於該方法的處理是這樣的流程:

    1. 根據給出的key,計算hash值(key的hashCode方法),從而定位到分桶;
    2. 檢查定位到的分桶中是否已經存在了數據。如果沒有,則直接將數據放入分桶中;反之,則將數據加入分桶的鏈表中。

    以上操作存在一個問題,那就是當分桶中數據比較多的時候,get方法需要在根據key的hash值定位到具體分桶後,還需要進行鏈表遍歷才能找到需要獲取到的value。假設鏈表長度為n,則時間複雜度為O(n),效率不夠理想。

    有什麼辦法進行改進呢?對數據結構有所瞭解的同學們立刻就能反應到,相對於鏈表的遍歷,二叉樹的遍歷往往能夠減小時間複雜度。因此在JDK8中對此進行了功能增強,變為如下流程:

    1. 據給出的key,計算hash值(key的hashCode方法),從而定位到分桶,此步驟與改版前一致;
    2. 檢查分桶中是否有數據,如果沒有數據則直接將數據放入分桶中;反之,更精細化地進行處理。對於分桶中不太多數據的場景(個數小於TREEIFY_THRESHOLD-1)採用鏈表的方式,將新節點放到鏈表的尾部;對於分桶中較多數據的場景(個數大於等於TREEIFY_THRESHOLD-1),採用紅黑樹的方式,將節點進行存儲。這樣一來,既減輕了數據結構的複雜程度(紅黑樹較鏈表要複雜一些),又在鏈表較長的情況下,有效地減少了遍歷節點的時間複雜度。個人覺得,這算是JDK8中,對於解決此問題的很精巧的一點。

    下面我們仔細地看一下這個方法的代碼。

    首先,需要先根據key找到對應的分桶,所使用的方法是key.hashCode()右移16位,再與自身按位異或運算得到的hash值,再與分桶總數-1進行按位與操作。初看這個方法的時候,你一定很疑惑,取了hashCode之後為什麼還要如此複雜地右移16位,再與自身按位異或才能獲取到hash值。原因在於,hashCode方法返回的hash值並不夠完全隨機,為了更好地使得不同的key均勻地散落在不同的分桶中(換句話說,為了不同key的hash值分佈得更加均勻),在原有的調用hashCode方法的基礎上,又做了一次運算,將結果進一步地隨機化。之後邏輯上需要將計算得來的h值,按照分桶總數取模,這樣才能儘可能地保證均勻地分配到不同的分桶中。但是,計算機取模運算的效率不高,所以採用了與分桶總數-1進行按位與操作,也能達到同樣的效果。

    接下來,根據分桶中已有的數據的情況來判斷如何將輸入的Key-Value存儲到分桶中。如果分桶中沒有數據,那麼就將輸入的Key-Value轉化成存儲節點Node,存儲到分桶中;如果分桶中有數據,且已採用紅黑樹進行存儲了,則將該存儲節點插入到紅黑樹中,以優化後續查找性能;如果分桶中的數據依然是使用鏈表進行存儲,則存儲到鏈表的尾部,之後判斷是否達到樹化條件(鏈表中存儲的數據個數大於等於TREEIFY_THRESHOLD-1),達到的話就將鏈表轉成紅黑樹存儲。具體的插入紅黑樹的代碼就不再這裡贅述了,感興趣的同學可在參照紅黑樹的介紹文章:https://baike.baidu.com/item/%E7%BA%A2%E9%BB%91%E6%A0%91。

    最後,在插入節點之後,判斷是否要進行擴容(resize),擴容的條件是加入新節點後,存儲的Key-Value的個數大於threshold(threshold的賦值參見構造函數中,為大於等於initialCapacity的最小的2的正整數冪)。擴容的具體操作步驟如下:


    1. /**
    2. * Initializes or doubles table size. If null, allocates in
    3. * accord with initial capacity target held in field threshold.
    4. * Otherwise, because we are using power-of-two expansion, the
    5. * elements from each bin must either stay at same index, or move
    6. * with a power of two offset in the new table.
    7. *
    8. * @return the table
    9. */
    10. final Node[] resize() {
    11. Node[] oldTab = table;
    12. int oldCap = (oldTab == null) ? 0 : oldTab.length;
    13. int oldThr = threshold;
    14. int newCap, newThr = 0;
    15. if (oldCap > 0) {
    16. if (oldCap >= MAXIMUM_CAPACITY) {
    17. threshold = Integer.MAX_VALUE;
    18. return oldTab;
    19. }
    20. else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
    21. oldCap >= DEFAULT_INITIAL_CAPACITY)
    22. newThr = oldThr << 1; // double threshold
    23. }
    24. else if (oldThr > 0) // initial capacity was placed in threshold
    25. newCap = oldThr;
    26. else { // zero initial threshold signifies using defaults
    27. newCap = DEFAULT_INITIAL_CAPACITY;
    28. newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
    29. }
    30. if (newThr == 0) {
    31. float ft = (float)newCap * loadFactor;
    32. newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
    33. (int)ft : Integer.MAX_VALUE);
    34. }
    35. threshold = newThr;
    36. @SuppressWarnings({"rawtypes","unchecked"})
    37. Node[] newTab = (Node[])new Node[newCap];
    38. table = newTab;
    39. if (oldTab != null) {
    40. for (int j = 0; j < oldCap; ++j) {
    41. Node e;
    42. if ((e = oldTab[j]) != null) {
    43. oldTab[j] = null;
    44. if (e.next == null)
    45. newTab[e.hash & (newCap - 1)] = e;
    46. else if (e instanceof TreeNode)
    47. ((TreeNode)e).split(this, newTab, j, oldCap);
    48. else { // preserve order
    49. Node loHead = null, loTail = null;
    50. Node hiHead = null, hiTail = null;
    51. Node next;
    52. do {
    53. next = e.next;
    54. if ((e.hash & oldCap) == 0) {
    55. if (loTail == null)
    56. loHead = e;
    57. else
    58. loTail.next = e;
    59. loTail = e;
    60. }
    61. else {
    62. if (hiTail == null)
    63. hiHead = e;
    64. else
    65. hiTail.next = e;
    66. hiTail = e;
    67. }
    68. } while ((e = next) != null);
    69. if (loTail != null) {
    70. loTail.next = null;
    71. newTab[j] = loHead;
    72. }
    73. if (hiTail != null) {
    74. hiTail.next = null;
    75. newTab[j + oldCap] = hiHead;
    76. }
    77. }
    78. }
    79. }
    80. }
    81. return newTab;
    82. }

    首先計算得到擴充後的threshold和capacity,然後將擴容前的各分桶中的數據按照新分桶的定位計算方法,定位到新分桶中,然後依次進行遷移。注意代碼中涉及到的HashMap中的table屬性在多線程操作時是臨界資源,因此HashMap不是線程安全的,需要在代碼中做線程安全保護。此外,我們觀察到HashMap的resize方法,需要進行新分桶的threshold/capacity重新計算,舊數據按照新分桶進行重新定位,舊分桶中的數據按新規則向新分桶中遷移,這裡邊的開銷還是比較大的,因此比較建議在創建HashMap實例的時候,儘可能地根據業務需求對HashMap的capacity進行一個預估,避免HashMap在程序運行過程中頻繁進行擴容計算,提升性能。

    根據Key獲取Value

    在瞭解了Key-Value的插入方法之後,再瞭解如何根據Key獲取Value就會簡單許多。get方法代碼如下:


    1. /**
    2. * Returns the value to which the specified key is mapped,
    3. * or {@code null} if this map contains no mapping for the key.
    4. *
    5. *

      More formally, if this map contains a mapping from a key

    6. * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
    7. * key.equals(k))}, then this method returns {@code v}; otherwise
    8. * it returns {@code null}. (There can be at most one such mapping.)
    9. *
    10. *

      A return value of {@code null} does not necessarily

    11. * indicate that the map contains no mapping for the key; it's also
    12. * possible that the map explicitly maps the key to {@code null}.
    13. * The {@link #containsKey containsKey} operation may be used to
    14. * distinguish these two cases.
    15. *
    16. * @see #put(Object, Object)
    17. */
    18. public V get(Object key) {
    19. Node e;
    20. return (e = getNode(hash(key), key)) == null ? null : e.value;
    21. }
    22. /**
    23. * Implements Map.get and related methods
    24. *
    25. * @param hash hash for key
    26. * @param key the key
    27. * @return the node, or null if none
    28. */
    29. final Node getNode(int hash, Object key) {
    30. Node[] tab; Node first, e; int n; K k;
    31. if ((tab = table) != null && (n = tab.length) > 0 &&
    32. (first = tab[(n - 1) & hash]) != null) {
    33. if (first.hash == hash && // always check first node
    34. ((k = first.key) == key || (key != null && key.equals(k))))
    35. return first;
    36. if ((e = first.next) != null) {
    37. if (first instanceof TreeNode)
    38. return ((TreeNode)first).getTreeNode(hash, key);
    39. do {
    40. if (e.hash == hash &&
    41. ((k = e.key) == key || (key != null && key.equals(k))))
    42. return e;
    43. } while ((e = e.next) != null);
    44. }
    45. }
    46. return null;
    47. }

    整體步驟分為以下幾步:

    1. 按照put方法中描述的規則,根據key定位到分桶;
    2. 將key與分桶中第一個節點的key做equals比較,如果相等則返回;不相等則考慮與後續節點進行比對。分兩種情況,如果分桶中存儲的是紅黑樹,則做樹的遍歷查找;如果存儲的是鏈表,則做鏈表的遍歷查找。如果遍歷結束依然沒有找到,則返回null。

    根據Key刪除

    remove方法的操作步驟如下:


    1. /**
    2. * Removes the mapping for the specified key from this map if present.
    3. *
    4. * @param key key whose mapping is to be removed from the map
    5. * @return the previous value associated with key, or
    6. * null if there was no mapping for key.
    7. * (A null return can also indicate that the map
    8. * previously associated null with key.)
    9. */
    10. public V remove(Object key) {
    11. Node e;
    12. return (e = removeNode(hash(key), key, null, false, true)) == null ?
    13. null : e.value;
    14. }
    15. /**
    16. * Implements Map.remove and related methods
    17. *
    18. * @param hash hash for key
    19. * @param key the key
    20. * @param value the value to match if matchValue, else ignored
    21. * @param matchValue if true only remove if value is equal
    22. * @param movable if false do not move other nodes while removing
    23. * @return the node, or null if none
    24. */
    25. final Node removeNode(int hash, Object key, Object value,
    26. boolean matchValue, boolean movable) {
    27. Node[] tab; Node p; int n, index;
    28. if ((tab = table) != null && (n = tab.length) > 0 &&
    29. (p = tab[index = (n - 1) & hash]) != null) {
    30. Node node = null, e; K k; V v;
    31. if (p.hash == hash &&
    32. ((k = p.key) == key || (key != null && key.equals(k))))
    33. node = p;
    34. else if ((e = p.next) != null) {
    35. if (p instanceof TreeNode)
    36. node = ((TreeNode)p).getTreeNode(hash, key);
    37. else {
    38. do {
    39. if (e.hash == hash &&
    40. ((k = e.key) == key ||
    41. (key != null && key.equals(k)))) {
    42. node = e;
    43. break;
    44. }
    45. p = e;
    46. } while ((e = e.next) != null);
    47. }
    48. }
    49. if (node != null && (!matchValue || (v = node.value) == value ||
    50. (value != null && value.equals(v)))) {
    51. if (node instanceof TreeNode)
    52. ((TreeNode)node).removeTreeNode(this, tab, movable);
    53. else if (node == p)
    54. tab[index] = node.next;
    55. else
    56. p.next = node.next;
    57. ++modCount;
    58. --size;
    59. afterNodeRemoval(node);
    60. return node;
    61. }
    62. }
    63. return null;
    64. }
    65. 根據key定位到分桶;
    66. 從分桶中找到要刪除的節點;
    67. 刪除節點,並將size-1。

    ConcurrentHashMap

    ConcurrentHashMap是線程安全的HashMap,在JDK8中,ConcurrentHashMap為進一步優化多線程下的併發性能,不再採用分段鎖對分桶進行保護,而是採用CAS操作(Compare And Set)。這個改變在思想上很像是樂觀鎖與悲觀鎖。

    在JDK8之前,ConcurrentHashMap採用分段鎖的方式來保證線程安全性,相對於CAS操作,量級更重一些,因為需要做加鎖、解鎖操作。這很類似於悲觀鎖的思想,即假設多線程併發操作臨界資源的幾率比較大,因此採用加鎖的方式來應對。

    JDK8中,採用了輕量級的CAS操作來獲取及向分桶中寫入元素(當分桶中沒有元素存儲時),採用類似LongAdder的方式計算ConcurrentHashMap的size,使得多線程操作時,採用更輕量級的方式加以應對。CAS很類似於樂觀鎖的思想,而LongAdder是將多線程的+1操作隨機地定位到不同分桶中來避免寫入時的衝突,在計算size時,通過將各分桶中的數值進行疊加從而得到總的size值,從而很好地解決了多線程+1寫操作的衝突和加鎖等待問題。

    構造函數

    與HashMap相類似,ConcurrentHashMap的構造函數也分為以下幾個:

    1. 默認構造函數:空實現;
    2. 給定initialCapacity的構造函數;
    3. 拷貝構造函數。

    這裡我們關注以下給定initialCapacity的構造函數,之所以關注這個構造函數,是因為HashMap與ConcurrentHashMap的reSize擴容過程都屬於開銷比較大的操作,所以期望使用者在使用的時候儘可能地根據業務需求對size有一個大致的預估,並使用該構造函數對Map進行構造,以避免後續不斷地擴容操作,給性能帶來不利的影響。

    我們都還記得上文中提到的HashMap的size計算,其值等於大於等於initialCapacity的最小的2的正整數冪。而ConcurrentHashMap的該構造函數源碼如下:


    1. /**
    2. * Creates a new, empty map with an initial table size
    3. * accommodating the specified number of elements without the need
    4. * to dynamically resize.
    5. *
    6. * @param initialCapacity The implementation performs internal
    7. * sizing to accommodate this many elements.
    8. * @throws IllegalArgumentException if the initial capacity of
    9. * elements is negative
    10. */
    11. public ConcurrentHashMap(int initialCapacity) {
    12. if (initialCapacity < 0)
    13. throw new IllegalArgumentException();
    14. int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
    15. MAXIMUM_CAPACITY :
    16. tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
    17. this.sizeCtl = cap;
    18. }

    從中不難看出,ConcurrentHashMap的sizeCtl值大於等於1.5倍的initialCapacity+1的最小2的正整數冪。

    而拷貝構造函數的思路則是遍歷入參中的Map,然後依次將其put到本次創建的ConcurrentHashMap中,put方法的操作過程我會在下邊加以詳細介紹,所以此處不再贅述。

    添加KV

    put方法與HashMap的put方法在基本思路上是一致的,主要分以下步驟:

    1. 對傳入的key計算hashCode,然後計算獲取到index,即指向哪個分桶;
    2. 訪問到指定的分桶,如果分桶中此時沒有節點,則將KV做插入處理;如果分桶中已經有節點了,則判斷是樹節點還是鏈表節點,然後根據樹或是鏈表進行遍歷。如果該Key已經存儲到了Map中了,則將新值寫入,舊值返回;反之,則創建新節點,存儲到樹的合適位置,或者是鏈表的尾節點(當然還會根據鏈表中存儲的節點數來判斷是否應該進行樹化處理);
    3. 將size自增1,並判斷是否需要進行reSize擴容操作,是則進行擴容,即生成新的分桶,將舊有分桶中的節點根據新的規則拷貝並創建到新的分桶中。

    只不過ConcurrentHashMap為了保證其是線程安全的,因此採用了一系列的手段來保證這一點。


    1. /**
    2. * Maps the specified key to the specified value in this table.
    3. * Neither the key nor the value can be null.
    4. *
    5. *

      The value can be retrieved by calling the {@code get} method

    6. * with a key that is equal to the original key.
    7. *
    8. * @param key key with which the specified value is to be associated
    9. * @param value value to be associated with the specified key
    10. * @return the previous value associated with {@code key}, or
    11. * {@code null} if there was no mapping for {@code key}
    12. * @throws NullPointerException if the specified key or value is null
    13. */
    14. public V put(K key, V value) {
    15. return putVal(key, value, false);
    16. }
    17. /** Implementation for put and putIfAbsent */
    18. final V putVal(K key, V value, boolean onlyIfAbsent) {
    19. if (key == null || value == null) throw new NullPointerException();
    20. int hash = spread(key.hashCode());
    21. int binCount = 0;
    22. for (Node[] tab = table;;) {
    23. Node f; int n, i, fh;
    24. if (tab == null || (n = tab.length) == 0)
    25. tab = initTable();
    26. else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
    27. if (casTabAt(tab, i, null,
    28. new Node(hash, key, value, null)))
    29. break; // no lock when adding to empty bin
    30. }
    31. else if ((fh = f.hash) == MOVED)
    32. tab = helpTransfer(tab, f);
    33. else {
    34. V oldVal = null;
    35. synchronized (f) {
    36. if (tabAt(tab, i) == f) {
    37. if (fh >= 0) {
    38. binCount = 1;
    39. for (Node e = f;; ++binCount) {
    40. K ek;
    41. if (e.hash == hash &&
    42. ((ek = e.key) == key ||
    43. (ek != null && key.equals(ek)))) {
    44. oldVal = e.val;
    45. if (!onlyIfAbsent)
    46. e.val = value;
    47. break;
    48. }
    49. Node pred = e;
    50. if ((e = e.next) == null) {
    51. pred.next = new Node(hash, key,
    52. value, null);
    53. break;
    54. }
    55. }
    56. }
    57. else if (f instanceof TreeBin) {
    58. Node p;
    59. binCount = 2;
    60. if ((p = ((TreeBin)f).putTreeVal(hash, key,
    61. value)) != null) {
    62. oldVal = p.val;
    63. if (!onlyIfAbsent)
    64. p.val = value;
    65. }
    66. }
    67. }
    68. }
    69. if (binCount != 0) {
    70. if (binCount >= TREEIFY_THRESHOLD)
    71. treeifyBin(tab, i);
    72. if (oldVal != null)
    73. return oldVal;
    74. break;
    75. }
    76. }
    77. }
    78. addCount(1L, binCount);
    79. return null;
    80. }

    關注其中在獲取分桶中節點的tabAt方法,和向分桶中添加節點的casTabAt方法,二者在底層都使用到了sun.misc.Unsafe。關於Unsafe,可參考文章:https://blog.csdn.net/anla_/article/details/78631026。簡單說就是,Unsafe提供了硬件級別的原子操作。藉助於Unsafe類,tabAt方法能夠獲取到分桶中的節點,casTabAt方法能夠採用CAS的方式將新節點寫入到分桶中,即如果分桶中如果當前存儲的節點與剛才使用tabAt方法獲取到的相同,則將新節點覆蓋之前的節點;反之,則說明在當前線程設置之前,其他線程已經改變了分桶中的節點,因此本線程的設置操作失敗。

    如果CAS操作失敗了,則進入到下邊的分支,首先check是否該節點在遷移中(遷移中的節點的hash值為-1,有可能是在reSize擴容中),如果不在遷移過程中,則採用synchronized關鍵字,對tabAt獲取到的節點加鎖,然後像上文中陳述的步驟2那樣完成新KV的寫入操作。

    最後一步則是將ConcurrentHashMap的size+1,並根據已存儲的節點的個數判斷是否要進行reSize擴容操作。為了滿足線程安全性,並儘可能地提升size+1的性能,ConcurrentHashMap採用的是類似於LongAdder的方式來完成size+1這個操作的。可參考文章:https://www.cnblogs.com/ten951/p/6590596.html。LongAdder的思想本質上就是將多線程操作同一個變量(將同一個size做+1操作),轉變為多線程隨機向一組cell做寫入,通過將各cell中的value累加,即可得到總的值,雖然這個值可能不準。這樣即可由多線程集中寫,轉變為多線程分散寫,有效地減輕了多線程操作時的競爭。

    根據Key獲取Value

    在瞭解了put方法之後,再去看get方法就會明晰很多。


    1. /**
    2. * Returns the value to which the specified key is mapped,
    3. * or {@code null} if this map contains no mapping for the key.
    4. *
    5. *

      More formally, if this map contains a mapping from a key

    6. * {@code k} to a value {@code v} such that {@code key.equals(k)},
    7. * then this method returns {@code v}; otherwise it returns
    8. * {@code null}. (There can be at most one such mapping.)
    9. *
    10. * @throws NullPointerException if the specified key is null
    11. */
    12. public V get(Object key) {
    13. Node[] tab; Node e, p; int n, eh; K ek;
    14. int h = spread(key.hashCode());
    15. if ((tab = table) != null && (n = tab.length) > 0 &&
    16. (e = tabAt(tab, (n - 1) & h)) != null) {
    17. if ((eh = e.hash) == h) {
    18. if ((ek = e.key) == key || (ek != null && key.equals(ek)))
    19. return e.val;
    20. }
    21. else if (eh < 0)
    22. return (p = e.find(h, key)) != null ? p.val : null;
    23. while ((e = e.next) != null) {
    24. if (e.hash == h &&
    25. ((ek = e.key) == key || (ek != null && key.equals(ek))))
    26. return e.val;
    27. }
    28. }
    29. return null;
    30. }

    從代碼中不難看出,ConcurrentHashMap的get方法與HashMap的get方法的大致思路是一致的。

    按Key刪除

    remove方法的源碼如下:


    1. /**
    2. * Removes the key (and its corresponding value) from this map.
    3. * This method does nothing if the key is not in the map.
    4. *
    5. * @param key the key that needs to be removed
    6. * @return the previous value associated with {@code key}, or
    7. * {@code null} if there was no mapping for {@code key}
    8. * @throws NullPointerException if the specified key is null
    9. */
    10. public V remove(Object key) {
    11. return replaceNode(key, null, null);
    12. }
    13. /**
    14. * Implementation for the four public remove/replace methods:
    15. * Replaces node value with v, conditional upon match of cv if
    16. * non-null. If resulting value is null, delete.
    17. */
    18. final V replaceNode(Object key, V value, Object cv) {
    19. int hash = spread(key.hashCode());
    20. for (Node[] tab = table;;) {
    21. Node f; int n, i, fh;
    22. if (tab == null || (n = tab.length) == 0 ||
    23. (f = tabAt(tab, i = (n - 1) & hash)) == null)
    24. break;
    25. else if ((fh = f.hash) == MOVED)
    26. tab = helpTransfer(tab, f);
    27. else {
    28. V oldVal = null;
    29. boolean validated = false;
    30. synchronized (f) {
    31. if (tabAt(tab, i) == f) {
    32. if (fh >= 0) {
    33. validated = true;
    34. for (Node e = f, pred = null;;) {
    35. K ek;
    36. if (e.hash == hash &&
    37. ((ek = e.key) == key ||
    38. (ek != null && key.equals(ek)))) {
    39. V ev = e.val;
    40. if (cv == null || cv == ev ||
    41. (ev != null && cv.equals(ev))) {
    42. oldVal = ev;
    43. if (value != null)
    44. e.val = value;
    45. else if (pred != null)
    46. pred.next = e.next;
    47. else
    48. setTabAt(tab, i, e.next);
    49. }
    50. break;
    51. }
    52. pred = e;
    53. if ((e = e.next) == null)
    54. break;
    55. }
    56. }
    57. else if (f instanceof TreeBin) {
    58. validated = true;
    59. TreeBin t = (TreeBin)f;
    60. TreeNode r, p;
    61. if ((r = t.root) != null &&
    62. (p = r.findTreeNode(hash, key, null)) != null) {
    63. V pv = p.val;
    64. if (cv == null || cv == pv ||
    65. (pv != null && cv.equals(pv))) {
    66. oldVal = pv;
    67. if (value != null)
    68. p.val = value;
    69. else if (t.removeTreeNode(p))
    70. setTabAt(tab, i, untreeify(t.first));
    71. }
    72. }
    73. }
    74. }
    75. }
    76. if (validated) {
    77. if (oldVal != null) {
    78. if (value == null)
    79. addCount(-1L, -1);
    80. return oldVal;
    81. }
    82. break;
    83. }
    84. }
    85. }
    86. return null;
    87. }

    從中不難看出,其操作流程與put方法極為類似。

    1. 通過key計算hashCode,繼而計算index;
    2. 根據index,定位到具體的分桶,如果分桶中沒有數據,返回,說明要刪除的key並不存在;
    3. 如果定位到的分桶中的節點的hash值為-1,則說明節點在遷移過程中,則helpTransfer;
    4. 2,3都不滿足,則通過synchronized鎖住節點,根據該分桶中存儲的節點是採用紅黑樹進行存儲還是鏈表進行存儲,進行相應的刪除處理操作。

    Set

    Set與List的區別在於,Set中存儲的元素是經過了去重的(即如果a.equals(b),則Set中只可能存在一個)。

    Set的典型實現是HashSet,其主要方法add,remove,contains,均是通過內置的HashMap來進行實現的。

    比如add方法,本質上是調用了HashMap的put方法,以傳入的object為Key,並以dummy value(private static final Object PRESENT = new Object();)為Value。

    remove方法,也是在內部調用了HashMap的remove方法,將傳入的object作為key,從而對HashSet中保存的object進行刪除。

    如果大家喜歡這篇文章的話,希望大家能夠收藏,轉發 謝謝!更多相關資訊可以關注西安華美校區,免費獲得java零基礎教程!額外附送excel教程!


    分享到:


    相關文章: