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.
Note that 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 and Type | Field and Description |
|---|---|
| private static final long | serialVersionUID
Serialisation version |
| Access | Constructor and Description |
|---|---|
| public | |
| protected | MultiKeyMap(final AbstractHashedMap<MultiKey<? extends K>, V>
the map to decorate map)Constructor that decorates the specified map and is called from
|
| Modifier and Type | Method and Description |
|---|---|
| protected void | |
| public MultiKeyMap | Returns: a shallow cloneOverrides java. |
| public boolean | Returns: true if the map contains the keythe first key key1, final Object the second key key2)Checks whether the map contains the specified multi-key. |
| public boolean | Returns: true if the map contains the keythe first key key1, final Object the second key key2, final Object the third key key3)Checks whether the map contains the specified multi-key. |
| public boolean | Returns: true if the map contains the keythe first key key1, final Object the second key key2, final Object the third key key3, final Object the fourth key key4)Checks whether the map contains the specified multi-key. |
| public boolean | |
| protected AbstractHashedMap | decorated()
Overrides org. |
| public V | |
| public V | |
| public V | |
| public V | |
| protected int | |
| protected int | |
| protected int | |
| protected int | |
| protected boolean | Returns: true if the key matchesthe entry to compare to entry,the first key key1, final Object the second key key2)Is the key equal to the combined key. |
| protected boolean | Returns: true if the key matchesthe entry to compare to entry,the first key key1, final Object the second key key2, final Object the third key key3)Is the key equal to the combined key. |
| protected boolean | Returns: true if the key matchesthe entry to compare to entry,the first key key1, final Object the second key key2, final Object the third key key3, final Object the fourth key key4)Is the key equal to the combined key. |
| protected boolean | Returns: true if the key matchesthe entry to compare to entry,the first key key1, final Object the second key key2, final Object the third key key3, final Object the fourth key key4, final Object the fifth key key5)Is the key equal to the combined key. |
| public MapIterator | mapIterator()
Overrides org. Implements org. MapIterator over the map.
|
| public static < the key type K, the value type V> MultiKeyMap | Returns: a new multi key mapthe map to decorate, not null map)Decorates the specified map to add the MultiKeyMap API and fast query. |
| public V | Returns: the value previously mapped to this combined key, null if nonethe first key key1, final K the second key key2, final V the value to store value)Stores the value against the specified multi-key. |
| public V | Returns: the value previously mapped to this combined key, null if nonethe first key key1, final K the second key key2, final K the third key key3, final V the value to store value)Stores the value against the specified multi-key. |
| public V | Returns: the value previously mapped to this combined key, null if nonethe first key key1, final K the second key key2, final K the third key key3, final K the fourth key key4, final V the value to store value)Stores the value against the specified multi-key. |
| public V | Returns: the value previously mapped to this combined key, null if nonethe first key key1, final K the second key key2, final K the third key key3, final K the fourth key key4, final K the fifth key key5, final V the value to store value)Stores the value against the specified multi-key. |
| public V | Returns: the previous value for the keythe non-null MultiKey object key, final V the value to store value)Overrides org. Implements java. |
| public void | putAll(final Map<? extends MultiKey<? extends K>, ? extends V>
to this map mapToCopy)Overrides org. Implements java. |
| private void | |
| public boolean | |
| public boolean | |
| public boolean | |
| public boolean | |
| public V | Returns: the value mapped to the removed key, null if key not in mapthe first key key1, final Object the second key key2)Removes the specified multi-key from this map. |
| public V | Returns: the value mapped to the removed key, null if key not in mapthe first key key1, final Object the second key key2, final Object the third key key3)Removes the specified multi-key from this map. |
| public V | Returns: the value mapped to the removed key, null if key not in mapthe first key key1, final Object the second key key2, final Object the third key key3, final Object the fourth key key4)Removes the specified multi-key from this map. |
| public V | |
| private void |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Serialisation version | |
| MultiKeyMap | back to summary |
|---|---|
| public MultiKeyMap() Constructs a new MultiKeyMap that decorates a | |
| MultiKeyMap | back to summary |
|---|---|
| protected MultiKeyMap(final AbstractHashedMap<MultiKey<? extends K>, V> map) Constructor that decorates the specified map and is called from
| |
| checkKey | back to summary |
|---|---|
| protected void checkKey(final MultiKey<?> key) Check to ensure that input keys are valid MultiKey objects.
| |
| clone | back to summary |
|---|---|
| public MultiKeyMap Overrides java. Clones the map without cloning the keys or values.
| |
| containsKey | back to summary |
|---|---|
| public boolean containsKey(final Object key1, final Object key2) Checks whether the map contains the specified multi-key. | |
| containsKey | back to summary |
|---|---|
| public boolean containsKey(final Object key1, final Object key2, final Object key3) Checks whether the map contains the specified multi-key. | |
| containsKey | back to summary |
|---|---|
| public boolean containsKey(final Object key1, final Object key2, final Object key3, final Object key4) Checks whether the map contains the specified multi-key. | |
| containsKey | back to summary |
|---|---|
| public boolean containsKey(final Object key1, final Object key2, final Object key3, final Object key4, final Object key5) Checks whether the map contains the specified multi-key. | |
| decorated | back to summary |
|---|---|
| protected AbstractHashedMap Overrides org. Doc from org. Gets the map being decorated.
| |
| get | back to summary |
|---|---|
| public V get(final Object key1, final Object key2) Gets the value mapped to the specified multi-key. | |
| get | back to summary |
|---|---|
| public V get(final Object key1, final Object key2, final Object key3) Gets the value mapped to the specified multi-key. | |
| get | back to summary |
|---|---|
| public V get(final Object key1, final Object key2, final Object key3, final Object key4) Gets the value mapped to the specified multi-key. | |
| get | back to summary |
|---|---|
| public V get(final Object key1, final Object key2, final Object key3, final Object key4, final Object key5) Gets the value mapped to the specified multi-key. | |
| hash | back to summary |
|---|---|
| protected int hash(final Object key1, final Object key2) Gets the hash code for the specified multi-key. | |
| hash | back to summary |
|---|---|
| protected int hash(final Object key1, final Object key2, final Object key3) Gets the hash code for the specified multi-key. | |
| hash | back to summary |
|---|---|
| protected int hash(final Object key1, final Object key2, final Object key3, final Object key4) Gets the hash code for the specified multi-key. | |
| hash | back to summary |
|---|---|
| protected int hash(final Object key1, final Object key2, final Object key3, final Object key4, final Object key5) Gets the hash code for the specified multi-key. | |
| isEqualKey | back to summary |
|---|---|
| protected boolean isEqualKey(final AbstractHashedMap. Is the key equal to the combined key.
| |
| isEqualKey | back to summary |
|---|---|
| protected boolean isEqualKey(final AbstractHashedMap. Is the key equal to the combined key. | |
| isEqualKey | back to summary |
|---|---|
| protected boolean isEqualKey(final AbstractHashedMap. Is the key equal to the combined key. | |
| isEqualKey | back to summary |
|---|---|
| protected boolean isEqualKey(final AbstractHashedMap. Is the key equal to the combined key. | |
| mapIterator | back to summary |
|---|---|
| public MapIterator Overrides org. Implements org. Doc from org. Obtains a 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<String,Integer> map = new HashedMap<String,Integer>();
MapIterator<String,Integer> it = map.mapIterator();
while (it.hasNext()) {
String key = it.next();
Integer value = it.getValue();
it.setValue(value + 1);
}
| |
| multiKeyMap | back to summary |
|---|---|
| public static <K, V> MultiKeyMap Decorates the specified map to add the MultiKeyMap API and fast query. The map must not be null and must be empty.
| |
| put | back to summary |
|---|---|
| public V put(final K key1, final K key2, final V value) Stores the value against the specified multi-key.
| |
| put | back to summary |
|---|---|
| public V put(final K key1, final K key2, final K key3, final V value) Stores the value against the specified multi-key.
| |
| put | back to summary |
|---|---|
| public V put(final K key1, final K key2, final K key3, final K key4, final V value) Stores the value against the specified multi-key.
| |
| put | back to summary |
|---|---|
| public V put(final K key1, final K key2, final K key3, final K key4, final K key5, final V value) Stores the value against the specified multi-key.
| |
| put | back to summary |
|---|---|
| public V put(final MultiKey<? extends K> key, final V value) Overrides org. Implements java. Puts the key and value into the map, where the key must be a non-null MultiKey object.
| |
| putAll | back to summary |
|---|---|
| public void putAll(final Map<? extends MultiKey<? extends K>, ? extends V> mapToCopy) Overrides org. Implements java. Copies all of the keys and values from the specified map to this map. Each key must be non-null and a MultiKey object.
| |
| readObject | back to summary |
|---|---|
| private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException Read the map in using a custom routine.
| |
| removeAll | back to summary |
|---|---|
| public boolean removeAll(final Object key1) Removes all mappings where the first key is that specified.
This method removes all the mappings where the
| |
| removeAll | back to summary |
|---|---|
| public boolean removeAll(final Object key1, final Object key2) Removes all mappings where the first two keys are those specified.
This method removes all the mappings where the | |
| removeAll | back to summary |
|---|---|
| public boolean removeAll(final Object key1, final Object key2, final Object key3) Removes all mappings where the first three keys are those specified.
This method removes all the mappings where the | |
| removeAll | back to summary |
|---|---|
| public boolean removeAll(final Object key1, final Object key2, final Object key3, final Object key4) Removes all mappings where the first four keys are those specified.
This method removes all the mappings where the | |
| removeMultiKey | back to summary |
|---|---|
| public V removeMultiKey(final Object key1, final Object key2) Removes the specified multi-key from this map. | |
| removeMultiKey | back to summary |
|---|---|
| public V removeMultiKey(final Object key1, final Object key2, final Object key3) Removes the specified multi-key from this map. | |
| removeMultiKey | back to summary |
|---|---|
| public V removeMultiKey(final Object key1, final Object key2, final Object key3, final Object key4) Removes the specified multi-key from this map. | |
| removeMultiKey | back to summary |
|---|---|
| public V removeMultiKey(final Object key1, final Object key2, final Object key3, final Object key4, final Object key5) Removes the specified multi-key from this map. | |
| writeObject | back to summary |
|---|---|
| private void writeObject(final ObjectOutputStream out) throws IOException Write the map out using a custom routine.
| |