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 int
private final ObjectPool
<PooledHashMap.Entry<K, V>> private static final float
private int
private ArrayList<PooledHashMap.Entry<K,
V>>[] -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newPooledHashMap
with a default amount of buckets (capacity).PooledHashMap
(int capacity) Creates aPooledHashMap
withcapacity
buckets. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
boolean
containsKey
(Object key) boolean
containsValue
(Object value) entrySet()
void
forEach
(BiConsumer<? super K, ? super V> action) Retrieves the mapped value forkey
.private int
boolean
isEmpty()
keySet()
Add a key, value pair to the map.void
private void
rehash()
Removes the mapping for the givenkey
.int
size()
values()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods 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 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:
-
-
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:
containsKey
in interfaceMap<K,
V>
-
containsValue
- Specified by:
containsValue
in interfaceMap<K,
V>
-
clear
public void clear() -
forEach
-
getBucket
-
entrySet
-
values
-
putAll
-
keySet
-