Friday, September 4, 2009

Bidirectional Map

The most common hits we make to a map is to retrieve the value of a mapping based on a key. Now suppose, we have a collection of values and we need to fetch their corresponding keys from the map. Plain old way iterate over the entry set in the map, get their values and then corresponding keys. Apache commons library has pretty cool utility methods for jdk collections API. One of them is a Bidirectional Map, with the interface as BidiMap. This map allows bidirectional lookup between key and values. Calling bidimap.getKey(Object value) gives you the corresponding key for the passed on value. Other useful interfaces like OrderdMap allows you to maintain ordering in the map based data structures. Other interesting feature comes in the form of Bag implementation. Bag is a data structure that stores an object along with its cardinality(number of occurences) .

No comments:

Post a Comment