|
APCS Java Subset | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
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 |
public java.lang.Object put(java.lang.Object key, java.lang.Object value)
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.
key
- is what is mapped to/associated with
value
value
- is what key
is mapped to
key
or null
if there was no previous mapping.public java.lang.Object get(java.lang.Object key)
key
in this
map, or null
if there is no value associated
wit key
.
key
- is the key whose associated value is returned
key
or null
if there is no such value.public boolean containsKey(java.lang.Object key)
true
if there is a value associated
with key
in this map, otherwise returns
false
.
key
- is the key for which the map is queried as to
the existence of an associated value
true
if there is a (key,value) pair in the map for
the specified key, otherwise return false
public int size()
public Set keySet()
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
public java.lang.Object remove(java.lang.Object key)
null
is
returned.
key
- is the key whose mapping is to be removed
key
or null
if there was no previous mapping.
|
unofficial documentation for the APCS Java Subset | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |