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.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final V fillIfAbsent(K key, SuspendFunction0<V> fill) Fills the value for the given key if it is absent in the map.
      final Unit put(K key, V value) Puts the value for the given key in the map.
      final V get(K key) Retrieves the value for the given key from the map.
      final Unit remove(K key) Removes the value for the given key from the map.
      final Unit clear() Clears all the entries in the map.
      final Boolean isEmpty() Checks if the map is empty.
      final Boolean containsKey(K key) Checks if the map contains the given key.
      String toString()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 fill
        fill - 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 put
        value - 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
      • clear

         final Unit clear()

        Clears all the entries in the map.

      • 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