public class MultiKeyMap<K,V> extends AbstractMapDecorator<MultiKey <? extends K> ,V> implements Serializable , Cloneable
Map implementation that uses multiple keys to map the value.
This class is the most efficient way to uses multiple keys to map to a value. The best way to use this class is via the additional map-style methods. These provide get, containsKey, put and remove for individual keys which operate without extra object creation.
The additional methods are the main interface of this map. As such, you will not normally hold this map in a variable of type Map.
The normal map methods take in and return a MultiKey. If you try to use put() with any other object type a ClassCastException is thrown. If you try to use null as the key in put() a NullPointerException is thrown.
This map is implemented as a decorator of a AbstractHashedMap which enables extra behaviour to be added easily.
MultiKeyMap.decorate(new LinkedMap()) creates an ordered map. MultiKeyMap.decorate(new LRUMap()) creates an least recently used map. MultiKeyMap.decorate(new ReferenceMap()) creates a garbage collector sensitive map. IdentityMap and
ReferenceIdentityMap are unsuitable for use as the key comparison would work on the whole MultiKey, not the elements within.
As an example, consider a least recently used cache that uses a String airline code and a Locale to lookup the airline's name:
private MultiKeyMap cache = MultiKeyMap.multiKeyMap(new LRUMap(50));
public String getAirlineName(String code, String locale) {
String name = (String) cache.get(code, locale);
if (name == null) {
name = getAirlineNameFromDB(code, locale);
cache.put(code, locale, name);
}
return name;
}
Note that MultiKeyMap is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. This class may throw exceptions when accessed by concurrent threads without synchronization.
| Modifier | Constructor and Description |
|---|---|
|
MultiKeyMap()
Constructs a new MultiKeyMap that decorates a
HashedMap.
|
protected |
MultiKeyMap(AbstractHashedMap
Constructor that decorates the specified map and is called from
multiKeyMap(AbstractHashedMap).
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
checkKey(MultiKey
Check to ensure that input keys are valid MultiKey objects.
|
MultiKeyMap |
clone()
Clones the map without cloning the keys or values.
|
boolean |
containsKey(Object
Checks whether the map contains the specified multi-key.
|
boolean |
containsKey(Object
Checks whether the map contains the specified multi-key.
|
boolean |
containsKey(Object
Checks whether the map contains the specified multi-key.
|
boolean |
containsKey(Object
Checks whether the map contains the specified multi-key.
|
protected AbstractHashedMap |
decorated()
Gets the map being decorated.
|
V |
get(Object
Gets the value mapped to the specified multi-key.
|
V |
get(Object
Gets the value mapped to the specified multi-key.
|
V |
get(Object
Gets the value mapped to the specified multi-key.
|
V |
get(Object
Gets the value mapped to the specified multi-key.
|
protected int |
hash(Object
Gets the hash code for the specified multi-key.
|
protected int |
hash(Object
Gets the hash code for the specified multi-key.
|
protected int |
hash(Object
Gets the hash code for the specified multi-key.
|
protected int |
hash(Object
Gets the hash code for the specified multi-key.
|
protected boolean |
isEqualKey(AbstractHashedMap
Is the key equal to the combined key.
|
protected boolean |
isEqualKey(AbstractHashedMap
Is the key equal to the combined key.
|
protected boolean |
isEqualKey(AbstractHashedMap
Is the key equal to the combined key.
|
protected boolean |
isEqualKey(AbstractHashedMap
Is the key equal to the combined key.
|
MapIterator |
mapIterator()
Obtains a
MapIterator over the map.
|
static <K |
multiKeyMap(AbstractHashedMap
Decorates the specified map to add the MultiKeyMap API and fast query.
|
V |
put(K key1, K key2, K key3, K key4, K key5, V value)
Stores the value against the specified multi-key.
|
V |
put(K key1, K key2, K key3, K key4, V value)
Stores the value against the specified multi-key.
|
V |
put(K key1, K key2, K key3, V value)
Stores the value against the specified multi-key.
|
V |
put(K key1, K key2, V value)
Stores the value against the specified multi-key.
|
V |
put(MultiKey
Puts the key and value into the map, where the key must be a non-null MultiKey object.
|
void |
putAll(Map
Copies all of the keys and values from the specified map to this map.
|
boolean |
removeAll(Object
Removes all mappings where the first key is that specified.
|
boolean |
removeAll(Object
Removes all mappings where the first two keys are those specified.
|
boolean |
removeAll(Object
Removes all mappings where the first three keys are those specified.
|
boolean |
removeAll(Object
Removes all mappings where the first four keys are those specified.
|
V |
removeMultiKey(Object
Removes the specified multi-key from this map.
|
V |
removeMultiKey(Object
Removes the specified multi-key from this map.
|
V |
removeMultiKey(Object
Removes the specified multi-key from this map.
|
V |
removeMultiKey(Object
Removes the specified multi-key from this map.
|
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, remove, size, toString, valuesfinalize, getClass, notify, notifyAll, wait, wait, waitcompute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAllpublic MultiKeyMap()
HashedMap.
protected MultiKeyMap(AbstractHashedMap<MultiKey <? extends K> ,V> map)
multiKeyMap(AbstractHashedMap). The map must not be null and should be empty or only contain valid keys. This constructor performs no validation.
map - the map to decorate
public static <K,V> MultiKeyMap <K ,V> multiKeyMap(AbstractHashedMap <MultiKey <? extends K> ,V> map)
K - the key type
V - the value type
map - the map to decorate, not null
NullPointerException - if map is null
IllegalArgumentException - if the map is not empty
public V get(Objectkey1, Object key2)
key1 - the first key
key2 - the second key
public boolean containsKey(Objectkey1, Object key2)
key1 - the first key
key2 - the second key
public V put(K key1, K key2, V value)
key1 - the first key
key2 - the second key
value - the value to store
public V removeMultiKey(Objectkey1, Object key2)
key1 - the first key
key2 - the second key
protected int hash(Objectkey1, Object key2)
key1 - the first key
key2 - the second key
protected boolean isEqualKey(AbstractHashedMap.HashEntry <MultiKey <? extends K> ,V> entry, Object key1, Object key2)
entry - the entry to compare to
key1 - the first key
key2 - the second key
public V get(Objectkey1, Object key2, Object key3)
key1 - the first key
key2 - the second key
key3 - the third key
public boolean containsKey(Objectkey1, Object key2, Object key3)
key1 - the first key
key2 - the second key
key3 - the third key
public V put(K key1, K key2, K key3, V value)
key1 - the first key
key2 - the second key
key3 - the third key
value - the value to store
public V removeMultiKey(Objectkey1, Object key2, Object key3)
key1 - the first key
key2 - the second key
key3 - the third key
protected int hash(Objectkey1, Object key2, Object key3)
key1 - the first key
key2 - the second key
key3 - the third key
protected boolean isEqualKey(AbstractHashedMap.HashEntry <MultiKey <? extends K> ,V> entry, Object key1, Object key2, Object key3)
entry - the entry to compare to
key1 - the first key
key2 - the second key
key3 - the third key
public V get(Objectkey1, Object key2, Object key3, Object key4)
key1 - the first key
key2 - the second key
key3 - the third key
key4 - the fourth key
public boolean containsKey(Objectkey1, Object key2, Object key3, Object key4)
key1 - the first key
key2 - the second key
key3 - the third key
key4 - the fourth key
public V put(K key1, K key2, K key3, K key4, V value)
key1 - the first key
key2 - the second key
key3 - the third key
key4 - the fourth key
value - the value to store
public V removeMultiKey(Objectkey1, Object key2, Object key3, Object key4)
key1 - the first key
key2 - the second key
key3 - the third key
key4 - the fourth key
protected int hash(Objectkey1, Object key2, Object key3, Object key4)
key1 - the first key
key2 - the second key
key3 - the third key
key4 - the fourth key
protected boolean isEqualKey(AbstractHashedMap.HashEntry <MultiKey <? extends K> ,V> entry, Object key1, Object key2, Object key3, Object key4)
entry - the entry to compare to
key1 - the first key
key2 - the second key
key3 - the third key
key4 - the fourth key
public V get(Objectkey1, Object key2, Object key3, Object key4, Object key5)
key1 - the first key
key2 - the second key
key3 - the third key
key4 - the fourth key
key5 - the fifth key
public boolean containsKey(Objectkey1, Object key2, Object key3, Object key4, Object key5)
key1 - the first key
key2 - the second key
key3 - the third key
key4 - the fourth key
key5 - the fifth key
public V put(K key1, K key2, K key3, K key4, K key5, V value)
key1 - the first key
key2 - the second key
key3 - the third key
key4 - the fourth key
key5 - the fifth key
value - the value to store
public V removeMultiKey(Objectkey1, Object key2, Object key3, Object key4, Object key5)
key1 - the first key
key2 - the second key
key3 - the third key
key4 - the fourth key
key5 - the fifth key
protected int hash(Objectkey1, Object key2, Object key3, Object key4, Object key5)
key1 - the first key
key2 - the second key
key3 - the third key
key4 - the fourth key
key5 - the fifth key
protected boolean isEqualKey(AbstractHashedMap.HashEntry <MultiKey <? extends K> ,V> entry, Object key1, Object key2, Object key3, Object key4, Object key5)
entry - the entry to compare to
key1 - the first key
key2 - the second key
key3 - the third key
key4 - the fourth key
key5 - the fifth key
public boolean removeAll(Objectkey1)
This method removes all the mappings where the MultiKey has one or more keys, and the first matches that specified.
key1 - the first key
public boolean removeAll(Objectkey1, Object key2)
This method removes all the mappings where the MultiKey has two or more keys, and the first two match those specified.
key1 - the first key
key2 - the second key
public boolean removeAll(Objectkey1, Object key2, Object key3)
This method removes all the mappings where the MultiKey has three or more keys, and the first three match those specified.
key1 - the first key
key2 - the second key
key3 - the third key
public boolean removeAll(Objectkey1, Object key2, Object key3, Object key4)
This method removes all the mappings where the MultiKey has four or more keys, and the first four match those specified.
key1 - the first key
key2 - the second key
key3 - the third key
key4 - the fourth key
protected void checkKey(MultiKey<?> key)
key - the key to check
public MultiKeyMap<K ,V> clone()
public V put(MultiKey<? extends K> key, V value)
put in interface
Map<MultiKey<? extends K>,V>
put in interface
Put<MultiKey<? extends K>,V>
put in class
AbstractMapDecorator<MultiKey<? extends K>,V>
key - the non-null MultiKey object
value - the value to store
NullPointerException - if the key is null
ClassCastException - if the key is not a MultiKey
Map.put(Object, Object)
public void putAll(Map<? extends MultiKey <? extends K> ,? extends V> mapToCopy)
putAll in interface
Map<MultiKey<? extends K>,V>
putAll in interface
Put<MultiKey<? extends K>,V>
putAll in class
AbstractMapDecorator<MultiKey<? extends K>,V>
mapToCopy - to this map
NullPointerException - if the mapToCopy or any key within is null
ClassCastException - if any key in mapToCopy is not a MultiKey
Map.putAll(Map)
public MapIterator<MultiKey <? extends K> ,V> mapIterator()
AbstractIterableMap
MapIterator over the map.
A map iterator is an efficient way of iterating over maps. There is no need to access the entry set or use Map Entry objects.
IterableMap
map = new HashedMap
(); MapIterator
it = map.mapIterator(); while (it.hasNext()) { String key = it.next(); Integer value = it.getValue(); it.setValue(value + 1); }
mapIterator in interface
IterableGet<MultiKey<? extends K>,V>
mapIterator in class
AbstractIterableMap<MultiKey<? extends K>,V>
protected AbstractHashedMap<MultiKey <? extends K> ,V> decorated()
decorated in class
AbstractMapDecorator<MultiKey<? extends K>,V>