In the past two months we discussed the basics of the Collection interface and reviewed the children of this interface, the List and Set interfaces. This month we will look at the Map interface. What exactly is a Map in Java? A Map is simply a class that stores key-value pairs and provides a way to locate a value based on the key. An array is simply a list of objects that allows you to find an object by specifying a number. A Map allows you to specify an Object to find a different Object. Imagine a Map containing Employee objects for eaach employee in your company. A Map would allow you to locate a particular Employee object in the Map by specifying a key such as their Social Security Number. A Map can not contain a duplicate key.
The Map interface is a replacement for the Dictionary class which is now considered obsolete. There are seven non-abstract implementations of the Map interface that we will discuss. The Hashtable and Properties classes have been around since JDK 1.0. TreeMap, HashMap, and WeakHashMap were added in JDK 1.2. Two new classes were added in J2SE 1.4. These are the LinkedHashMap and the IdentityHashMap. Each implementation contains features that make it appropriate for particular applications. Each of the classes has a unique feature that makes its use desirable in certain circumstances. The HashMap is the basic Map implementation. Hashtable is synchronized. TreeMap maintains keys in a sorted order. LinkedhashMap maintains the objects in the order they were added to the Map. The IdentityHashMap is a special Map that maintains key by reference equality (they point to the same object) rather than object equality (they contain the same value). WeakHashMap is a Map with weak keys, that is, objects in a WeakHashMap will be garbage collected if the only reference to them is the WeakHashMap itself. Finally, the Properties class is a special class used to access and maintain properties files that contain name value pairs.
Figure 1: relationships between the classes and interfaces of the Map interface
The map interface contains one static nested interface, Map.Entry
which we will discuss in a moment. The Map
contains the basic methods for all Map implementations such as put and
get. In addition, the Map provides three
separate ways to get objects from it as Collection objects. You can get
a Set of keys, a Collection of values,
or a Set of key-value pairs stored in a Map.Entry object.
Here are some of the key methods of the Map interface:
The Map interface contains a static nested interface called Entry. This interface is used to access key-value pairs as individual objects outside of the Map. The entrySet( ) method can be used to get individual key-value pairs out of the Map. You can then use methods of the Map.Entry interface to get the key or valu out of the Object.
Here are some of the key methods of the Map.Entry interface:
The HashMap class is the simplest implementation of the Map
interface. The HashMap does not add any additional
methods (other than clone) beyond those found in the Map
interface. The HashMap achieves good performance
by using a hash to store the key in the Map. The hash allows fast
lookup which means that the containKey( ) method
will perform much better than the containsValue( ) method. Any Object
used as a key in a HashMap must implement
the hashCode( ) and equals( ) methods. See Part 2 of this series for a
discussion of this issue.
The HashMap does not guarantee the order of the items in the Map and
allows one null key. Duplicates are not permitted.
The HashSet offers "constant time" performance for lookups involving
the key and linear time for lookups
based on value. This means that adding items to the Map will not cause
significant performance degradation as long
as lookups are done by the key. The performance of basic functions such
as put, remove, get, etc is based on two
factors which can be specified in the constructor of the HashMap,
initial capacity and load factor. See the HashSet
discussion in Part 2 of this series for the further discussion of these
parameters.
This page is an excerpt from an article at Java Ranch
http://www.javaranch.com/journal/2002/08/mapinterface.html