public interface Map<K,V>
Map
is a data structure consisting of a set of keys and values
in which each key is mapped to a single value. The class of the objects
used as keys is declared when the Map
is declared, as is the
class of the corresponding values.
A Map
provides helper methods to iterate through all of the
keys contained in it, as well as various methods to access and update
the key/value pairs.
Modifier and Type | Interface and Description |
---|---|
static interface |
Map.Entry<K,V>
Map.Entry is a key/value mapping contained in a Map . |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all elements from this
Map , leaving it empty. |
boolean |
containsKey(Object key)
Returns whether this
Map contains the specified key. |
boolean |
containsValue(Object value)
Returns whether this
Map contains the specified value. |
Set<Map.Entry<K,V>> |
entrySet()
Returns a
Set containing all of the mappings in this Map . |
boolean |
equals(Object object)
Compares the argument to the receiver, and returns
true if the
specified object is a Map and both Map s contain the same mappings. |
V |
get(Object key)
Returns the value of the mapping with the specified key.
|
int |
hashCode()
Returns an integer hash code for the receiver.
|
boolean |
isEmpty()
Returns whether this map is empty.
|
Set<K> |
keySet()
Returns a set of the keys contained in this
Map . |
V |
put(K key,
V value)
Maps the specified key to the specified value.
|
void |
putAll(Map<? extends K,? extends V> map)
Copies every mapping in the specified
Map to this Map . |
V |
remove(Object key)
Removes a mapping with the specified key from this
Map . |
int |
size()
Returns the number of mappings in this
Map . |
Collection<V> |
values()
Returns a
Collection of the values contained in this Map . |
void clear()
Map
, leaving it empty.UnsupportedOperationException
- if removing elements from this Map
is not supported.isEmpty()
,
size()
boolean containsKey(Object key)
Map
contains the specified key.key
- the key to search for.true
if this map contains the specified key,
false
otherwise.boolean containsValue(Object value)
Map
contains the specified value.value
- the value to search for.true
if this map contains the specified value,
false
otherwise.Set<Map.Entry<K,V>> entrySet()
Set
containing all of the mappings in this Map
. Each mapping is
an instance of Map.Entry
. As the Set
is backed by this Map
,
changes in one will be reflected in the other.boolean equals(Object object)
true
if the
specified object is a Map
and both Map
s contain the same mappings.equals
in class Object
object
- the Object
to compare with this Object
.true
if the Object
is the same as this Object
false
if it is different from this Object
.hashCode()
,
entrySet()
V get(Object key)
key
- the key.null
if no mapping for the specified key is found.int hashCode()
Object
s which are equal
return the same value for this method.hashCode
in class Object
equals(Object)
boolean isEmpty()
true
if this map has no elements, false
otherwise.size()
Set<K> keySet()
Map
. The Set
is backed by
this Map
so changes to one are reflected by the other. The Set
does not
support adding.V put(K key, V value)
key
- the key.value
- the value.null
if there was no mapping.UnsupportedOperationException
- if adding to this Map
is not supported.ClassCastException
- if the class of the key or value is inappropriate for
this Map
.IllegalArgumentException
- if the key or value cannot be added to this Map
.NullPointerException
- if the key or value is null
and this Map
does
not support null
keys or values.void putAll(Map<? extends K,? extends V> map)
Map
to this Map
.map
- the Map
to copy mappings from.UnsupportedOperationException
- if adding to this Map
is not supported.ClassCastException
- if the class of a key or a value of the specified Map
is
inappropriate for this Map
.IllegalArgumentException
- if a key or value cannot be added to this Map
.NullPointerException
- if a key or value is null
and this Map
does not
support null
keys or values.V remove(Object key)
Map
.key
- the key of the mapping to remove.null
if no mapping
for the specified key was found.UnsupportedOperationException
- if removing from this Map
is not supported.int size()
Map
.Map
.Collection<V> values()
Collection
of the values contained in this Map
. The Collection
is backed by this Map
so changes to one are reflected by the other. The
Collection
supports Collection.remove(java.lang.Object)
, Collection.removeAll(java.util.Collection<?>)
,
Collection.retainAll(java.util.Collection<?>)
, and Collection.clear()
operations,
and it does not support Collection.add(E)
or Collection.addAll(java.util.Collection<? extends E>)
operations.
This method returns a Collection
which is the subclass of
AbstractCollection
. The AbstractCollection.iterator()
method of this subclass returns a
"wrapper object" over the iterator of this Map
's entrySet()
. The AbstractCollection.size()
method
wraps this Map
's size()
method and the AbstractCollection.contains(java.lang.Object)
method wraps this Map
's
containsValue(java.lang.Object)
method.
The collection is created when this method is called at first time and returned in response to all subsequent calls. This method may return different Collection when multiple calls to this method, since it has no synchronization performed.