The Map Interface

public interface Map<K, V>
    V put(K key, V value); // associates value with key in this map. Returns value
       // prev. associated with key, or null if no value associated with key previously.
    V get(Object key); // returns value to which this map maps key. Returns null if
          // no mapping for key in this map.

    V remove(Object key); //removes mapping of this key from the map if present.
          // Returns value that was associated with key, or null if there was no mapping
          // for key

    boolean containsKey(Object key); // returns true if this map contains a mapping
          // for key.

    boolean containsValue(Object val); // returns true if some mapping in this map
          // contains value val

    int size(); // returns the number of key-value pairs in this map.

    boolean isEmpty(); //returns true if this map contains no key-value pairs.

    void putAll(Map<? extends K, ? extends V> m); // copies all mappings in specified map
          // m to this map.

    void clear(); // removes all mappings from this map.

    // Ways to view map as different kinds of collections
    public Set<K> keySet(); // returns a set view of the keys in this map.
          // Changes to map or set reflected in the other.

    public Collection<V> values(); // returns a collection view of the values in this map

    public Set<Map.Entry<K, V>> entrySet(); //returns a set view of the mappings in
          // this map. Each element in the set is a Map.Entry.

    // nested interface for objects returned by entrySet()
    public interface Map.Entry<K, V>
       K getKey(); // returns the key for this entry
       V getValue(); // returns the value for this entry
       V setValue(V val); // sets the value in this entry to val, and returns the old
             // value
       boolean equals(Object o); // returns true if o is a map entry with the same key
             // and value as this map entry

Two Map implementations

HashMap - best performing implementation
TreeMap - guarantees order of iteration

Map<String, String> defs = new HashMap<String, String>();
defs.put("cheese", "a food made from milk curd");
defs.put("thanksgiving", "the act of giving thanks");
defs.put("cs 313e", "best Java class ever");
System.out.println("this map contains " + defs.size() + " definitions");
defs.put("cs 313e", "best class ever");

Example: Write a program that maps each String on the command line to the number of times it appears on the command line.

Map<String, String> myMap = new HashMap<String, String>();
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}

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

Map Constructors

Two constructors:

Map<Integer, Double> copy = new TreeMap<Integer,Double>(m);

Collection Views of a Map

We can view a map as a collection in 3 ways, using 3 different methods:

These views of the map provide us the only ways to iterate over a map.

Example: Iterate over the keys in a map m, and print each key. Assume the keys in m are Strings.
for(Iterator<String> i = m.keySet().iterator(); i.hasNext();   )

Example: Iterate over the key-value pairs, assuming that the values are also Strings.
for(Iterator<Map.Entry<<String, String>> i = m.entrySet().iterator(); i.hasNext();   )
    Map.Entry<String, String> e =;
    System.out.println(e.getKey() + ":" + e.getValue());