- The java.util.HashMap class implements the Map interface using a hash code into an array.
Map<Integer, String> familyMap = new HashMap<Integer, String>();
String name = "John";
familyMap.put( new Integer(3), name );
familyMap.put( new Integer(2), "Nancy" );
familyMap.put( new Integer(1), "George" );
Set<Integer> familyList = new TreeSet<Integer>( familyMap.keySet() );
Iterator<Integer> iter = familyList.iterator();
while ( iter.hasNext() )
{
Integer key = iter.next();
String value = familyMap.get(key);
System.out.println( key + " is mapped to " + value );
}
The output for this code fragment is
1 is mapped to George
2 is mapped to Nancy
3 is mapped to John