K
- the type of keys maintained by this mapV
- the type of mapped valuespublic class DefaultHashMap<K,V> extends HashMap<K,V>
DefaultHashMap
is a subclass of HashMap
that overrides
only the method HashMap.get(Object)
. This class accepts as argument
an instance factory. If a certain key is missing in the map,
the class produces a default instance and returns it as a value.
The remaining functionality is the same as for the HashMap
class
and is not documented here.
An example on how to use DefaultHashMap
:
DefaultHashMap<Integer, List<String>> map = new DefaultHashMap(() -> new ArrayList<>());
map.get(11).add("first");
HashMap
,
Serialized FormAbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Constructor and Description |
---|
DefaultHashMap(NullaryOperator<V> operator)
Constructs an empty HashMap with the default initial capacity
(16) and the default load factor (0.75).
|
DefaultHashMap(NullaryOperator<V> operator,
int initialCapacity)
Constructs an empty HashMap with the specified initial
capacity and the default load factor (0.75).
|
DefaultHashMap(NullaryOperator<V> operator,
int initialCapacity,
float loadFactor)
Constructs an empty HashMap with the specified initial
capacity and load factor.
|
DefaultHashMap(NullaryOperator<V> operator,
Map<? extends K,? extends V> otherMap)
Constructs a new HashMap with the same mappings as the
specified Map.
|
Modifier and Type | Method and Description |
---|---|
V |
get(Object key)
Returns the value to which the specified key is mapped,
or a default value generated by the instance factory if this map
contains no mapping for the key.
|
clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, getOrDefault, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
equals, hashCode, toString
public DefaultHashMap(NullaryOperator<V> operator, int initialCapacity, float loadFactor)
operator
- the instance factoryinitialCapacity
- the initial capacityloadFactor
- the load factorIllegalArgumentException
- if the initial capacity is negative
or the load factor is nonpositivepublic DefaultHashMap(NullaryOperator<V> operator, int initialCapacity)
operator
- the instance factoryinitialCapacity
- the initial capacity.IllegalArgumentException
- if the initial capacity is negative.public DefaultHashMap(NullaryOperator<V> operator)
operator
- the instance factorypublic DefaultHashMap(NullaryOperator<V> operator, Map<? extends K,? extends V> otherMap)
operator
- the instance factoryotherMap
- the map whose mappings are to be placed in this mapNullPointerException
- if the specified map is nullpublic final V get(Object key)
By calling this method multiple times passing the same key, the method will return the same value (idempotent).
Copyright © 2016. All rights reserved.