Map interface

 

■ Note that Map is not derived from Collection

 

■ Maps always store key–value pairs, where both are objects.  The API calls these pairs mappings.  They look like key=value, key=value, key=value when they are printed or extracted.

 

■ One value per key.  No duplicate keys allowed. Duplicate values can occur however. If you must have duplicate keys, use an IdentityHashMap.

 

HashMap provides an unpredictable order of keys. A TreeMap is like a HashMap except it automatically sorts the keys for you, into ascending order.

 

Map provides these basic methods used by HashMap, TreeMap, and IdentityHashMap:   ( TreeMap  adds a few more methods.)

 

 

void  .clear( )  method

 

■ Removes all the keys and values from the Map.  i.e. Assuming HashMap hm already exists:

 

import java.util.*;

 

hm.clear( );     hm becomes empty

 

 

boolean  .containsKey ( Obj )   method

 

■ This method checks for a key object reference matching the one supplied.  It returns true if one is found. If one is not found, it returns false.  i.e. The first snippet prints  true  then  false because one is a key but four is not.

                    

import java.util.*;

 

HashMap hm = new HashMap();

Integer one = new Integer( 1 );

Integer two = new Integer( 2 );

Integer four = new Integer( 4 );

hm.put( one, "one" );

hm.put( two, "two" );

System.out.println( hm.containsKey( one ) );

System.out.println( hm.containsKey( four ) );

 

TreeMap tm = new TreeMap( );

tm.put("1","One");

tm.put("3","Three");

tm.put("5","Five");

tm.put("4","Four");

tm.put("2","Two");

tm.put("0", "");

System.out.println(tm);

if(tm.containsKey("5")) System.out.println("Found key 5");                         // finds key "5"

 

 

 

boolean  .containsValue( Obj )  method

 

■ This method checks for a value object whose contents matching the object supplied.  It returns true if one is found. If one is not found, it returns false.  i.e. The first snippet prints  true  then  false because Integer value 1 is a key but 4 is not.

                    

import java.util.*;

 

HashMap hm = new HashMap();

Integer one = new Integer( 1 );

Integer two = new Integer( 2 );

Integer four = new Integer( 4 );

hm.put( one, "one" );

hm.put( two, "two" );

System.out.println( hm.containsValue( one ) );

 

import java.util.*;

 

TreeMap tm = new TreeMap( );

tm.put("1","One");

tm.put("3","Three");

tm.put("5","Five");

tm.put("4","Four");

tm.put("2","Two");

tm.put("0", "");

System.out.println(tm);

if(tm.containsValue("Five"))

System.out.println("Found value 5");               // finds value "Five"

 

 

■ Here is a snippet proving that the comparison is on content, not pointer reference.  It also applies to .containsKey(...) .

 

import java.util.*;

 

HashMap hm = new HashMap( );

Integer keyOne = new Integer( 1 );

String valueOne = "one";

hm.put( keyOne, valueOne );

String differentOne = new String ("one");

System.out.println( valueOne == differentOne );                             // false - so the pointers are different

System.out.println( valueOne.equals(differentOne) );               // true - but their content is the same

System.out.println( hm.containsValue( differentOne ));                 // true - so it's a content comprison

 

 

Set  .entrySet ( Obj )    method

 

■ Returns a set view of the “key=value” mappings contained in this map.  Each element in the returned set is a Map.Entry 

 

import java.util.*;

 

TreeMap tm = new TreeMap( );

tm.put("1","One");

tm.put("3","Three");

tm.put("5","Five");

tm.put("4","Four");

tm.put("2","Two");

tm.put("0", "");

System.out.println(tm);

Set s = new TreeSet( );

s = tm.entrySet( );                  // loads the set with all the mappings k1=v1, k2=v2, k3=v3

System.out.println(s);         // note the null value is nothing, not the word "null"

 

 

Object  .get( key )    method

 

Finding things in Maps.  Returns the corresponding value, or returns null if either (1) no such key was present, or (2) a value was a present but the value was null.  i.e.

 

import java.util.*;

 

// Uses the tm Map from the example above

System.out.println(tm.get("2"));  // finds the value "Two" for key "2"

System.out.println(tm.get("6"));  // returns "null" for key not found

 

boolean  .isEmpty( )    method

 

■ Returns true if this map is empty and contains no mappings.

 

 

Set  .keySet( )    method

 

■ returns a Set of all the keys (again no duplicates) but is otherwise just like entrySet( )

 

import java.util.*;

 

// Uses the tm Map from the examples above

Set s = new TreeSet( );

s = tm.keySet( );    //returns just the keys

System.out.println(s);

 

Object  .put ( key, value )     method

 

■ This is the way you put objects into a Map. It adds or replaces the value associated with this key. i.e. Some examples, assuming hm already exists:

 

import java.util.*;

 

                                // assume hm exists here...

hm.put( "1", new Integer(1) );

hm.put("one", "1");

 

■ Here are the rules for what gets returned:

 

If the key previously existed with a real value, it returns the old object which was replaced.

If the key did not exist before, it returns null.

If the key previously existed before with a null value, it returns null no matter what you replace it with.

 

■ Remember that the physical order of the keys is unpredictable.  Even if you do your puts in order, your JVM will almost certainly not print the results of this snippet in order like:  1=one, 2=two, 3=three, 4=four.   If you need printouts in order, override toString( ) or simply use a TreeMap instead, as in the example below.

 

import java.util.*;

 

TreeMap tm = new TreeMap( );

tm.put("0", "");

tm.put("1","One");

tm.put("2","Two");

tm.put("3","Three");

tm.put("4","Four");

tm.put("5","Five");

System.out.println(tm);

 

 

void  .putAll( map )    method

 

■ Adds or replaces everything from the supplied Map.

 

import java.util.*;

 

// Uses the tm Map from the examples above

TreeMap tm2 = new TreeMap( );

tm2.put("1","O-N-E");

tm2.put("3","T-H-R-E-E");

tm.putAll(tm2);                      // replaces the values in tm just for keys  "1" amd "3"

System.out.println(tm);

 

 

Obj  .remove( Objkey )    method

 

■ Returns the value to which the map previously associated the key, or null if the map contained no mapping for this key.

 

import java.util.*;

 

// Uses the tm Map from the examples above

System.out.println(tm.remove("4"));                 // returns and removes value for key "4" for key "2"

System.out.println(tm);

 

 

int  .size( )    method

 

■ Returns no. of elements

 

import java.util.*;

 

// Uses the tm Map from the examples above 

System.out.println("Size is: " + tm.size( ));       // returns the int size of tm, its no. of entries

 

 

Map provides several ways to get all the entries out at once:

 

 

Collection  .values( )    method

 

■ This method returns the Map's value elements in a new Collection, in their natural iterator order. For use if duplicates are present or suspected. The resulting array is safe to modify without modifying the underlying Map.

 

import java.util.*;

 

// Uses the tm Map from the examples above

Collection s = new TreeSet( );

s = tm.values( );                     // loads the set with just the Map's  v values

System.out.println(s);          // note the null value is nothing, not the word "null"

 

Here's a snippet to convert just the the values from a Map into a List:

 

Map hm = new HashMap( );
List al = new ArrayList( tm.values( )); 

                        System.out.println(al);