Java Reference
In-Depth Information
distinguish between a failed search and a successful search that returns
null
for the value.
containsKey
can be used if
null
values are known to be in the map.
The
Map
interface does not provide an
iterator
method or class. Instead it
returns a
Collection
that can be used to view the contents of the map.
The
keySet
method gives a
Collection
that contains all the keys. Since
duplicate keys are not allowed, the result of
keySet
is a
Set
, for which we can
obtain an iterator. If the
Map
is a
SortedMap
, the
Set
is a
SortedSet
.
Similarly, the
values
method returns a
Collection
that contains all the val-
ues. This really is a
Collection
, since duplicate values are allowed.
Finally, the
entrySet
method returns a collection of key/value pairs.
Again, this is a
Set
, because the pairs must have different keys. The objects in
the
Set
returned by the
entrySet
are pairs; there must be a type that represents
key/value pairs. This is specified by the
Entry
interface that is nested in the
Map
interface. Thus the type of object that is in the
entrySet
is
Map.Entry
.
Figure 6.38 illustrates the use of the
Map
with a
TreeMap
. An empty map is
created at line 23 and then populated with a series of
put
calls at lines 25 to
29. The last call to
put
simply replaces a value with
“unlisted”
. Lines 31 and
32 print the result of a call to
get
, which is used to obtain the value for the key
"Jane Doe"
. More interesting is the
printMap
routine that spans lines 8 to 19.
In
printMap
, at line 12, we obtain a
Set
containing
Map.Entry
pairs. From
the
Set
, we can use an enhanced
for
loop to view the
Map.Entry
s, and we can
obtain the key and value information using
getKey
and
getValue
, as shown on
lines 16 and 17.
Map.Entry
abstracts the
notion of a pair in
the map.
Returning to
main
, we see that
keySet
returns a set of keys (at line 37) that
can be printed at line 38 by calling
printCollection
(in Figure 6.11); similarly
at lines 41 and 42,
values
returns a collection of values that can be printed.
More interesting, the key set and value collection are
views
of the map, so
changes to the map are immediately reflected in the key set and value collec-
tion, and removals from the key set or value set become removals from the
underlying map. Thus line 44 removes not only the key from the key set but
also the associated entry from the map. Similarly, line 45 removes an entry
from the map. Thus the printing at line 49 reflects a map with two entries
removed.
Views themselves are an interesting concept and we will discuss specifics
of how they are implemented later when we implement the map classes. Some
further examples of views are discussed in Section 6.10.
Figure 6.39 illustrates another use of the map, in a method that returns
items in a list that appear more than once. In this code, a map is being used
internally to group the duplicates together: the key of the map is an item, and
the value is the number of times the item has occurred. Lines 8-12 illustrate
the typical idea seen in building up a map this way. If the item has never been
keySet
,
values
, and
entrySet
return
views.
Search WWH ::
Custom Search