public abstract class AbstractMap<K,V> extends Object implements Map<K,V>
Map implementations.
Subclasses that permit new mappings to be added must override put(K, V).
The default implementations of many methods are inefficient for large
maps. For example in the default implementation, each call to get(java.lang.Object)
performs a linear iteration of the entry set. Subclasses should override such
methods to improve their performance.
| Modifier and Type | Class and Description |
|---|---|
static class |
AbstractMap.SimpleEntry<K,V>
A key-value mapping with mutable values.
|
static class |
AbstractMap.SimpleImmutableEntry<K,V>
An immutable key-value mapping.
|
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractMap() |
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Removes all of the mappings from this map (optional operation).
|
protected Object |
clone()
Creates and returns a copy of this
Object. |
boolean |
containsKey(Object key)
Returns
true if this map contains a mapping for the specified
key. |
boolean |
containsValue(Object value)
Returns
true if this map maps one or more keys to the
specified value. |
abstract Set<Map.Entry<K,V>> |
entrySet()
Returns a
Set view of the mappings contained in this map. |
boolean |
equals(Object object)
Compares this instance with the specified object and indicates if they
are equal.
|
V |
get(Object key)
Returns the value to which the specified key is mapped,
or
null if this map contains no mapping for the key. |
int |
hashCode()
Returns an integer hash code for this object.
|
boolean |
isEmpty()
Returns
true if this map contains no key-value mappings. |
Set<K> |
keySet()
Returns a
Set view of the keys contained in this map. |
V |
put(K key,
V value)
Associates the specified value with the specified key in this map
(optional operation).
|
void |
putAll(Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map
(optional operation).
|
V |
remove(Object key)
Removes the mapping for a key from this map if it is present
(optional operation).
|
int |
size()
Returns the number of key-value mappings in this map.
|
String |
toString()
Returns a string containing a concise, human-readable description of this
object.
|
Collection<V> |
values()
Returns a
Collection view of the values contained in this map. |
public void clear()
This implementation calls entrySet().clear().
public boolean containsKey(Object key)
true if this map contains a mapping for the specified
key. More formally, returns true if and only if
this map contains a mapping for a key k such that
Objects.equals(key, k). (There can be
at most one such mapping.)
This implementation iterates its key set, looking for a key that
key equals.
containsKey in interface Map<K,V>key - key whose presence in this map is to be testedtrue if this map contains a mapping for the specified
keypublic boolean containsValue(Object value)
true if this map maps one or more keys to the
specified value. More formally, returns true if and only if
this map contains at least one mapping to a value v such that
Objects.equals(value, v). This operation
will probably require time linear in the map size for most
implementations of the Map interface.
This implementation iterates its entry set, looking for an entry with
a value that value equals.
containsValue in interface Map<K,V>value - value whose presence in this map is to be testedtrue if this map maps one or more keys to the
specified valuepublic abstract Set<Map.Entry<K,V>> entrySet()
MapSet view of the mappings contained in this map.
The set is backed by the map, so changes to the map are
reflected in the set, and vice-versa. If the map is modified
while an iteration over the set is in progress (except through
the iterator's own remove operation, or through the
setValue operation on a map entry returned by the
iterator) the results of the iteration are undefined. The set
supports element removal, which removes the corresponding
mapping from the map, via the Iterator.remove,
Set.remove, removeAll, retainAll and
clear operations. It does not support the
add or addAll operations.public boolean equals(Object object)
o must represent the same object
as this instance using a class-specific comparison. The general contract
is that this comparison should be reflexive, symmetric, and transitive.
Also, no object reference other than null is equal to null.
The default implementation returns true only if this ==
o. See Writing a correct
equals method
if you intend implementing your own equals method.
The general contract for the equals and Object.hashCode() methods is that if equals returns true for
any two objects, then hashCode() must return the same value for
these objects. This means that subclasses of Object usually
override either both methods or neither of them.
This implementation first checks the structure of object. If
it is not a map or of a different size, this returns false. Otherwise it
iterates its own entry set, looking up each entry's key in object. If any value does not equal the other map's value for the same
key, this returns false. Otherwise it returns true.
public V get(Object key)
null if this map contains no mapping for the key.
More formally, if this map contains a mapping from a key
k to a value v such that
Objects.equals(key, k),
then this method returns v; otherwise
it returns null. (There can be at most one such mapping.)
If this map permits null values, then a return value of
null does not necessarily indicate that the map
contains no mapping for the key; it's also possible that the map
explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.
This implementation iterates its entry set, looking for an entry with
a key that key equals.
public int hashCode()
Object.equals(java.lang.Object) returns true must return
the same hash code value. This means that subclasses of Object
usually override both methods or neither method.
Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCode method
if you intend implementing your own hashCode method.
This implementation iterates its entry set, summing the hashcodes of its entries.
public boolean isEmpty()
true if this map contains no key-value mappings.
This implementation compares size() to 0.
public Set<K> keySet()
Set view of the keys contained in this map.
The set is backed by the map, so changes to the map are
reflected in the set, and vice-versa. If the map is modified
while an iteration over the set is in progress (except through
the iterator's own remove operation), the results of
the iteration are undefined. The set supports element removal,
which removes the corresponding mapping from the map, via the
Iterator.remove, Set.remove,
removeAll, retainAll, and clear
operations. It does not support the add or addAll
operations.
This implementation returns a view that calls through this to map. Its iterator transforms this map's entry set iterator to return keys.
public V put(K key, V value)
m is said to contain a mapping for a key k if and only
if m.containsKey(k) would return
true.)
This base implementation throws UnsupportedOperationException.
put in interface Map<K,V>key - key with which the specified value is to be associatedvalue - value to be associated with the specified keykey, or
null if there was no mapping for key.
(A null return can also indicate that the map
previously associated null with key,
if the implementation supports null values.)public void putAll(Map<? extends K,? extends V> map)
put(k, v) on this map once
for each mapping from key k to value v in the
specified map. The behavior of this operation is undefined if the
specified map is modified while the operation is in progress.
This implementation iterates through map's entry set, calling
put() for each.
public V remove(Object key)
k to value v such that
Objects.equals(key, k), that mapping
is removed. (The map can contain at most one such mapping.)
Returns the value to which this map previously associated the key,
or null if the map contained no mapping for the key.
If this map permits null values, then a return value of
null does not necessarily indicate that the map
contained no mapping for the key; it's also possible that the map
explicitly mapped the key to null.
The map will not contain a mapping for the specified key once the call returns.
This implementation iterates its entry set, removing the entry with
a key that key equals.
public int size()
Integer.MAX_VALUE elements, returns
Integer.MAX_VALUE.
This implementation returns its entry set's size.
public String toString()
getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toString method
if you intend implementing your own toString method.
This implementation composes a string by iterating its entry set. If this map contains itself as a key or a value, the string "(this Map)" will appear in its place.
public Collection<V> values()
Collection view of the values contained in this map.
The collection is backed by the map, so changes to the map are
reflected in the collection, and vice-versa. If the map is
modified while an iteration over the collection is in progress
(except through the iterator's own remove operation),
the results of the iteration are undefined. The collection
supports element removal, which removes the corresponding
mapping from the map, via the Iterator.remove,
Collection.remove, removeAll,
retainAll and clear operations. It does not
support the add or addAll operations.
This implementation returns a view that calls through this to map. Its iterator transforms this map's entry set iterator to return values.
protected Object clone() throws CloneNotSupportedException
ObjectObject. The default
implementation returns a so-called "shallow" copy: It creates a new
instance of the same class and then copies the field values (including
object references) from this instance to the new instance. A "deep" copy,
in contrast, would also recursively clone nested objects. A subclass that
needs to implement this kind of cloning should call super.clone()
to create the new instance and then create deep copies of the nested,
mutable objects.clone in class ObjectCloneNotSupportedException - if this object's class does not implement the Cloneable interface.