|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Map
An object that maps keys onto values. Keys cannot be duplicated. This
interface replaces the obsolete Dictionary
abstract class.
The map has three collection views, which are backed by the map (modifications on one show up on the other): a set of keys, a collection of values, and a set of key-value mappings. Some maps have a guaranteed order, but not all do.
Note: Be careful about using mutable keys. Behavior is unspecified if a key's comparison behavior is changed after the fact. As a corollary to this rule, don't use a Map as one of its own keys or values, as it makes hashCode and equals have undefined behavior.
All maps are recommended to provide a no argument constructor, which builds an empty map, and one that accepts a Map parameter and copies the mappings (usually by putAll), to create an equivalent map. Unfortunately, Java cannot enforce these suggestions.
The map may be unmodifiable, in which case unsupported operations will throw an UnsupportedOperationException. Note that some operations may be safe, such as putAll(m) where m is empty, even if the operation would normally fail with a non-empty argument.
HashMap
,
TreeMap
,
Hashtable
,
SortedMap
,
Collection
,
Set
Nested Class Summary | |
---|---|
static interface |
Map.Entry
A map entry (key-value pair). |
Method Summary | |
---|---|
void |
clear()
Remove all entries from this Map (optional operation). |
boolean |
containsKey(Object key)
Returns true if this contains a mapping for the given key. |
boolean |
containsValue(Object value)
Returns true if this contains at least one mapping with the given value. |
Set |
entrySet()
Returns a set view of the mappings in this Map. |
boolean |
equals(Object o)
Compares the specified object with this map for equality. |
Object |
get(Object key)
Returns the value mapped by the given key. |
int |
hashCode()
Returns the hash code for this map. |
boolean |
isEmpty()
Returns true if the map contains no mappings. |
Set |
keySet()
Returns a set view of the keys in this Map. |
Object |
put(Object key,
Object value)
Associates the given key to the given value (optional operation). |
void |
putAll(Map m)
Copies all entries of the given map to this one (optional operation). |
Object |
remove(Object key)
Removes the mapping for this key if present (optional operation). |
int |
size()
Returns the number of key-value mappings in the map. |
Collection |
values()
Returns a collection (or bag) view of the values in this Map. |
Method Detail |
---|
void clear()
UnsupportedOperationException
- if clear is not supportedboolean containsKey(Object key)
key
- the key to search for
ClassCastException
- if the key is of an inappropriate type
NullPointerException
- if key is null
but the map
does not permit null keysboolean containsValue(Object value)
(value == null ? v == null : value.equals(v))
. This usually
requires linear time.
value
- the value to search for
ClassCastException
- if the type of the value is not a valid type
for this map.
NullPointerException
- if the value is null and the map doesn't
support null values.Set entrySet()
Iterator.remove
, Set.remove
,
removeAll
, retainAll
, and clear
.
Element addition, via add
or addAll
, is
not supported via this set.
Map.Entry
boolean equals(Object o)
true
if the other object is a Map with the same mappings,
that is,o instanceof Map && entrySet().equals(((Map) o).entrySet();
This allows comparison of maps, regardless of implementation.
equals
in class Object
o
- the object to be compared
Set.equals(Object)
Object get(Object key)
null
if
there is no mapping. However, in Maps that accept null values, you
must rely on containsKey
to determine if a mapping exists.
key
- the key to look up
ClassCastException
- if the key is an inappropriate type
NullPointerException
- if this map does not accept null keyscontainsKey(Object)
Object put(Object key, Object value)
null
values, a null return does not
always imply that the mapping was created.
key
- the key to mapvalue
- the value to be mapped
UnsupportedOperationException
- if the operation is not supported
ClassCastException
- if the key or value is of the wrong type
IllegalArgumentException
- if something about this key or value
prevents it from existing in this map
NullPointerException
- if either the key or the value is null,
and the map forbids null keys or valuescontainsKey(Object)
int hashCode()
hashCode
in class Object
Map.Entry.hashCode()
boolean isEmpty()
Set keySet()
Iterator.remove
, Set.remove
,
removeAll
, retainAll
, and clear
.
Element addition, via add
or addAll
, is
not supported via this set.
void putAll(Map m)
m
- the mapping to load into this map
UnsupportedOperationException
- if the operation is not supported
ClassCastException
- if a key or value is of the wrong type
IllegalArgumentException
- if something about a key or value
prevents it from existing in this map
NullPointerException
- if the map forbids null keys or values, or
if m
is null.put(Object, Object)
Object remove(Object key)
key
- the key to remove
UnsupportedOperationException
- if deletion is unsupported
NullPointerException
- if the key is null and this map doesn't
support null keys.
ClassCastException
- if the type of the key is not a valid type
for this map.int size()
Collection values()
Iterator.remove
,
Collection.remove
, removeAll
,
retainAll
, and clear
. Element addition, via
add
or addAll
, is not supported via this
collection.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |