Class DefaultedMap
- java.lang.Object
-
- org.apache.commons.collections.map.AbstractMapDecorator
-
- org.apache.commons.collections.map.DefaultedMap
-
- All Implemented Interfaces:
Serializable
,Map
@Deprecated(since="2021-04-30") public class DefaultedMap extends AbstractMapDecorator implements Map, Serializable
Deprecated.Commons Collections 3 is in maintenance mode. Commons Collections 4 should be used instead.Decorates anotherMap
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 theMap.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 behaviour 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:
- Commons Collections 3.2
- See Also:
LazyMap
, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description DefaultedMap(Object defaultValue)
Deprecated.Constructs a new emptyDefaultedMap
that decorates aHashMap
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Map
decorate(Map map, Object defaultValue)
Deprecated.Factory method to create a defaulting map.static Map
decorate(Map map, Factory factory)
Deprecated.Factory method to create a defaulting map.static Map
decorate(Map map, Transformer factory)
Deprecated.Factory method to create a defaulting map.Object
get(Object key)
Deprecated.-
Methods inherited from class org.apache.commons.collections.map.AbstractMapDecorator
clear, containsKey, containsValue, entrySet, equals, hashCode, isEmpty, keySet, put, putAll, remove, size, toString, values
-
Methods inherited from interface java.util.Map
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
-
-
-
Constructor Detail
-
DefaultedMap
public DefaultedMap(Object defaultValue)
Deprecated.Constructs a new emptyDefaultedMap
that 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 Detail
-
decorate
public static Map decorate(Map map, Object defaultValue)
Deprecated.Factory method to create a defaulting map.The value specified is returned when a missing key is found.
- Parameters:
map
- the map to decorate, must not be nulldefaultValue
- the default value to return when the key is not found- Throws:
IllegalArgumentException
- if map is null
-
decorate
public static Map decorate(Map map, Factory factory)
Deprecated.Factory 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.
- Parameters:
map
- the map to decorate, must not be nullfactory
- the factory to use, must not be null- Throws:
IllegalArgumentException
- if map or factory is null
-
decorate
public static Map decorate(Map map, Transformer factory)
Deprecated.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.
- Parameters:
map
- the map to decorate, must not be nullfactory
- the factory to use, must not be null- Throws:
IllegalArgumentException
- if map or factory is null
-
-