Class KeyInValueMap<K,V>
java.lang.Object
org.openjdk.jmc.common.collection.KeyInValueMap<K,V>
- Type Parameters:
K
- key typeV
- value type
- All Implemented Interfaces:
Iterable<V>
- Direct Known Subclasses:
EntryHashMap
A map for values that has the key inside the value, so only storing the value directly without a
wrapping map entry is sufficient. It can also compute absent values which is not available in
Java 7 maps.
Does not support null keys or values. Not thread safe.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract V
computeValue
(K key) This method must be overridden in subclasses so that values can be computed for missing keys.Get the value for a key and optionally compute a new value if it is not already present in the map.protected abstract K
Get the key for a value.protected int
Calculate the hash for a key.iterator()
int
size()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
KeyInValueMap
public KeyInValueMap(int initialCapacity, float loadFactor) Create a new map.- Parameters:
initialCapacity
- initial storage capacityloadFactor
- load factor at which to increase the internal storage capacity
-
-
Method Details
-
get
Get the value for a key and optionally compute a new value if it is not already present in the map. Automatic value computation is done withcomputeValue(Object)
which must be implemented by subclasses.- Parameters:
key
- keycomputeIfAbsent
- If a value is not found and this is set totrue
, then compute and add a new value usingcomputeValue(Object)
.- Returns:
- The value for the key. If computeIfAbsent is
false
and no matching value exists, thennull
is returned.
-
size
public int size() -
iterator
-
computeValue
This method must be overridden in subclasses so that values can be computed for missing keys. Called byget(Object, boolean)
when the requested key is missing in the map.- Parameters:
key
- key to calculate value for- Returns:
- calculated value
-
hashKey
Calculate the hash for a key. May be overridden by subclasses.- Parameters:
key
- key to calculate hash for- Returns:
- hash for key
-
getKey
Get the key for a value.- Parameters:
value
- value to get key for- Returns:
- key for value
-