Class DefaultMap<K,​V>

java.lang.Object
java.util.AbstractMap<K,​V>
java.util.HashMap<K,​V>
uk.ac.manchester.spinnaker.utils.DefaultMap<K,​V>
Type Parameters:
K - The type of keys.
V - The type of values.
All Implemented Interfaces:
Serializable, Cloneable, Map<K,​V>

public class DefaultMap<K,​V>
extends HashMap<K,​V>
A map that will extend itself with new values (on get) when the key is otherwise absent from the map. Note that this map is only safely serializable if the default value is literal and serializable, in addition to the usual constraints of maps.
Author:
Donal Fellows
See Also:
Serialized Form
  • Constructor Details

    • DefaultMap

      public DefaultMap​(Supplier<? extends V> defaultFactory)
      Create a new map.

      The defaultFactory is a method to generate an object. If the default value is mutable it is highly recommended to pass in a method like this, and not an object. For example use ArrayList::new and not new ArrayList() otherwise the single value will be used every time and values added after one get will be in the default for the next get.

      Parameters:
      defaultFactory - A method to create a new value/object to insert in the map.
  • Method Details

    • newMapWithDefault

      public static <K,​ DV> DefaultMap<K,​DV> newMapWithDefault​(DV defaultValue)
      Create a new map.
      Type Parameters:
      K - The type of keys.
      DV - The type of the default value.
      Parameters:
      defaultValue - The default value to use in the map. This should be an immutable value as it can be potentially inserted for many keys. Must not be null.
      Returns:
      The new default map.
    • newAdvancedDefaultMap

      public static <K,​ V> DefaultMap<K,​V> newAdvancedDefaultMap​(DefaultMap.KeyAwareFactory<? super K,​? extends V> keyAwareFactory)
      Create a new map that manufactures new elements that are aware of their key from the beginning. This is done through this method because otherwise it clashes with the more common case of the unaware factory.

      The Factory can be a lambda method to create a me value based on the key.
      For example:

      DefaultMap.newAdvancedDefaultMap(i -> i*2);

      The Factory can also be a Object of a class that implements the KeyAwareFactory interface.

      Type Parameters:
      K - The type of keys.
      V - The type of values.
      Parameters:
      keyAwareFactory - Method or Object to create the default values.
      Returns:
      The new default map.
    • get

      public V get​(Object key)
      Gets a value from the dictionary, inserting a newly manufactured value if the key has no mapping.
      Specified by:
      get in interface Map<K,​V>
      Overrides:
      get in class HashMap<K,​V>
    • compute

      public V compute​(K key, BiFunction<? super K,​? super V,​? extends V> remappingFunction)

      NB: This converts nulls into the correct default value.

      Specified by:
      compute in interface Map<K,​V>
      Overrides:
      compute in class HashMap<K,​V>
    • computeIfAbsent

      public V computeIfAbsent​(K key, Function<? super K,​? extends V> mappingFunction)

      NB: This converts nulls into the correct default value.

      Specified by:
      computeIfAbsent in interface Map<K,​V>
      Overrides:
      computeIfAbsent in class HashMap<K,​V>
    • computeIfPresent

      public V computeIfPresent​(K key, BiFunction<? super K,​? super V,​? extends V> remappingFunction)

      NB: This converts nulls into the correct default value.

      Specified by:
      computeIfPresent in interface Map<K,​V>
      Overrides:
      computeIfPresent in class HashMap<K,​V>
    • merge

      public V merge​(K key, V value, BiFunction<? super V,​? super V,​? extends V> remappingFunction)

      NB: This converts nulls into the correct default value.

      Specified by:
      merge in interface Map<K,​V>
      Overrides:
      merge in class HashMap<K,​V>