Class DefaultMap<K,V> 
- Type Parameters:
- K- The type of keys.
- V- The type of values.
- All Implemented Interfaces:
- Serializable,- Cloneable,- Map<K,- V> 
- Author:
- Donal Fellows
- See Also:
- 
Nested Class SummaryNested 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 java.util.AbstractMapAbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> 
- 
Constructor SummaryConstructors
- 
Method SummaryModifier 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 java.util.HashMapclear, clone, containsKey, containsValue, entrySet, forEach, getOrDefault, isEmpty, keySet, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, valuesMethods inherited from class java.util.AbstractMapequals, hashCode, toString
- 
Constructor Details- 
DefaultMapCreate 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- 
newMapWithDefaultCreate 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.
 
- 
newAdvancedDefaultMappublic 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.
 
- 
getGets a value from the dictionary, inserting a newly manufactured value if the key has no mapping.
- 
computeNB: This converts nulls into the correct default value.
- 
computeIfAbsentNB: This converts nulls into the correct default value.- Specified by:
- computeIfAbsentin interface- Map<K,- V> 
- Overrides:
- computeIfAbsentin class- HashMap<K,- V> 
 
- 
computeIfPresentNB: This converts nulls into the correct default value.- Specified by:
- computeIfPresentin interface- Map<K,- V> 
- Overrides:
- computeIfPresentin class- HashMap<K,- V> 
 
- 
mergeNB: This converts nulls into the correct default value.
 
-