Class AbstractDefaultingHashMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,V>
com.onenetwork.platform.tools.collections.AbstractDefaultingHashMap<K,V>
- Type Parameters:
K
- key typeV
- value type
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.util.Map<K,V>
- Direct Known Subclasses:
DefaultingHashMap
public abstract class AbstractDefaultingHashMap<K,V>
extends java.util.HashMap<K,V>
Identical to
HashMap
, except a call to get(Object)
for a nonexistent key
will call newDefaultValue(Object)
and put() the resulting value, then return that.
This makes it possible to have a Map implementation where get() never returns null.- See Also:
- Serialized Form
-
Nested Class Summary
-
Constructor Summary
Constructors Constructor Description AbstractDefaultingHashMap()
-
Method Summary
Modifier and Type Method Description V
get(java.lang.Object key)
If a value exists for the given key, return it.protected abstract V
newDefaultValue(java.lang.Object key)
Whenget(Object)
is called for a nonexistent key, this method will be called to initialize a default value for that key and put() it in the Map.Methods inherited from class java.util.HashMap
clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, getOrDefault, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
Constructor Details
-
AbstractDefaultingHashMap
public AbstractDefaultingHashMap()
-
-
Method Details
-
get
If a value exists for the given key, return it. Else, callnewDefaultValue(Object)
to initialize a default value, put that in the Map, and return it. Note that this method assumes that the key passed in will be an instance of key type K. -
newDefaultValue
Whenget(Object)
is called for a nonexistent key, this method will be called to initialize a default value for that key and put() it in the Map.- Parameters:
key
-- Returns:
-