Save This Page
Home » commons-collections-3.2.1-src » org.apache.commons » collections » map » [javadoc | source]
org.apache.commons.collections.map
public class: MultiKeyMap [javadoc | source]
java.lang.Object
   org.apache.commons.collections.map.MultiKeyMap

All Implemented Interfaces:
    IterableMap, Serializable

A 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.

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.decorate(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.

Field Summary
protected final  AbstractHashedMap map    The decorated map 
Constructor:
 public MultiKeyMap() 
 protected MultiKeyMap(AbstractHashedMap map) 
    Constructor that decorates the specified map and is called from #decorate(AbstractHashedMap) . The map must not be null and should be empty or only contain valid keys. This constructor performs no validation.
    Parameters:
    map - the map to decorate
Method from org.apache.commons.collections.map.MultiKeyMap Summary:
checkKey,   clear,   clone,   containsKey,   containsKey,   containsKey,   containsKey,   containsKey,   containsValue,   decorate,   entrySet,   equals,   get,   get,   get,   get,   get,   hash,   hash,   hash,   hash,   hashCode,   isEmpty,   isEqualKey,   isEqualKey,   isEqualKey,   isEqualKey,   keySet,   mapIterator,   put,   put,   put,   put,   put,   putAll,   remove,   remove,   remove,   remove,   remove,   removeAll,   removeAll,   removeAll,   removeAll,   size,   toString,   values
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.commons.collections.map.MultiKeyMap Detail:
 protected  void checkKey(Object key) 
    Check to ensure that input keys are valid MultiKey objects.
 public  void clear() 
 public Object clone() 
    Clones the map without cloning the keys or values.
 public boolean containsKey(Object key) 
 public boolean containsKey(Object key1,
    Object key2) 
    Checks whether the map contains the specified multi-key.
 public boolean containsKey(Object key1,
    Object key2,
    Object key3) 
    Checks whether the map contains the specified multi-key.
 public boolean containsKey(Object key1,
    Object key2,
    Object key3,
    Object key4) 
    Checks whether the map contains the specified multi-key.
 public boolean containsKey(Object key1,
    Object key2,
    Object key3,
    Object key4,
    Object key5) 
    Checks whether the map contains the specified multi-key.
 public boolean containsValue(Object value) 
 public static MultiKeyMap decorate(AbstractHashedMap map) 
    Decorates the specified map to add the MultiKeyMap API and fast query. The map must not be null and must be empty.
 public Set entrySet() 
 public boolean equals(Object obj) 
 public Object get(Object key) 
 public Object get(Object key1,
    Object key2) 
    Gets the value mapped to the specified multi-key.
 public Object get(Object key1,
    Object key2,
    Object key3) 
    Gets the value mapped to the specified multi-key.
 public Object get(Object key1,
    Object key2,
    Object key3,
    Object key4) 
    Gets the value mapped to the specified multi-key.
 public Object get(Object key1,
    Object key2,
    Object key3,
    Object key4,
    Object key5) 
    Gets the value mapped to the specified multi-key.
 protected int hash(Object key1,
    Object key2) 
    Gets the hash code for the specified multi-key.
 protected int hash(Object key1,
    Object key2,
    Object key3) 
    Gets the hash code for the specified multi-key.
 protected int hash(Object key1,
    Object key2,
    Object key3,
    Object key4) 
    Gets the hash code for the specified multi-key.
 protected int hash(Object key1,
    Object key2,
    Object key3,
    Object key4,
    Object key5) 
    Gets the hash code for the specified multi-key.
 public int hashCode() 
 public boolean isEmpty() 
 protected boolean isEqualKey(AbstractHashedMap.HashEntry entry,
    Object key1,
    Object key2) 
    Is the key equal to the combined key.
 protected boolean isEqualKey(AbstractHashedMap.HashEntry entry,
    Object key1,
    Object key2,
    Object key3) 
    Is the key equal to the combined key.
 protected boolean isEqualKey(AbstractHashedMap.HashEntry entry,
    Object key1,
    Object key2,
    Object key3,
    Object key4) 
    Is the key equal to the combined key.
 protected boolean isEqualKey(AbstractHashedMap.HashEntry entry,
    Object key1,
    Object key2,
    Object key3,
    Object key4,
    Object key5) 
    Is the key equal to the combined key.
 public Set keySet() 
 public MapIterator mapIterator() 
 public Object put(Object key,
    Object value) 
    Puts the key and value into the map, where the key must be a non-null MultiKey object.
 public Object put(Object key1,
    Object key2,
    Object value) 
    Stores the value against the specified multi-key.
 public Object put(Object key1,
    Object key2,
    Object key3,
    Object value) 
    Stores the value against the specified multi-key.
 public Object put(Object key1,
    Object key2,
    Object key3,
    Object key4,
    Object value) 
    Stores the value against the specified multi-key.
 public Object put(Object key1,
    Object key2,
    Object key3,
    Object key4,
    Object key5,
    Object value) 
    Stores the value against the specified multi-key.
 public  void putAll(Map mapToCopy) 
    Copies all of the keys and values from the specified map to this map. Each key must be non-null and a MultiKey object.
 public Object remove(Object key) 
 public Object remove(Object key1,
    Object key2) 
    Removes the specified multi-key from this map.
 public Object remove(Object key1,
    Object key2,
    Object key3) 
    Removes the specified multi-key from this map.
 public Object remove(Object key1,
    Object key2,
    Object key3,
    Object key4) 
    Removes the specified multi-key from this map.
 public Object remove(Object key1,
    Object key2,
    Object key3,
    Object key4,
    Object key5) 
    Removes the specified multi-key from this map.
 public boolean removeAll(Object key1) 
    Removes all mappings where the first key is that specified.

    This method removes all the mappings where the MultiKey has one or more keys, and the first matches that specified.

 public boolean removeAll(Object key1,
    Object key2) 
    Removes all mappings where the first two keys are those specified.

    This method removes all the mappings where the MultiKey has two or more keys, and the first two match those specified.

 public boolean removeAll(Object key1,
    Object key2,
    Object key3) 
    Removes all mappings where the first three keys are those specified.

    This method removes all the mappings where the MultiKey has three or more keys, and the first three match those specified.

 public boolean removeAll(Object key1,
    Object key2,
    Object key3,
    Object key4) 
    Removes all mappings where the first four keys are those specified.

    This method removes all the mappings where the MultiKey has four or more keys, and the first four match those specified.

 public int size() 
 public String toString() 
 public Collection values()