Class Map<K,V>

Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values
All Implemented Interfaces:
Serializable, ConcurrentMap<K,V>, Map<K,V>

public class Map<K,V> extends ConcurrentHashMap<K,V>
The Map class implements Go's sync.Map. A thread-safe map that extends ConcurrentHashMap and provides additional utility methods.
See Also:
  • Constructor Details

    • Map

      public Map()
      Creates a new, empty map with the default initial capacity, load factor, and concurrency level.
    • Map

      public Map(int initialCapacity)
      Creates a new, empty map with the specified initial capacity, and with the default load factor and concurrency level.
      Parameters:
      initialCapacity - the initial capacity. The implementation performs internal sizing to accommodate this many elements.
    • Map

      public Map(Map<? extends K,? extends V> m)
      Creates a new map with the same mappings as the specified map.
      Parameters:
      m - the map whose mappings are to be placed in this map
    • Map

      public Map(int initialCapacity, float loadFactor)
      Creates a new, empty map with the specified initial capacity and load factor, and with the default concurrency level.
      Parameters:
      initialCapacity - the initial capacity. The implementation performs internal sizing to accommodate this many elements.
      loadFactor - the load factor threshold, used to control resizing. Resizing may be performed when the average number of elements per bin exceeds this threshold.
    • Map

      public Map(int initialCapacity, float loadFactor, int concurrencyLevel)
      Creates a new, empty map with the specified initial capacity, load factor, and concurrency level.
      Parameters:
      initialCapacity - the initial capacity. The implementation performs internal sizing to accommodate this many elements.
      loadFactor - the load factor threshold, used to control resizing. Resizing may be performed when the average number of elements per bin exceeds this threshold.
      concurrencyLevel - the estimated number of concurrently updating threads. The implementation performs internal sizing to try to accommodate this many threads.
  • Method Details

    • compareAndDelete

      public boolean compareAndDelete(K key, V value)
      Removes the entry for a key only if currently mapped to a given value.
      Parameters:
      key - the key whose associated value is to be removed
      value - the value expected to be associated with the specified key
      Returns:
      true if the value was removed
    • compareAndSwap

      public boolean compareAndSwap(K key, V oldValue, V newValue)
      Replaces the entry for a key only if currently mapped to a given value.
      Parameters:
      key - the key with which the specified value is associated
      oldValue - the value expected to be associated with the specified key
      newValue - the value to be associated with the specified key
      Returns:
      true if the value was replaced
    • delete

      public void delete(K key)
      Removes the mapping for a key from this map if it is present.
      Parameters:
      key - the key whose mapping is to be removed from the map
    • load

      public V load(K key)
      Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      the value to which the specified key is mapped, or null if this map contains no mapping for the key
    • loadAndDelete

      public V loadAndDelete(K key)
      Removes the mapping for a key from this map if it is present and returns the associated value.
      Parameters:
      key - the key whose mapping is to be removed from the map
      Returns:
      the previous value associated with key, or null if there was no mapping for key
    • loadOrStore

      public V loadOrStore(K key, V value)
      If the specified key is not already associated with a value, associates it with the given value and returns null, else returns the current value.
      Parameters:
      key - the key with which the specified value is to be associated
      value - the value to be associated with the specified key
      Returns:
      the previous value associated with the specified key, or null if there was no mapping for the key
    • range

      public void range(BiConsumer<? super K,? super V> consumer)
      Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
      Parameters:
      consumer - the action to be performed for each entry
    • store

      public void store(K key, V value)
      Associates the specified value with the specified key in this map.
      Parameters:
      key - the key with which the specified value is to be associated
      value - the value to be associated with the specified key
    • swap

      public V swap(K key, V value)
      Associates the specified value with the specified key in this map and returns the previous value associated with the key, or null if there was no mapping for the key.
      Parameters:
      key - the key with which the specified value is to be associated
      value - the value to be associated with the specified key
      Returns:
      the previous value associated with key, or null if there was no mapping for key