Java Reference
In-Depth Information
Map<K,V>
Interface
Package:
java.util
Ancestor interfaces: none
All the exception classes mentioned are unchecked exceptions, which means they are not
required to be caught in a
catch
block or declared in a
throws
clause. No
import
statement is
required because these exception classes are in the package
java.lang
.
CONSTRUCTORS
Although not offi cially required by the interface, any class that implements the
Map<K,V>
interface should have at least two constructors: a no-argument constructor that creates an empty
Map<K,V>
object, and a constructor with one
Map<K,V>
parameter that creates a
Map<K,V>
object with the same elements as the constructor argument. The interface does not specify
whether the copy produced by the one-argument constructor is a shallow copy or a deep copy of
its argument.
METHODS
public boolean
containsKey(Object key)
Returns
true
if the calling object contains
key
as one of its keys.
Throws:
ClassCastException
if the type of
key
is incompatible with the type for this map (optional).
NullPointerException
if the key is
null
and this map does not permit
null
keys (optional).
public boolean
containsValue(Object value)
Returns
true
if the calling object contains one or more keys that map to an instance of
value
.
Throws:
ClassCastException
if the type of
value
is incompatible with the type for this map (optional).
NullPointerException
if the value is
null
and this map does not permit
null
values (optional).
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 refl ected in the set and vice-versa.
public boolean
equals(Object other)
This is the
equals
of the map, not the
equals
of the elements in the map. Overrides the
inherited method
equals
.
public
V get(Object key)
Returns the value onto 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, because it
is possible to map a key to
null
. The
containsKey
method can be used to distinguish the
two cases.
Throws:
ClassCastException
if the type of
key
is incompatible with the type for this map (optional).
NullPointerException
if the
key
is
null
and this map does not permit
null
keys (optional).