Java Reference
In-Depth Information
21.8.
Map
and
SortedMap
The interface
Map<K,V>
does not extend
Collection
because it has a con-
tract that is different in important ways. The primary difference is that
you do not add an element to a
Map
you add a key/value pair. A
Map
allows
you to look up the value stored under a key. A given key (as defined by
the
equals
method of the key) maps to one value or no values. A value
can be mapped to by as many keys as you like. For example, you might
use a map to store a mapping of a person's name to their address. If
you have an address listed under a name, there will be exactly one in
the map. If you have no mapping, there will be no address value for that
name. Multiple people might share a single address, so the map might
return the same value for two or more names.
The basic methods of the
Map
interface are
public int
size()
Returns the size of this map, that is, the number of key/value
mappings it currently holds. The return value is limited to
In-
teger.MAX_VALUE
even if the map contains more elements.
public boolean
isEmpty()
Returns
TRue
if this collection currently holds no mappings.
public boolean
containsKey(Object key)
Returns
true
if the collection contains a mapping for the given
key
.
public boolean
containsValue(Object value)
Returns
TRue
if the collection contains at least one mapping to
the given
value
.