Commons-Collections and the Java Collections Framework provide a wide variety of collections to choose from. This choice can be bewildering, so this document seeks to help you choose.
Do you want to be able to access the key from the value as well as the value from the key? Then use a <ahref="#"onclick="showBidiMap()">BidiMap</a>.
For example you could convert country codes to text <i>and</i> text to country codes, allowing you to convert 'FR' to 'France' and 'France' to 'FR'.<br/>
Or do you just need a single direction lookup from key to value? Then use a <ahref="#"onclick="showMap()">Map</a>.
For example you could convert a country code to text <i>but not</i> text to country code.<br/>
The <ahref="http://jakarta.apache.org/commons/collections/apidocs-COLLECTIONS_3_1/org/apache/commons/collections/BidiMap.html">BidiMap</a> interface and implementations in Commons Collections allow you to lookup data in both directions. There are three implementations:
<td>Uses two <ahref="http://java.sun.com/j2se/1.4/docs/api/java/util/HashMap.html">HashMap</a> objects internally. This implementation is a useful general purpose BidiMap that can store any object. The objects are stored in any order, and the order may change over time.</td>
<td>Uses two <ahref="http://java.sun.com/j2se/1.4/docs/api/java/util/TreeMap.html">TreeMap</a> objects internally which means that keys and values must implement <ahref="http://java.sun.com/j2se/1.4/docs/api/java/lang/Comparable.html">Comparable</a>, unless a comparator is supplied. If considering this class, the TreeBidiMap is usually a better choice. However, this is the only implementation of the <ahref="http://jakarta.apache.org/commons/collections/apidocs-COLLECTIONS_3_1/org/apache/commons/collections/SortedBidiMap.html">SortedBidiMap</a> interface.</td>
<td>Dedicated implementation that requires keys and values to implement <ahref="http://java.sun.com/j2se/1.4/docs/api/java/lang/Comparable.html">Comparable</a>, unless a comparator is supplied. This is an efficient implementation using a data structure that avoids duplicating data. The keys and values are held and viewed in sorted order, and the class implements the <ahref="http://jakarta.apache.org/commons/collections/apidocs-COLLECTIONS_3_1/org/apache/commons/collections/OrderedBidiMap.html">OrderedBidiMap</a> interface.</td>
The <ahref="http://java.sun.com/j2se/1.4/docs/api/java/util/Map.html">Map</a> interface and implementations in both the JDK and Commons Collections allow you to lookup data from a key to a value. This is one of the most powerful interfaces in the JDK, however it is very difficult to implement. These are the available implementations:
<td>This map is the most commonly used and fastest implementation. It is suitable for most situations and can store any object. Objects are stored in any order, and the order may change over time.</td>
<td>The Commons Collections hash map implementation which is very similar in design to HashMap, but also supports easy iteration via a <ahref="http://jakarta.apache.org/commons/collections/apidocs-COLLECTIONS_3_1/org/apache/commons/collections/MapIterator.html">MapIterator</a>. This implementation is also designed to be subclassed. Use this map if you want to use the extra iterator, or to subclass. Otherwise, there is no advantage (or disadvantage) compared to the JDK HashMap.</td>
<td>This map ensures that the keys are always sorted, and iteration order is consistent. All keys must implement <ahref="http://java.sun.com/j2se/1.4/docs/api/java/lang/Comparable.html">Comparable</a>, unless a comparator is supplied. This map should be used if you need the map to be sorted.</td>
<td>This map maintains the order that each object is added to the map. When the map is viewed via an iterator, the insertion order will be seen. This map should be used if you need to retain the insertion order.</td>
<td>This map, available from JDK 1.4, maintains the order that each object is added to the map. When the map is viewed via an iterator, the insertion order will be seen. Commons collections' LinkedMap provides the same functionality for JDK1.2 onwards and provides a MapIterator.</td>
<td>This map decorates another map to maintains the order that each object is added to the map. The order is maintained using a list. This map should be used if you need to retain the insertion order but you also need the special features of another map.</td>
<td>This map places a maximum size limit on the map and removes the least recently used (LRU) key-value when it is full. This map should be used for caches where you want to limit the maximum memory size used, and are happy with the least recently used algorithm for removals.</td>
<td>This map enables keys and values to be garbage collected whilst still held in the map. This can be useful for building memory sensitive caches. This map should be used for caches where you want to allow garbage collection from the map.</td>
<td>This map stores each key-value pair with a key that can be garbage collected. This can be useful for building memory sensitive caches. ReferenceMap provides the same functionality but with much more flexibility.</td>
<td>This map is restricted to storing one key-value pair. It may contain no more than and no less than one pair. It provides a MapIterator. This map should be used if you want to return one key-value pair and must use the Map interface.</td>
<td>This map is optimised to store one, two or three key-value pairs and outperforms HashMap at these sizes. For size four and above performance is about 5% slower than HashMap. This map also has good garbage collection characteristics when below size four. It provides a MapIterator. This map should be used if are 99% sure that the map will be size three or less.</td>
<td>This map is optimised to work well in a heavily loaded multi-threaded environment. It provides synchronization internally, locking on a per 'bucket' basis, where the buckets are fixed at construction time. You should use this class if you can predict the size of the map and you are working in a very intensive multi-threaded environment.</td>