AbstractMap

public abstract class AbstractMap extends Object
implements Map<K, V>
Known Direct Subclasses
Known Indirect Subclasses

This class provides a skeletal implementation of the Map interface, to minimize the effort required to implement this interface.

To implement an unmodifiable map, the programmer needs only to extend this class and provide an implementation for the entrySet method, which returns a set-view of the map's mappings. Typically, the returned set will, in turn, be implemented atop AbstractSet. This set should not support the add or remove methods, and its iterator should not support the remove method.

To implement a modifiable map, the programmer must additionally override this class's put method (which otherwise throws an UnsupportedOperationException), and the iterator returned by entrySet().iterator() must additionally implement its remove method.

The programmer should generally provide a void (no argument) and map constructor, as per the recommendation in the Map interface specification.

The documentation for each non-abstract method in this class describes its implementation in detail. Each of these methods may be overridden if the map being implemented admits a more efficient implementation.

This class is a member of the Java Collections Framework.

See Also

Nested Class Summary

class AbstractMap.SimpleEntry<K, V> An Entry maintaining a key and a value. 
class AbstractMap.SimpleImmutableEntry<K, V> An Entry maintaining an immutable key and value. 

Protected Constructor Summary

AbstractMap()
Sole constructor.

Public Method Summary

void
clear()
Removes all of the mappings from this map (optional operation).

This implementation calls entrySet().clear().

boolean
containsKey(Object key)
Returns true if this map contains a mapping for the specified key.

This implementation iterates over entrySet() searching for an entry with the specified key.

boolean
containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.

This implementation iterates over entrySet() searching for an entry with the specified value.

abstract Set<Entry<K, V>>
entrySet()
Returns a Set view of the mappings contained in this map.
boolean
equals(Object o)
Compares the specified object with this map for equality.
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.

This implementation iterates over entrySet() searching for an entry with the specified key.

int
hashCode()
Returns the hash code value for this map.
boolean
isEmpty()
Returns true if this map contains no key-value mappings.

This implementation returns size() == 0.

Set<K>
keySet()
Returns a Set view of the keys contained in this map.

This implementation returns a set that subclasses AbstractSet.

V
put(K key, V value)
Associates the specified value with the specified key in this map (optional operation).

This implementation always throws an UnsupportedOperationException.

void
putAll(Map<? extends K, ? extends V> m)
Copies all of the mappings from the specified map to this map (optional operation).

This implementation iterates over the specified map's entrySet() collection, and calls this map's put operation once for each entry returned by the iteration.

V
remove(Object key)
Removes the mapping for a key from this map if it is present (optional operation).

This implementation iterates over entrySet() searching for an entry with the specified key.

int
size()
Returns the number of key-value mappings in this map.

This implementation returns entrySet().size().

String
toString()
Returns a string representation of this map.
Collection<V>
values()
Returns a Collection view of the values contained in this map.

This implementation returns a collection that subclasses AbstractCollection.

Protected Method Summary

Object
clone()
Returns a shallow copy of this AbstractMap instance: the keys and values themselves are not cloned.

Inherited Method Summary