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 {@link 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.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; }
@author Matt Hall, John Watkinson, Stephen Colebourne
@version $Revision: 1.1 $ $Date: 2005/10/11 17:05:32 $
@since Commons Collections 3.1