org.springframework.util

## 类 ConcurrentReferenceHashMap<K,V>

- java.lang.Object
- - java.util.AbstractMap<K,V>
  - - org.springframework.util.ConcurrentReferenceHashMap<K,V>

- 类型参数:
    `K` - the key type
    `V` - the value type
  所有已实现的接口:
    ConcurrentMap<K,V>, Map<K,V>

      public class ConcurrentReferenceHashMap<K,V>
      extends AbstractMap<K,V>
      implements ConcurrentMap<K,V>

  A `ConcurrentHashMap` that uses `soft` or weak references for both `keys` and `values`.

  This class can be used as an alternative to `Collections.synchronizedMap(new WeakHashMap<K, Reference<V>>())` in order to support better performance when accessed concurrently. This implementation follows the same design constraints as `ConcurrentHashMap` with the exception that `null` values and `null` keys are supported.

  NOTE: The use of references means that there is no guarantee that items placed into the map will be subsequently available. The garbage collector may discard references at any time, so it may appear that an unknown thread is silently removing entries.

  If not explicitly specified, this implementation will use soft entry references.

  从以下版本开始:
    3.2
  作者:
    Phillip Webb, Juergen Hoeller

- - ### 嵌套类概要

    嵌套类
    限定符和类型 | 类和说明
    --- | ---
    `protected static class` | `ConcurrentReferenceHashMap.Entry<K,V>` A single map entry.
    `protected static interface` | `ConcurrentReferenceHashMap.Reference<K,V>` A reference to an `ConcurrentReferenceHashMap.Entry` contained in the map.
    `protected class` | `ConcurrentReferenceHashMap.ReferenceManager` Strategy class used to manage `ConcurrentReferenceHashMap.Reference`s.
    `static class` | `ConcurrentReferenceHashMap.ReferenceType` Various reference types supported by this map.
    `protected static class` | `ConcurrentReferenceHashMap.Restructure` The types of restructuring that can be performed.
    `protected class` | `ConcurrentReferenceHashMap.Segment` A single segment used to divide the map to allow better concurrent performance.

    - ### 从类继承的嵌套类/接口 java.util.AbstractMap

      `AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>`

  - ### 构造器概要

    构造器
    构造器和说明
    `ConcurrentReferenceHashMap()` Create a new `ConcurrentReferenceHashMap` instance.
    `ConcurrentReferenceHashMap(int initialCapacity)` Create a new `ConcurrentReferenceHashMap` instance.
    `ConcurrentReferenceHashMap(int initialCapacity, ConcurrentReferenceHashMap.ReferenceType referenceType)` Create a new `ConcurrentReferenceHashMap` instance.
    `ConcurrentReferenceHashMap(int initialCapacity, float loadFactor)` Create a new `ConcurrentReferenceHashMap` instance.
    `ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)` Create a new `ConcurrentReferenceHashMap` instance.
    `ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel, ConcurrentReferenceHashMap.ReferenceType referenceType)` Create a new `ConcurrentReferenceHashMap` instance.
    `ConcurrentReferenceHashMap(int initialCapacity, int concurrencyLevel)` Create a new `ConcurrentReferenceHashMap` instance.

  - ### 方法概要

    所有方法 静态方法 实例方法 具体方法
    限定符和类型 | 方法和说明
    --- | ---
    `protected static int` | `calculateShift(int minimumValue, int maximumValue)` Calculate a shift value that can be used to create a power-of-two value between the specified maximum and minimum values.
    `void` | `clear()`
    `boolean` | `containsKey(Object key)`
    `protected ConcurrentReferenceHashMap.ReferenceManager` | `createReferenceManager()` Factory method that returns the `ConcurrentReferenceHashMap.ReferenceManager`.
    `Set<Map.Entry<K,V>>` | `entrySet()`
    `V` | `get(Object key)`
    `protected int` | `getHash(Object o)` Get the hash for a given object, apply an additional hash function to reduce collisions.
    `protected float` | `getLoadFactor()`
    `V` | `getOrDefault(Object key, V defaultValue)`
    `protected ConcurrentReferenceHashMap.Reference<K,V>` | `getReference(Object key, ConcurrentReferenceHashMap.Restructure restructure)` Return a `ConcurrentReferenceHashMap.Reference` to the `ConcurrentReferenceHashMap.Entry` for the specified `key`, or `null` if not found.
    `protected ConcurrentReferenceHashMap.Segment` | `getSegment(int index)`
    `protected int` | `getSegmentsSize()`
    `boolean` | `isEmpty()`
    `void` | `purgeUnreferencedEntries()` Remove any entries that have been garbage collected and are no longer referenced.
    `V` | `put(K key, V value)`
    `V` | `putIfAbsent(K key, V value)`
    `V` | `remove(Object key)`
    `boolean` | `remove(Object key, Object value)`
    `V` | `replace(K key, V value)`
    `boolean` | `replace(K key, V oldValue, V newValue)`
    `int` | `size()`

    - ### 从类继承的方法 java.util.AbstractMap

      `clone, containsValue, equals, hashCode, keySet, putAll, toString, values`

    - ### 从类继承的方法 java.lang.Object

      `finalize, getClass, notify, notifyAll, wait, wait, wait`

    - ### 从接口继承的方法 java.util.concurrent.ConcurrentMap

      `compute, computeIfAbsent, computeIfPresent, forEach, merge, replaceAll`

    - ### 从接口继承的方法 java.util.Map

      `containsValue, equals, hashCode, keySet, putAll, values`

