K
- key typeV
- value typepublic abstract class KeyInValueMap<K,V>
extends java.lang.Object
implements java.lang.Iterable<V>
Does not support null keys or values. Not thread safe.
Modifier and Type | Field and Description |
---|---|
private int |
capacity |
private float |
loadFactor |
private int |
size |
private int |
threshold |
private java.lang.Object[] |
values |
Constructor and Description |
---|
KeyInValueMap(int initialCapacity,
float loadFactor)
Create a new map.
|
Modifier and Type | Method and Description |
---|---|
protected abstract V |
computeValue(K key)
This method must be overridden in subclasses so that values can be computed for missing keys.
|
private void |
createTable(int newCapacity) |
V |
get(K key,
boolean computeIfAbsent)
Get the value for a key and optionally compute a new value if it is not already present in
the map.
|
private int |
getIndex(int hash) |
protected abstract K |
getKey(V value)
Get the key for a value.
|
private V |
getValue(int index) |
private int |
hashFromValue(V value) |
protected int |
hashKey(K key)
Calculate the hash for a key.
|
private boolean |
isKeyFor(K key,
V value)
This method must be overridden in subclasses so that values can be checked if they match a
given key.
|
java.util.Iterator<V> |
iterator() |
private void |
rehash() |
int |
size() |
private java.lang.Object[] values
private int size
private int capacity
private int threshold
private final float loadFactor
public KeyInValueMap(int initialCapacity, float loadFactor)
initialCapacity
- initial storage capacityloadFactor
- load factor at which to increase the internal storage capacitypublic V get(K key, boolean computeIfAbsent)
computeValue(Object)
which must be
implemented by subclasses.key
- keycomputeIfAbsent
- If a value is not found and this is set to true
, then compute and add a
new value using computeValue(Object)
.false
and no matching value
exists, then null
is returned.public int size()
public java.util.Iterator<V> iterator()
iterator
in interface java.lang.Iterable<V>
private void createTable(int newCapacity)
private V getValue(int index)
private void rehash()
private int getIndex(int hash)
private boolean isKeyFor(K key, V value)
get(Object, boolean)
when there are multiple values that share
the same key hash.key
- key to checkvalue
- value to checktrue
if key
is the key for value
, false
otherwiseprotected abstract V computeValue(K key)
get(Object, boolean)
when the requested key is missing in the map.key
- key to calculate value forprotected int hashKey(K key)
key
- key to calculate hash forprivate int hashFromValue(V value)