Class PooledHashMap<K,V>
- java.lang.Object
-
- io.opentelemetry.sdk.metrics.internal.state.PooledHashMap<K,V>
-
- Type Parameters:
K
- The map key typeV
- The map value type
- All Implemented Interfaces:
java.util.Map<K,V>
public final class PooledHashMap<K,V> extends java.lang.Object implements java.util.Map<K,V>
A bucket-based hash map with an internal re-usable map entry objects poolThe 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
PooledHashMap.Entry<K,V>
-
Field Summary
Fields Modifier and Type Field Description private static int
DEFAULT_CAPACITY
private ObjectPool<PooledHashMap.Entry<K,V>>
entryPool
private static float
LOAD_FACTOR
private int
size
private java.util.ArrayList<PooledHashMap.Entry<K,V>>[]
table
-
Constructor Summary
Constructors Constructor Description PooledHashMap()
Creates a newPooledHashMap
with a default amount of buckets (capacity).PooledHashMap(int capacity)
Creates aPooledHashMap
withcapacity
buckets.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
boolean
containsKey(java.lang.Object key)
boolean
containsValue(java.lang.Object value)
java.util.Set<java.util.Map.Entry<K,V>>
entrySet()
void
forEach(java.util.function.BiConsumer<? super K,? super V> action)
V
get(java.lang.Object key)
Retrieves the mapped value forkey
.private int
getBucket(K key)
boolean
isEmpty()
java.util.Set<K>
keySet()
V
put(K key, V value)
Add a key, value pair to the map.void
putAll(java.util.Map<? extends K,? extends V> m)
private void
rehash()
V
remove(java.lang.Object key)
Removes the mapping for the givenkey
.int
size()
java.util.Collection<V>
values()
-
-
-
Field Detail
-
DEFAULT_CAPACITY
private static final int DEFAULT_CAPACITY
- See Also:
- Constant Field Values
-
LOAD_FACTOR
private static final float LOAD_FACTOR
- See Also:
- Constant Field Values
-
table
private java.util.ArrayList<PooledHashMap.Entry<K,V>>[] table
-
entryPool
private final ObjectPool<PooledHashMap.Entry<K,V>> entryPool
-
size
private int size
-
-
Constructor Detail
-
PooledHashMap
public PooledHashMap(int capacity)
Creates aPooledHashMap
withcapacity
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 newPooledHashMap
with a default amount of buckets (capacity).- See Also:
PooledHashMap(int)
-
-
Method Detail
-
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
-
rehash
private void rehash()
-
get
@Nullable public V get(java.lang.Object key)
Retrieves the mapped value forkey
.
-
remove
@Nullable public V remove(java.lang.Object key)
Removes the mapping for the givenkey
.
-
containsKey
public boolean containsKey(java.lang.Object key)
-
containsValue
public boolean containsValue(java.lang.Object value)
-
getBucket
private int getBucket(K key)
-
values
public java.util.Collection<V> values()
-
-