Java Programming/Map

Aside from the  interface, the Java JDK has the   interface as well. It is sometimes also called an Associated Array or a Dictionary. A map defines key value mappings. Implementations of the Map interface do not contain collections of objects. Instead they contain collections of key->value mappings. It can be thought of as an array where the index doesn't need to be an integer.

Use the Map interface if you need to keep related objects together in a Map where you can:
 * Access an element by a key object
 * Map one object to other


 * java.util. Map  : maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The key is usually a non-mutable object. The value object however can be a mutable object.
 * java.util. SortedMap  : same as the Map interface, plus the keys in the Map are sorted.

In the above example, the same operations are made with two different map implementations: We see that only the  has sorted the keys. Beware of the generics. The Map interface is tricky. The methods  and   are not generic. This means that you must be careful of the type of the key:

The  call has done nothing because   is a , not an   so no key and value has been found and removed.

Map Classes
The Map interface has the following implementations:


 * java.util. TreeMap  : guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class, not-synchronized.
 * java.util. Hashtable  : Synchronized, null can not be used as key
 * java.util. HashMap  : is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls
 * java.util.concurrent. ConcurrentHashMap : same as Hashtable, plus retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove).
 * java.util. WeakHashMap  : entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. Non-synchronized.
 * java.util. LinkedHashMap  : This linked list defines the iteration ordering, which is normally the order in which keys were first inserted into the map (first insertion-order). Note that insertion order is not affected if a key is re-inserted into the map.
 * java.util. IdentityHashMap : This class implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values). In other words, in an IdentityHashMap, two keys k1 and k2 are considered equal if and only . (In normal Map implementations (like HashMap) two keys k1 and k2 are considered equal if and only  .) Not-synchronized.
 * java.util. EnumMap : All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. Enum maps are represented internally as arrays. This representation is extremely compact and efficient. Not-synchronized.

Thread safe maps
The following table lists all the synchronized map classes: