Java Reference
In-Depth Information
Display 16.9
Method Headings in the
Map<K,V>
Interface
(part 2 of 3)
METHODS
boolean
isEmpty( )
Returns
true
if the calling object is empty; otherwise returns
false
.
public
boolean
containsValue(Object value)
Returns
true
if the calling object contains at least one or more keys that map to an instance of
value
.
public
boolean
containsKey(Object key)
Returns
true
if the calling object contains
key
as one of its keys.
public
boolean
equals(Object other)
This is the
equals
of the map, not the
equals
of the elements in the map. Overrides the inher-
ited method
equals
.
public
int
size( )
Returns the number of (key, value) mappings in the calling object.
public
int
hashCode( )
Returns the hash code value for the calling object.
public
Set<Map.Entry<K,V>> entrySet( )
Returns a set
view
consisting of (key, value) mappings for all entries in the map. Changes to the
map are reflected in the set and vice-versa.
public
Collection<V> values( )
Returns a collection
view
consisting of all values in the map. Changes to the map are reflected in
the collection and vice-versa.
public
V get(Object key)
Returns the value to which the calling object maps
key
. If
key
is not in the map, then
null
is
returned. Note that this does not always mean that the key is not in the map since it is possible to
map a key to
null
. The
containsKey
method can be used to distinguish the two cases.
OPTIONAL METHODS
The following methods are optional, which means they still must be implemented, but the imple-
mentation can simply throw an
UnsupportedOperationException
if, for some reason, you do
not want to give the methods a “real” implementation. An
UnsupportedOperationException
is
a
RunTimeException
and so is not required to be caught or declared in a
throws
clause.
public
V put(K key, V value) (Optional)
Associates
key
to
value
in the map. If
key
was associated with an existing value then the old
value is overwritten and returned. Otherwise
null
is returned.