Class DefaultedMap<K,V> 
- Type Parameters:
- K- the type of the keys in this map
- V- the type of the values in this map
- All Implemented Interfaces:
- Serializable,- Map<K,,- V> - Get<K,,- V> - IterableGet<K,,- V> - IterableMap<K,,- V> - Put<K,- V> 
Map returning a default value if the map
 does not contain the requested key.
 
 When the get(Object) method is called with a key that does not
 exist in the map, this map will return the default value specified in
 the constructor/factory. Only the get method is altered, so the
 Map.containsKey(Object) can be used to determine if a key really
 is in the map or not.
 
 The defaulted value is not added to the map.
 Compare this behavior with LazyMap, which does add the value
 to the map (via a Transformer).
 
For instance:
 Map map = new DefaultedMap("NULL");
 Object obj = map.get("Surname");
 // obj == "NULL"
 
 After the above code is executed the map is still empty.
 Note that DefaultedMap is not synchronized and is not thread-safe.
 If you wish to use this map from multiple threads concurrently, you must use
 appropriate synchronization. The simplest approach is to wrap this map
 using Collections.synchronizedMap(Map). This class may throw
 exceptions when accessed by concurrent threads without synchronization.
 
- Since:
- 3.2
- See Also:
- 
Nested Class Summary
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedDefaultedMap(Map<K, V> map, Transformer<? super K, ? extends V> defaultValueTransformer) Constructor that wraps (not copies).DefaultedMap(Transformer<? super K, ? extends V> defaultValueTransformer) Constructs a new emptyDefaultedMapthat decorates aHashMap.DefaultedMap(V defaultValue) Constructs a new emptyDefaultedMapthat decorates aHashMap.
- 
Method SummaryModifier and TypeMethodDescriptionstatic <K,V> DefaultedMap <K, V> defaultedMap(Map<K, V> map, Factory<? extends V> factory) Factory method to create a defaulting map.static <K,V> Map <K, V> defaultedMap(Map<K, V> map, Transformer<? super K, ? extends V> transformer) Factory method to create a defaulting map.static <K,V> DefaultedMap <K, V> defaultedMap(Map<K, V> map, V defaultValue) Factory method to create a defaulting map.Gets a value at a given key.Methods inherited from class org.apache.commons.collections4.map.AbstractMapDecoratorclear, containsKey, containsValue, decorated, entrySet, equals, hashCode, isEmpty, keySet, put, putAll, remove, size, toString, valuesMethods inherited from class org.apache.commons.collections4.map.AbstractIterableMapmapIteratorMethods inherited from class java.lang.Objectclone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Mapcompute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
- 
Constructor Details- 
DefaultedMapConstructor that wraps (not copies).- Parameters:
- map- the map to decorate, must not be null
- defaultValueTransformer- the value transformer to use
- Throws:
- NullPointerException- if map or transformer is null
 
- 
DefaultedMapConstructs a new emptyDefaultedMapthat decorates aHashMap.- Parameters:
- defaultValueTransformer- transformer to use to generate missing values.
 
- 
DefaultedMapConstructs a new emptyDefaultedMapthat decorates aHashMap.The object passed in will be returned by the map whenever an unknown key is requested. - Parameters:
- defaultValue- the default value to return when the key is not found
 
 
- 
- 
Method Details- 
defaultedMapFactory method to create a defaulting map.The factory specified is called when a missing key is found. The result will be returned as the result of the map get(key) method. - Type Parameters:
- K- the key type
- V- the value type
- Parameters:
- map- the map to decorate, must not be null
- factory- the factory to use to create entries, must not be null
- Returns:
- a new defaulting map
- Throws:
- NullPointerException- if map or factory is null
- Since:
- 4.0
 
- 
defaultedMappublic static <K,V> Map<K,V> defaultedMap(Map<K, V> map, Transformer<? super K, ? extends V> transformer) Factory method to create a defaulting map.The transformer specified is called when a missing key is found. The key is passed to the transformer as the input, and the result will be returned as the result of the map get(key) method. - Type Parameters:
- K- the key type
- V- the value type
- Parameters:
- map- the map to decorate, must not be null
- transformer- the transformer to use as a factory to create entries, must not be null
- Returns:
- a new defaulting map
- Throws:
- NullPointerException- if map or transformer is null
- Since:
- 4.0
 
- 
defaultedMapFactory method to create a defaulting map.The value specified is returned when a missing key is found. - Type Parameters:
- K- the key type
- V- the value type
- Parameters:
- map- the map to decorate, must not be null
- defaultValue- the default value to return when the key is not found
- Returns:
- a new defaulting map
- Throws:
- NullPointerException- if map is null
- Since:
- 4.0
 
- 
getDescription copied from interface:GetGets a value at a given key.- Specified by:
- getin interface- Get<K,- V> 
- Specified by:
- getin interface- Map<K,- V> 
- Overrides:
- getin class- AbstractMapDecorator<K,- V> 
- Parameters:
- key- the key whose associated value is to be returned
- Returns:
- the value to which the specified key is mapped, or nullif this map contains no mapping for the key
- See Also:
 
 
-