Class LockingHashMap
-
- All Implemented Interfaces:
public final class LockingHashMap<K extends Object, V extends Object>A thread-safe implementation of a concurrent hash map with mutex-based locking. This class provides concurrent access to the underlying map while ensuring fairness in lock acquisition.
-
-
Constructor Summary
Constructors Constructor Description LockingHashMap(Integer concurrencyLevel)
-
Method Summary
Modifier and Type Method Description final VfillIfAbsent(K key, SuspendFunction0<V> fill)Fills the value for the given key if it is absent in the map. final Unitput(K key, V value)Puts the value for the given key in the map. final Vget(K key)Retrieves the value for the given key from the map. final Unitremove(K key)Removes the value for the given key from the map. final Unitclear()Clears all the entries in the map. final BooleanisEmpty()Checks if the map is empty. final BooleancontainsKey(K key)Checks if the map contains the given key. StringtoString()-
-
Constructor Detail
-
LockingHashMap
LockingHashMap(Integer concurrencyLevel)
- Parameters:
concurrencyLevel- The estimated number of concurrently updating threads.
-
-
Method Detail
-
fillIfAbsent
final V fillIfAbsent(K key, SuspendFunction0<V> fill)
Fills the value for the given key if it is absent in the map. The fill function is a suspending lambda that is invoked only if the key is not present in the map. The function returns the value associated with the key after filling.
- Parameters:
key- The key to fillfill- The suspending lambda to fill the value for the key- Returns:
The value associated with the key after filling
-
put
final Unit put(K key, V value)
Puts the value for the given key in the map.
- Parameters:
key- The key to putvalue- The value to put
-
get
final V get(K key)
Retrieves the value for the given key from the map.
- Parameters:
key- The key to get- Returns:
The value associated with the key, or null if the key is not present in the map
-
remove
final Unit remove(K key)
Removes the value for the given key from the map.
- Parameters:
key- The key to remove
-
isEmpty
final Boolean isEmpty()
Checks if the map is empty.
- Returns:
True if the map is empty, false otherwise
-
containsKey
final Boolean containsKey(K key)
Checks if the map contains the given key.
- Parameters:
key- The key to check- Returns:
True if the map contains the key, false otherwise
-
-
-
-