Thursday, August 6, 2009

Using MultiKey for a Map

A MultiKey allows multiple map keys to be merged together.

The purpose of this class is to avoid the need to write code to handle maps of maps. An example might be the need to lookup a filename by key and locale. The typical solution might be nested maps. This class can be used instead by creating an instance passing in the key and locale.

Example usage:

 // populate map with data mapping key+locale to localizedText
Map map = new HashMap();
MultiKey multiKey = new MultiKey(key, locale);
map.put(multiKey, localizedText);

// later retireve the localized text
MultiKey multiKey = new MultiKey(key, locale);
String localizedText = (String) map.get(multiKey);

No comments:

Post a Comment