K
- the type of the keys in this mapV
- the type of the values in this mappublic class DefaultedMap<K,V> extends AbstractMapDecorator<K,V> implements Serializable
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 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.
LazyMap
,
Serialized FormModifier | Constructor and Description |
---|---|
protected |
DefaultedMap(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 empty
DefaultedMap that decorates a HashMap . |
|
DefaultedMap(V defaultValue)
Constructs a new empty
DefaultedMap that decorates
a HashMap . |
Modifier and Type | Method and Description |
---|---|
static <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.
|
V |
get(Object key) |
clear, containsKey, containsValue, decorated, entrySet, equals, hashCode, isEmpty, keySet, put, putAll, remove, size, toString, values
mapIterator
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
public DefaultedMap(V defaultValue)
DefaultedMap
that decorates
a HashMap
.
The object passed in will be returned by the map whenever an unknown key is requested.
defaultValue
- the default value to return when the key is not foundpublic DefaultedMap(Transformer<? super K,? extends V> defaultValueTransformer)
DefaultedMap
that decorates a HashMap
.defaultValueTransformer
- transformer to use to generate missing values.protected DefaultedMap(Map<K,V> map, Transformer<? super K,? extends V> defaultValueTransformer)
map
- the map to decorate, must not be nulldefaultValueTransformer
- the value transformer to useNullPointerException
- if map or transformer is nullpublic static <K,V> DefaultedMap<K,V> defaultedMap(Map<K,V> map, V defaultValue)
The value specified is returned when a missing key is found.
K
- the key typeV
- the value typemap
- the map to decorate, must not be nulldefaultValue
- the default value to return when the key is not foundNullPointerException
- if map is nullpublic static <K,V> DefaultedMap<K,V> defaultedMap(Map<K,V> map, Factory<? extends V> factory)
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.
K
- the key typeV
- the value typemap
- the map to decorate, must not be nullfactory
- the factory to use to create entries, must not be nullNullPointerException
- if map or factory is nullpublic static <K,V> Map<K,V> defaultedMap(Map<K,V> map, Transformer<? super K,? extends V> transformer)
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.
K
- the key typeV
- the value typemap
- the map to decorate, must not be nulltransformer
- the transformer to use as a factory to create entries, must not be nullNullPointerException
- if map or factory is nullpublic V get(Object key)
get
in interface Map<K,V>
get
in interface Get<K,V>
get
in class AbstractMapDecorator<K,V>
key
- the key whose associated value is to be returnednull
if this map contains no mapping for the keyMap.get(Object)
Copyright © 2001–2018 The Apache Software Foundation. All rights reserved.