Map Interface

A map is a grouping of keys and their corresponding values. A map cannot contain duplicate keys, and each key can have at most one associated value. Recall that a map is a top-level interface, so it does not inherit any of the Collection methods but is provided with its own methods similar in functionality to Collection methods. We will discuss three of the classes in the java.util package that implement the Map interface: HashMap, TreeMap, and Hashtable. HashMap is fastest but does not guarantee ordering; if ordering is required, TreeMap should be used. Prior to Java version 1.2, Hashtable was a separate data structure, since version 1.2, Hashtable is an implementation of the Map interface. The Hashtable class is similar to a HashMap, but a Hashtable is synchronized.
MapExample adds a number of elements to a HashMap, then prints the values of the map.

MapExample

import java.util.*;


public class MapExample
{

public static void main(String[] args)
{
Map m = new HashMap();
m.put(new Integer(1),”Smith”);
m.put(new Integer(2),”Jones”);
m.put(new Integer(3),”Smith”);
m.put(new Integer(4),”Brown”);
m.put(new Integer(5),”Able”);


Collection c = m.keySet();
Iterator i = c.iterator();
while (i.hasNext() )
{
System.out.print( m.get(i.next()) + " ");
}
}
}



The output of the program is

> java MapExample
Able Brown Smith Jones Smith


Note (line 6) that we have defined our HashMap as a Map type, m. We could have defined the HashMap as a HashMap type, as follows:

HashMap m = new HashMap();

However, as with a Collection, a Map type makes the code more generic: the code that follows the preceding statement will work whatever the implementation. To use a Hashtable implementation, we simply change the statement to

Map m = new Hashtable();

for the program to work.

In lines 7–11, we use the put(key, value) method to add pairs of keys with associated values to our map. Because the put arguments must be Object types, we use the Integer wrapper to convert integer numbers to objects.

There is no iterator object that iterates directly over a map. We need to use a collection or set view method that converts a map to a collection or set. We then iterate over this collection or set as before. The statement (line 13)

Collection c = m.keySet();

uses the collection view method keySet to return a collection, c, of keys contained in the map, m.

In lines 15–17, we then iterate over the collection c. In line 16, we use the Iterator.next method to obtain the next key in the collection. We look up the key's value using the Map.get method and print this value. Note that although in this case the map has been ordered by descending key order, the HashMap class does not guarantee ordering. If order has to be guaranteed, we must use the SortedMap interface.

Apart from keySet, the other set view methods are values, which returns a collection of values contained in the map, and entrySet, which returns a collection of key value mappings contained in the map. In the preceding MapExample, the entrySet view would be

5=Able 4=Brown 3=Smith 2=Jones 1=Smith

No comments:

Post a Comment