Contents | Package | Class | Tree | Deprecated | Index | Help Java 1.2 Beta 3
PREV | NEXT SHOW LISTS | HIDE LISTS

Class java.util.AbstractMap

java.lang.Object
    |
    +----java.util.AbstractMap
Subclasses:
HashMap, TreeMap

public abstract class AbstractMap
extends Object
implements Map
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.

Since:
JDK1.2
See Also:
Map, Collection

Constructor Summary
 AbstractMap()
 
 

Method Summary
void  clear()
Removes all mappings from this Map (optional operation).
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 this value.
Set  entries()
Returns a Set view of the mappings contained in this Map.
boolean  equals(Object o)
Compares the specified Object with this Map for equality.
Object  get(Object key)
Returns the value to which this Map maps 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.
Set  keySet()
Returns a Set view of the keys contained in this Map.
Object  put(Object key, Object value)
Associates the specified value with the specified key in this Map (optional operation).
void  putAll(Map t)
Copies all of the mappings from the specified Map to this Map (optional operation).
Object  remove(Object key)
Removes the mapping for this key from this Map if present (optional operation).
int  size()
Returns the number of key-value mappings in this Map.
String  toString()
Returns a String representation of this Map.
Collection  values()
Returns a Collection view of the values contained in this Map.
 
Methods inherited from class java.lang.Object
 clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractMap

public AbstractMap()
Method Detail

size

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

This implementation returns entries().size().

Implements:
size in interface Map

isEmpty

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

This implementation returns size() == 0.

Implements:
isEmpty in interface Map

containsValue

public boolean containsValue(Object 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.

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

containsKey

public boolean containsKey(Object 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.

Implements:
containsKey in interface Map
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.

get

public Object get(Object 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.

Implements:
get in interface Map
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(Object)

put

public Object put(Object key,
                  Object 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.

Implements:
put in interface Map
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.

remove

public Object remove(Object 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.

Implements:
remove in interface Map
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.

putAll

public void putAll(Map 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.

Implements:
putAll in interface Map
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.

clear

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

This implementation calls entries().clear().

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

keySet

public Set 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.

Implements:
keySet in interface Map

values

public Collection 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.

Implements:
values in interface Map

entries

public abstract Set 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.
Implements:
entries in interface Map

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.

Implements:
equals in interface Map
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

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.

Implements:
hashCode in interface Map
Overrides:
hashCode in class Object
See Also:
Entry#hashCode(), hashCode(), equals(Object), equals(Object)

toString

public String toString()
Returns a String representation of this Map.
Overrides:
toString in class Object

Contents | Package | Class | Tree | Deprecated | Index | Help Java 1.2 Beta 3
PREV | NEXT SHOW LISTS | HIDE LISTS

Submit a bug or feature
Submit comments/suggestions about new javadoc look.
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. All Rights Reserved.