Class PooledHashMap<K,V>

java.lang.Object
io.opentelemetry.sdk.metrics.internal.state.PooledHashMap<K,V>
Type Parameters:
K - The map key type
V - The map value type
All Implemented Interfaces:
Map<K,V>

public final class PooledHashMap<K,V> extends Object implements Map<K,V>
A bucket-based hash map with an internal re-usable map entry objects pool

The goal of this map is to minimize memory allocation, leading to reduced time spent in garbage collection.

This map avoids allocating a new map entry on each put operation by maintaining a pool of reusable (mutable) map entries and borrowing a map entry object from the pool to hold the given key-value of the put operation. The borrowed object is returned to the pool when the map entry key is removed from the map.

This class is internal and is hence not for public use. Its APIs are unstable and can change at any time.

This class is not thread-safe.

  • Field Details

  • Constructor Details

    • PooledHashMap

      public PooledHashMap(int capacity)
      Creates a PooledHashMap with capacity buckets.

      The hashmap contains an array of buckets, each is an array-list of items. The number of buckets expands over time to avoid having too many items in one bucket, otherwise accessing an item by key won't be a constant time complexity.

      Parameters:
      capacity - The initial number of buckets to start with
    • PooledHashMap

      public PooledHashMap()
      Creates a new PooledHashMap with a default amount of buckets (capacity).
      See Also:
  • Method Details

    • put

      @Nullable public V put(K key, V value)
      Add a key, value pair to the map.

      Internally it uses a MapEntry from a pool of entries, to store this mapping

      Specified by:
      put in interface Map<K,V>
      Parameters:
      key - key with which the specified value is to be associated
      value - value to be associated with the specified key
      Returns:
      Null if the was no previous mapping for this key, or the value of the previous mapping of this key
    • rehash

      private void rehash()
    • get

      @Nullable public V get(Object key)
      Retrieves the mapped value for key.
      Specified by:
      get in interface Map<K,V>
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      The mapped value for key or null if there is no such mapping
    • remove

      @Nullable public V remove(Object key)
      Removes the mapping for the given key.
      Specified by:
      remove in interface Map<K,V>
      Parameters:
      key - key whose mapping is to be removed from the map
      Returns:
      The value mapped to this key, if the mapping exists, or null otherwise
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,V>
    • forEach

      public void forEach(BiConsumer<? super K,? super V> action)
      Specified by:
      forEach in interface Map<K,V>
    • getBucket

      private int getBucket(K key)
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
    • putAll

      public void putAll(Map<? extends K,? extends V> m)
      Specified by:
      putAll in interface Map<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>