[all packages] [package java.util] [class hierarchy] [index]

public abstract class java.util.AbstractMap<A, B>

(source file: AbstractMap.java)
java.lang.Object
   |
   +----java.util.AbstractMap<A, B>

The pure class interface.
public abstract class AbstractMap<A, B>
  implements Map<A, B>
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 entries() 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 entries().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 methods 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.

See also:
Map, Collection

Constuctor Index

O AbstractMap()

Methods

O clear()
Removes all mappings from this Map (optional operation).
O containsKey(A)
Returns true if this Map contains a mapping for the specified key.
O containsValue(B)
Returns true if this Map maps one or more keys to this value. More formally, returns true
O entries()
Returns a Set view of the mappings contained in this Map
O equals(Object)
Compares the specified Object with this Map for equality. Returns true if the given
O get(A)
Returns the value to which this Map maps the specified key. Returns null if the Map
O hashCode()
Returns the hash code value for this Map
O isEmpty()
Returns true if this Map contains no key-value mappings.
O keySet()
Returns a Set view of the keys contained in this Map
O put(A, B)
Associates the specified value with the specified key in this Map (optional operation)
O putAll(Map<A, B>)
Copies all of the mappings from the specified Map to this Map (optional operation)
O remove(A)
Removes the mapping for this key from this Map if present (optional operation).
O size()
Returns the number of key-value mappings in this Map.
O toString()
Returns a String representation of this Map.
O values()
Returns a Collection view of the values contained in this Map

Constructors

O AbstractMap
public AbstractMap();

Methods

O size
public int size();
Returns the number of key-value mappings in this Map.

This implementation returns entries().size().

O isEmpty

public boolean isEmpty();
Returns true if this Map contains no key-value mappings.

This implementation returns size() == 0.

O containsValue

public boolean containsValue(B value);
Returns true if this Map maps one or more keys to this value. More formally, returns true if and only if this Map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the Map size for most implementations of Map.

This implementation iterates over entries() searching for an Entry with the specified value. If such an Entry is found, true is returned. If the iteration terminates without finding such an Entry, false is returned. Note that this implementation requires linear time in the size of the Map.

Parameters:
value - value whose presence in this Map is to be tested.

O containsKey

public boolean containsKey(A key);
Returns true if this Map contains a mapping for the specified key.

This implementation iterates over entries() searching for an Entry with the specified key. If such an Entry is found, true is returned. If the iteration terminates without finding such an Entry, false is returned. Note that this implementation requires linear time in the size of the Map; many implementations will override this method.

Parameters:
key - key whose presence in this Map is to be tested.
Throws:
ClassCastException -key is of an inappropriate type for this Map.
NullPointerException -key is null and this Map does not not permit null keys.

O get

public B get(A key);
Returns the value to which this Map maps the specified key. Returns null if the Map contains no mapping for this key. 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 over entries() searching for an Entry with the specified key. If such an Entry is found, the Entry's value is returned. If the iteration terminates without finding such an Entry, null is returned. Note that this implementation requires linear time in the size of the Map; many implementations will override this method.

Parameters:
key - key whose associated value is to be returned.
Throws:
ClassCastException -key is of an inappropriate type for this Map.
NullPointerException -key is null and this Map does not not permit null keys.
See also:
containsKey(A)

O put

public B put(A key,
             B value);
Associates the specified value with the specified key in this Map (optional operation). If the Map previously contained a mapping for this key, the old value is replaced.

This implementation always throws an UnsupportedOperationException.

Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
previous value associated with specified key, or null if there was no mapping for key. (A null return can also indicate that the Map previously associated null with the specified key, if the implementation supports null values.)
Throws:
UnsupportedOperationException -put operation is not supported by this Map.
ClassCastException -class of the specified key or value prevents it from being stored in this Map.
IllegalArgumentException -some aspect of this key or value prevents it from being stored in this Map.
NullPointerException -this Map does not permit null keys or values, and the specified key or value is null.

O remove

public B remove(A key);
Removes the mapping for this key from this Map if present (optional operation).

This implementation iterates over entries() searching for an Entry with the specified key. If such an Entry is found, its value is obtained with its getValue operation, the Entry is is removed from the Collection (and the backing Map) with the Iterator's remove operation, and the saved value is returned. If the iteration terminates without finding such an Entry, null is returned. Note that this implementation requires linear time in the size of the Map; many implementations will override this method.

