APCS Java Subset

ap.java.util
Interface Map

All Known Implementing Classes:
HashMap, TreeMap

public interface Map

A Map is a collection that stores (key,value) pairs, that is it maps a key to a value. A map does not contain duplicate keys, each key in a map maps to one value. The Map interface is only used in the AB course.

As a simple example, consider the code below to count how many times each different word in a file occurs. For the code below, the output is a list of words with the number of times each word occurs, e.g., the output might be as follows.

    20        apple
    5         cherry
    17        lemon
    8         orange
 
(The output will be in alphabetical order by key if a TreeMap is used, but not if a HashMap is used.)

The code assumes the existence of a class Counter since Map classes only store objects, not primitive types like int. Here is the Counter class.

   public static void countAll(BufferedReader r, Map map) throws IOEXception
   {
      String line;
      while ((line = r.readLine()) != null) {               // line read?
         StringTokenizer toks = new StringTokenizer(line);  // parse it
         while (toks.hasMoreTokens()) {                     // into strings
            String s = toks.nextToken().toLowerCase();      // convert
            Counter c = (Counter) map.get(s);               // find counter?
            if (c == null) {  
               map.put(s,new Counter(1));                   // occurs once
            }
            else {
               c.increment();                               // occurs again
            }
         }
      }


      // now print all key/value pairs

      Iterator it = tmap.keySet().iterator();
      while (it.hasNext()) {
         String s = (String) it.next();
         System.out.println(tmap.get(s) + "\t" + s);
      }

   }
 


Method Summary
 boolean containsKey(java.lang.Object key)
          Returns true if there is a value associated with key in this map, otherwise returns false.
 java.lang.Object get(java.lang.Object key)
          Return the value associated with key in this map, or null if there is no value associated wit key.
 Set keySet()
          Returns a Set of the keys in the maps.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Associates value with key in this map so that get(key) returns value.
 java.lang.Object remove(java.lang.Object key)
          Removes the mapping for this key from the map and returns the value previously associated with the key in the map.
 int size()
          Return the number of keys in this map, which is the same as the number of (key,value) pairs.
 

Method Detail

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Associates value with key in this map so that get(key) returns value. If key was mapped to a different value, the previous mapping is replaced.

Parameters:
key - is what is mapped to/associated with value
value - is what key is mapped to
Returns:
the previous value associated with key or null if there was no previous mapping.

get

public java.lang.Object get(java.lang.Object key)
Return the value associated with key in this map, or null if there is no value associated wit key.

Parameters:
key - is the key whose associated value is returned
Returns:
the value associated with key or null if there is no such value.

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if there is a value associated with key in this map, otherwise returns false.

Parameters:
key - is the key for which the map is queried as to the existence of an associated value
Returns:
true if there is a (key,value) pair in the map for the specified key, otherwise return false

size

public int size()
Return the number of keys in this map, which is the same as the number of (key,value) pairs.

Returns:
the number of (key,value) pairs in the map

keySet

public Set keySet()
Returns a Set of the keys in the maps. Removing a key from the associated set, e.g., via an iterator, removes it from the map as well. The terminology for this is that the set is backed by the map

Returns:
a set of the keys in this map

remove

public java.lang.Object remove(java.lang.Object key)
Removes the mapping for this key from the map and returns the value previously associated with the key in the map. If the map contains no value associated with key then null is returned.

Parameters:
key - is the key whose mapping is to be removed
Returns:
the previous value associated with key or null if there was no previous mapping.

unofficial documentation for the APCS Java Subset