- - ### 构造器详细资料

    - #### ConcurrentReferenceHashMap

          public ConcurrentReferenceHashMap()

      Create a new `ConcurrentReferenceHashMap` instance.

    - #### ConcurrentReferenceHashMap

          public ConcurrentReferenceHashMap(int initialCapacity)

      Create a new `ConcurrentReferenceHashMap` instance.
      参数:
        `initialCapacity` - the initial capacity of the map

    - #### ConcurrentReferenceHashMap

          public ConcurrentReferenceHashMap(int initialCapacity,
                                            float loadFactor)

      Create a new `ConcurrentReferenceHashMap` instance.
      参数:
        `initialCapacity` - the initial capacity of the map
        `loadFactor` - the load factor. When the average number of references per table exceeds this value resize will be attempted

    - #### ConcurrentReferenceHashMap

          public ConcurrentReferenceHashMap(int initialCapacity,
                                            int concurrencyLevel)

      Create a new `ConcurrentReferenceHashMap` instance.
      参数:
        `initialCapacity` - the initial capacity of the map
        `concurrencyLevel` - the expected number of threads that will concurrently write to the map

    - #### ConcurrentReferenceHashMap

          public ConcurrentReferenceHashMap(int initialCapacity,
                                            ConcurrentReferenceHashMap.ReferenceType referenceType)

      Create a new `ConcurrentReferenceHashMap` instance.
      参数:
        `initialCapacity` - the initial capacity of the map
        `referenceType` - the reference type used for entries (soft or weak)

    - #### ConcurrentReferenceHashMap

          public ConcurrentReferenceHashMap(int initialCapacity,
                                            float loadFactor,
                                            int concurrencyLevel)

      Create a new `ConcurrentReferenceHashMap` instance.
      参数:
        `initialCapacity` - the initial capacity of the map
        `loadFactor` - the load factor. When the average number of references per table exceeds this value, resize will be attempted.
        `concurrencyLevel` - the expected number of threads that will concurrently write to the map

    - #### ConcurrentReferenceHashMap

          public ConcurrentReferenceHashMap(int initialCapacity,
                                            float loadFactor,
                                            int concurrencyLevel,
                                            ConcurrentReferenceHashMap.ReferenceType referenceType)

      Create a new `ConcurrentReferenceHashMap` instance.
      参数:
        `initialCapacity` - the initial capacity of the map
        `loadFactor` - the load factor. When the average number of references per table exceeds this value, resize will be attempted.
        `concurrencyLevel` - the expected number of threads that will concurrently write to the map
        `referenceType` - the reference type used for entries (soft or weak)

  - ### 方法详细资料

    - #### getLoadFactor

          protected final float getLoadFactor()

    - #### getSegmentsSize

          protected final int getSegmentsSize()

    - #### getSegment

          protected final ConcurrentReferenceHashMap.Segment getSegment(int index)

    - #### createReferenceManager

          protected ConcurrentReferenceHashMap.ReferenceManager createReferenceManager()

      Factory method that returns the `ConcurrentReferenceHashMap.ReferenceManager`. This method will be called once for each `ConcurrentReferenceHashMap.Segment`.
      返回:
        a new reference manager

    - #### getHash

          protected int getHash(@Nullable
                                Object o)

      Get the hash for a given object, apply an additional hash function to reduce collisions. This implementation uses the same Wang/Jenkins algorithm as `ConcurrentHashMap`. Subclasses can override to provide alternative hashing.
      参数:
        `o` - the object to hash (may be null)
      返回:
        the resulting hash code

    - #### get

          @Nullable
          public V get(@Nullable
                                  Object key)

      指定者:
        `get` 在接口中 `Map<K,V>`
      覆盖:
        `get` 在类中 `AbstractMap<K,V>`

    - #### getOrDefault

          @Nullable
          public V getOrDefault(@Nullable
                                           Object key,
                                           @Nullable
                                           V defaultValue)

      指定者:
        `getOrDefault` 在接口中 `ConcurrentMap<K,V>`
      指定者:
        `getOrDefault` 在接口中 `Map<K,V>`

    - #### containsKey

          public boolean containsKey(@Nullable
                                     Object key)

      指定者:
        `containsKey` 在接口中 `Map<K,V>`
      覆盖:
        `containsKey` 在类中 `AbstractMap<K,V>`

    - #### getReference

          @Nullable
          protected final ConcurrentReferenceHashMap.Reference<K,V> getReference(@Nullable
                                                                                            Object key,
                                                                                            ConcurrentReferenceHashMap.Restructure restructure)

      Return a `ConcurrentReferenceHashMap.Reference` to the `ConcurrentReferenceHashMap.Entry` for the specified `key`, or `null` if not found.
      参数:
        `key` - the key (can be `null`)
        `restructure` - types of restructure allowed during this call
      返回:
        the reference, or `null` if not found

    - #### put

          @Nullable
          public V put(@Nullable
                                  K key,
                                  @Nullable
                                  V value)

      指定者:
        `put` 在接口中 `Map<K,V>`
      覆盖:
        `put` 在类中 `AbstractMap<K,V>`

    - #### putIfAbsent

          @Nullable
          public V putIfAbsent(@Nullable
                                          K key,
                                          @Nullable
                                          V value)

      指定者:
        `putIfAbsent` 在接口中 `ConcurrentMap<K,V>`
      指定者:
        `putIfAbsent` 在接口中 `Map<K,V>`

    - #### remove

          @Nullable
          public V remove(Object key)

      指定者:
        `remove` 在接口中 `Map<K,V>`
      覆盖:
        `remove` 在类中 `AbstractMap<K,V>`

    - #### remove

          public boolean remove(Object key,
                                Object value)

      指定者:
        `remove` 在接口中 `ConcurrentMap<K,V>`
      指定者:
        `remove` 在接口中 `Map<K,V>`

    - #### replace

          public boolean replace(K key,
                                 V oldValue,
                                 V newValue)

      指定者:
        `replace` 在接口中 `ConcurrentMap<K,V>`
      指定者:
        `replace` 在接口中 `Map<K,V>`

    - #### replace

          @Nullable
          public V replace(K key,
                                      V value)

      指定者:
        `replace` 在接口中 `ConcurrentMap<K,V>`
      指定者:
        `replace` 在接口中 `Map<K,V>`

    - #### clear

          public void clear()

      指定者:
        `clear` 在接口中 `Map<K,V>`
      覆盖:
        `clear` 在类中 `AbstractMap<K,V>`

    - #### purgeUnreferencedEntries

          public void purgeUnreferencedEntries()

      Remove any entries that have been garbage collected and are no longer referenced. Under normal circumstances garbage collected entries are automatically purged as items are added or removed from the Map. This method can be used to force a purge, and is useful when the Map is read frequently but updated less often.

    - #### size

          public int size()

      指定者:
        `size` 在接口中 `Map<K,V>`
      覆盖:
        `size` 在类中 `AbstractMap<K,V>`

    - #### isEmpty

          public boolean isEmpty()

      指定者:
        `isEmpty` 在接口中 `Map<K,V>`
      覆盖:
        `isEmpty` 在类中 `AbstractMap<K,V>`

    - #### entrySet

          public Set<Map.Entry<K,V>> entrySet()

      指定者:
        `entrySet` 在接口中 `Map<K,V>`
      指定者:
        `entrySet` 在类中 `AbstractMap<K,V>`

    - #### calculateShift

          protected static int calculateShift(int minimumValue,
                                              int maximumValue)

      Calculate a shift value that can be used to create a power-of-two value between the specified maximum and minimum values.
      参数:
        `minimumValue` - the minimum value
        `maximumValue` - the maximum value
      返回:
        the calculated shift (use `1 << shift` to obtain a value)