Class PooledHashMap<K,V>
- Type Parameters:
K- The map key typeV- The map value type
- All Implemented Interfaces:
Map<K,V>
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.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intprivate final ObjectPool<PooledHashMap.Entry<K, V>> private static final floatprivate intprivate ArrayList<PooledHashMap.Entry<K,V>>[] -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newPooledHashMapwith a default amount of buckets (capacity).PooledHashMap(int capacity) Creates aPooledHashMapwithcapacitybuckets. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()booleancontainsKey(Object key) booleancontainsValue(Object value) entrySet()voidforEach(BiConsumer<? super K, ? super V> action) Retrieves the mapped value forkey.private intbooleanisEmpty()keySet()Add a key, value pair to the map.voidprivate voidrehash()Removes the mapping for the givenkey.intsize()values()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
DEFAULT_CAPACITY
private static final int DEFAULT_CAPACITY- See Also:
-
LOAD_FACTOR
private static final float LOAD_FACTOR- See Also:
-
table
-
entryPool
-
size
private int size
-
-
Constructor Details
-
PooledHashMap
public PooledHashMap(int capacity) Creates aPooledHashMapwithcapacitybuckets.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 newPooledHashMapwith a default amount of buckets (capacity).- See Also:
-
-
Method Details
-
put
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
Retrieves the mapped value forkey. -
remove
Removes the mapping for the givenkey. -
size
public int size() -
isEmpty
public boolean isEmpty() -
containsKey
- Specified by:
containsKeyin interfaceMap<K,V>
-
containsValue
- Specified by:
containsValuein interfaceMap<K,V>
-
clear
public void clear() -
forEach
-
getBucket
-
entrySet
-
values
-
putAll
-
keySet
-