Class 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>
- Author:
- Donal Fellows
- See Also:
- Serialized Form
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
DefaultMap.KeyAwareFactory<K,V>
An advanced factory that has access to the key when it is creating the value to associate with it.Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
-
Constructor Summary
Constructors Constructor Description DefaultMap(Supplier<? extends V> defaultFactory)
Create a new map. -
Method Summary
Modifier and Type Method Description V
compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
V
computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
V
computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
V
get(Object key)
Gets a value from the dictionary, inserting a newly manufactured value if the key has no mapping.V
merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
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 java.util.HashMap
clear, clone, containsKey, containsValue, entrySet, forEach, getOrDefault, isEmpty, keySet, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
Constructor Details
-
DefaultMap
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 useArrayList::new
and 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
Gets a value from the dictionary, inserting a newly manufactured value if the key has no mapping. -
compute
NB: This converts
null
s into the correct default value. -
computeIfAbsent
NB: This converts
null
s into the correct default value.- Specified by:
computeIfAbsent
in interfaceMap<K,V>
- Overrides:
computeIfAbsent
in classHashMap<K,V>
-
computeIfPresent
NB: This converts
null
s into the correct default value.- Specified by:
computeIfPresent
in interfaceMap<K,V>
- Overrides:
computeIfPresent
in classHashMap<K,V>
-
merge
NB: This converts
null
s into the correct default value.
-