Collection
-view methods allow a Map
to be viewed as a Collection
in three ways: keySet
: theSet
of keys contained in theMap
.values
: TheCollection
of values contained in theMap
. ThisCollection
is not aSet
, as multiple keys can map to the same value.entrySet
: TheSet
of key-value pairs contained in theMap
. TheMap
interface provides a small nested interface calledMap.Entry
that is the type of the elements in thisSet
.
The Collection
-views provide the only means to iterate over a Map
.
Iterating over the keys in a Map
Here's an example illustrating the standard idiom for iterating over the keys in a Map
:
for (Iterator i=m.keySet().iterator(); i.hasNext(); )
System.out.println(i.next());
Iterating over the values in Map
The idiom for iterating over values is analogous.
for (Iterator i=m.values().iterator(); i.hasNext(); )
System.out.println(i.next());
Iterating over the key value pairs
Here's the idiom for iterating over key-value pairs:
for (Iterator i=m.entrySet().iterator(); i.hasNext(); ) {
Map.Entry e = (Map.Entry) i.next();
System.out.println(e.getKey() +
": " + e.getValue());
}
Map
has to create a new Collection
object each time a Collection
-view operation is called. Rest easy: This is not the case. There's no reason that a Map
can't always return the same object each time it is asked for a given Collection
-view. This is precisely what all of the JDK's Map
implementations do. With all three
Collection
-views, calling an Iterator
's remove
operation removes the associated entry from the backing Map
(assuming the Map
supports element removal to begin with). With the entrySet
view, it is also possible to change the value associated with a key, by calling a Map.Entry
's setValue
method during iteration (again, assuming the Map
supports value modification to begin with). Note that these are the only safe ways to modify a Map
during iteration; the behavior is unspecified if the underlying Map
is modified in any other way while the iteration is in progress. The
Collection
-views support element removal in all its many forms: the remove
, removeAll
, retainAll
, and clear
operations, as well as the Iterator.remove
operation. (Yet again, this assumes that the backing Map
supports element removal.) The
Collection
-views do not support element addition under any circumstances. It would make no sense for the keySet
and values
views, and it's unnecessary for the entrySet
view, as the backing Map
's put
and putAll
provide the same functionality.
No comments:
Post a Comment