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>
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAn advanced factory that has access to the key when it is creating the value to associate with it.Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncomputeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) Gets a value from the dictionary, inserting a newly manufactured value if the key has no mapping.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.static <K,DV> DefaultMap <K, DV> newMapWithDefault(DV defaultValue) Create a new map.Methods inherited from class HashMap
clear, clone, containsKey, containsValue, entrySet, forEach, getOrDefault, isEmpty, keySet, newHashMap, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, valuesMethods inherited from class AbstractMap
equals, hashCode, toString
-
Constructor Details
-
DefaultMap
Create a new map.The
defaultFactoryis 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 useArrayList::newand notnew 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
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 benull.- 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
-
compute
-
computeIfAbsent
-
computeIfPresent
NB: This converts
nulls into the correct default value.- Specified by:
computeIfPresentin interfaceMap<K,V> - Overrides:
computeIfPresentin classHashMap<K,V>
-
merge
-