The Map Interface

Reading Assignment: read the API specification pages for the Map interface and Map.Entry interface
In the Java tutorial, read the Map interface lesson:
http://java.sun.com/docs/books/tutorial/collections/interfaces/map.html

Feedback Questions for Nov 5:
1. Write Java code that prints the keys in the Map m
2. Assume that a Map favFood contains key-value pairs such that the key is a person's name, and the associated value is the person's favorite food. Write Java code that adds a new pair to the Map representing you and your favorite food.
3. Give one real-world example of a Map. What are the keys and what are the values? Give an example other than the dictionary example we discussed in class.
4. Can a map have two keys with the same value? What about two values with the same key?
5. How comfortable do you feel about maps?

Map
- an object that maps keys to values. Duplicate keys are not allowed (ie each key can map to at most one value).

Recall: The Map interface does NOT inherit from the Collection interface.


Example: A dictionary is a map.

From the online Merriam-Webster dictionary:
cat: a carnivorous mammal long domesticated as a pet and for catching rats and mice


compiler: a computer program that translates an entire set of instructions written in a higher-level symbolic language into machine language before the instructions can be executed

In this excerpt from the Merriam-Webster dictionary, cat and compiler are keys, and the definitions are the values.



public interface Map
{
   Object put (Object key, Object value); // Associates specified value with the specified key in this map.
   Object get(Object key); // returns value to which this map maps the specified key
   Object remove(Object key); // removes the mapping of this key from this map (if present). Returns previous value associated with specified key, or null if
                                                     there was no mapping for the key.
   boolean containsKey(Object key); // returns true if this map contains a mapping for the specified key
   boolean containsValue(Object value); // returns true if this map maps one of more keys to the specified value
   int size(); // returns number of key-value mappings in this map
   boolean isEmpty(); // returns true if the map contains no mappings, false otherwise

   void putAll(Map m); // copies all of the mappings from m to this map.
   void clear();

   // Different views of the Map
   Set keySet(); // returns a set view of the keys contained in this map. Changes to the set are reflected in the map and vice versa.
   Set entrySet(); // returns a set view of the key-value mappings contained in this map. Elements in returned set are of type Map.Entry.
   Collection values(); // returns a collection view of the values contained in this map. Changes to the collection are reflected in the map and vice versa.

   // Interface for entrySet elements
   public interface Entry
   {
      Object getKey();  // returns the key corresponding to this entry
      Object getValue(); // returns the value corresponding to this entry
      Object setValue(Object value); // replaces the value of this entry with the specified value
   }
}

Example:
Map myMap = new HashMap();
myMap.put("a", "x");  // map is {a --> x}
myMap.put("b", "y"); // map is {a-->x, b-->y}
myMap.put("c", "x"); // map is {a-->x, b-->y, c-->x}
myMap.put("a", "z"); // map is {a-->z, b-->y, c-->x}

Object val = myMap.get("a"); // val is "z"
myMap.remove("a"); // map is {b-->y, c-->x}


2 general-purpose Map implementation classes:

Example: Write a program that maps words on the command line to the number of times it occurs. We must use the Integer wrapper class to represent the word frequencies, since values in a map must be of type Object. Set the frequency to an Integer representing 1 if the word hasn't been seen previously, and otherwise associate the word with the incremented integer. We will get the best performance from a HashMap, but the words will be printed in alphabetical order if we use a TreeMap.



Map constructors for both Implementation Classes
Example:
Map dup = new HashMap(m); // dup contains all mappings in Map m.



Three Ways to View a Map

The Collection views methods:
1. keySet() - Set of keys contained in the Map
2. values() - Collection of values contained in the Map
3. entrySet() - Set of key-value pairs (stored in objects of type Map.Entry) contained in the Map

The only way to iterate over a map is by using one of the collections returned by these 3 methods.



Example: Print the key-value pairs in a Map myMap

for (Iterator it = myMap.entrySet().iterator() ; it.hasNext() ;     )
{
   Map.Entry e = (Map.Entry) it.next();  // get the next key-value pair and store it in e
   System.out.println(e.getKey() + " : " + e.getValue());
}




Exercise: Write a program that creates a Map called favoriteColors. Prompt the user from the keyboard to enter the names of ten people and their favorite colors. Create a map containing these values. If there is a person named "Giles" in the map, remove him and his associated favorite color. Print a message indicating whether or not Giles was in the map or not. Then print only the colors in the map.