Parameters:
key - key whose mapping is to be removed from the Map.
Returns:
previous value associated with specified key, or null if there was no entry for key. (A null return can also indicate that the Map previously associated null with the specified key, if the implementation supports null values.)
Throws:
UnsupportedOperationException -remove is not supported by this Map.

O putAll

public void putAll(Map<A, B> t);
Copies all of the mappings from the specified Map to this Map (optional operation). These mappings will replace any mappings that this Map had for any of the keys currently in the specified Map.

This implementation iterates over the specified Map's entries() Collection, and calls this Map's put operation once for each Entry returned by the iteration.

Parameters:
t - Mappings to be stored in this Map.
Throws:
UnsupportedOperationException -putAll is not supported by this Map.
ClassCastException -class of a key or value in the specified Map prevents it from being stored in this Map.
IllegalArgumentException -some aspect of a key or value in the specified Map prevents it from being stored in this Map.
NullPointerException -this Map does not permit null keys or values, and the specified key or value is null.

O clear

public void clear();
Removes all mappings from this Map (optional operation).

This implementation calls entries().clear().

Throws:
UnsupportedOperationException -clear is not supported by this Map.

O keySet

public Set<A> keySet();
Returns a 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 while an iteration over the Set is in progress, the results of the iteration are undefined.) The Set supports element removal, which removes the corresponding entry 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 Set that subclasses AbstractSet. The subclass's iterator method returns a "wrapper object" over this Map's entries() Iterator. The size method delegates to this Map's size method.

The Set is created the first time this method is called, and returned in response to all subsequent calls. No synchronization is performed, so there is a slight chance that multiple calls to this method will not all return the same Set.

O values

public Collection<B> values();
Returns a 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 while an iteration over the Collection is in progress, the results of the iteration are undefined.) The Collection supports element removal, which removes the corresponding entry 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 Collection that subclasses AbstractCollection. The subclass's iterator method returns a "wrapper object" over this Map's entries() Iterator. The size method delegates to this Map's size method.

The Collection is created the first time this method is called, and returned in response to all subsequent calls. No synchronization is performed, so there is a slight chance that multiple calls to this method will not all return the same Collection.

O entries

public abstract Set<Entry<A, B>> entries();
Returns a Set view of the mappings contained in this Map. Each element in this Set is a Map.Entry. 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 while an iteration over the Set is in progress, the results of the iteration are undefined.) The Set supports element removal, which removes the corresponding entry from the Map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

O equals

public boolean equals(Object o);
Compares the specified Object with this Map for equality. Returns true if the given object is also a Map and the two Maps represent the same mappings. More formally, two Maps t1 and t2 represent the same mappings if t1.keySet().equals(t2.keySet()) and for every key k in t1.keySet(), (t1.get(k)==null ? t2.get(k)==null : t1.get(k).equals(t2.get(k))) . This ensures that the equals method works properly across different implementations of the Map interface.

This implementation first checks if the specified Object is this Map; if so it returns true. Then, it checks if the specified Object is a Map whose size is identical to the size of this Set; if not, it it returns false. If so, it iterates over this Map's entries() Collection, and checks that the specified Map contains each mapping that this Map contains. If the specified Map fails to contain such a mapping, false is returned. If the iteration completes, true is returned.

Parameters:
o - Object to be compared for equality with this Map.
Returns:
true if the specified Object is equal to this Map.
Overrides:
equals in class Object

O hashCode

public int hashCode();
Returns the hash code value for this Map. The hash code of a Map is defined to be the sum of the hashCodes of each Entry in the Map's entries() view. This ensures that t1.equals(t2) implies that t1.hashCode()==t2.hashCode() for any two Maps t1 and t2, as required by the general contract of Object.hashCode.

This implementation iterates over entries(), calling hashCode on each element (Entry) in the Collection, and adding up the results.

Overrides:
hashCode in class Object
See also:
hashCode(), hashCode(), equals(Object), equals(Object)

O toString

public String toString();
Returns a String representation of this Map.

Overrides:
toString in class Object


[all packages] [package java.util] [class hierarchy] [index]
java.util.AbstractMap.html