Class MutableMap<K,V>

java.lang.Object
me.sbasalaev.collection.Multimap<K,V,Opt<V>>
me.sbasalaev.collection.Map<K,V>
me.sbasalaev.collection.MutableMap<K,V>
All Implemented Interfaces:
Cloneable, MultimapMutator<K,V,Opt<V>>

public abstract class MutableMap<K,V> extends Map<K,V> implements MultimapMutator<K,V,Opt<V>>
Map that can be mutated.
  • Constructor Details

    • MutableMap

      public MutableMap()
      Constructor for subclasses.
  • Method Details

    • empty

      public static <K, V> MutableMap<K,V> empty()
      Returns new mutable map that is initially empty.
    • copyOf

      public static <K, V> MutableMap<K,V> copyOf(Map<K,V> map)
      Returns new mutable map that initially contains given values.
    • set

      public abstract Opt<V> set(K key, V value)
      Associates the value with the key in this map.
      Returns:
      value that was previously associated with the key or empty optional if there was none.
    • add

      public boolean add(K key, V value)
      Associates the value with the key in this map.
      Specified by:
      add in interface MultimapMutator<K,V,Opt<V>>
      Returns:
      false if the value was already associated with this key, true otherwise.
    • createIfMissing

      public V createIfMissing(K key, Supplier<? extends V> supplier)
      Returns value associated with given key, adds it if no value was associated. If a mapping already exists in this map, just returns the value. Otherwise, given supplier is used to retrieve a value, it is put in this map and then returned.
    • createOrUpdate

      public V createOrUpdate(K key, Supplier<? extends V> supplier, UnaryOperator<V> transformer)
      Creates or updates association with given key in this map. If given key is not in this map, given supplier is used to retrieve a value, it is put in this map and returned. If the map contains given key, the associated value is transformed using supplied transformer, the new association is put in this map and then new value is returned.
    • updateIfPresent

      public Opt<V> updateIfPresent(K key, UnaryOperator<V> transformer)
      Updates association with given key if it is already present in this map.
      Returns:
      the new value associated with the key, or none() if the key was not present in this map.
    • removeKey

      public abstract Opt<V> removeKey(K key)
      Removes key and associated value from this map.
      Specified by:
      removeKey in interface MultimapMutator<K,V,Opt<V>>
      Returns:
      the value previously associated with this key, if any.
    • removeAllKeysMatching

      public boolean removeAllKeysMatching(Predicate<? super K> condition)
      Removes all elements matching given condition from the collection. Returns true if the collection was modified.
    • retainAllKeysMatching

      public boolean retainAllKeysMatching(Predicate<? super K> condition)
      Retains only entries with key matching given condition. Returns true if the map was modified.