Skip to content

Most visited

Recently visited

navigation
Added in API level 1

AbstractMap

public abstract class AbstractMap
extends Object implements Map<K, V>

java.lang.Object
   ↳ java.util.AbstractMap<K, V>
Known Direct Subclasses
Known Indirect Subclasses


A base class for 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(Object) performs a linear iteration of the entry set. Subclasses should override such methods to improve their performance.

Summary

Nested classes

class AbstractMap.SimpleEntry<K, V>

A key-value mapping with mutable values. 

class AbstractMap.SimpleImmutableEntry<K, V>

An immutable key-value mapping. 

Protected constructors

AbstractMap()

Public methods

void clear()

Removes all elements from this Map, leaving it empty.

This implementation calls entrySet().clear().

boolean containsKey(Object key)

Returns whether this Map contains the specified key.

This implementation iterates its key set, looking for a key that key equals.

boolean containsValue(Object value)

Returns whether this Map contains the specified value.

This implementation iterates its entry set, looking for an entry with a value that value equals.

abstract Set<Entry<K, V>> entrySet()

Returns a Set containing all of the mappings in this Map.

boolean equals(Object object)

Compares this instance with the specified object and indicates if they are equal.

This implementation first checks the structure of object.

V get(Object key)

Returns the value of the mapping with the specified key.

This implementation iterates its entry set, looking for an entry with a key that key equals.

int hashCode()

Returns an integer hash code for this object.

This implementation iterates its entry set, summing the hashcodes of its entries.

boolean isEmpty()

Returns whether this map is empty.

This implementation compares size() to 0.

Set<K> keySet()

Returns a set of the keys contained in this Map.

This implementation returns a view that calls through this to map.

V put(K key, V value)

Maps the specified key to the specified value.

This base implementation throws UnsupportedOperationException.

void putAll(Map<? extends K, ? extends V> map)

Copies every mapping in the specified Map to this Map.

This implementation iterates through map's entry set, calling put() for each.

V remove(Object key)

Removes a mapping with the specified key from this Map.

This implementation iterates its entry set, removing the entry with a key that key equals.

int size()

Returns the number of mappings in this Map.

This implementation returns its entry set's size.

String toString()

Returns a string containing a concise, human-readable description of this object.

This implementation composes a string by iterating its entry set.

Collection<V> values()

Returns a Collection of the values contained in this Map.

This implementation returns a view that calls through this to map.

Protected methods

Object clone()

Creates and returns a copy of this Object.

Inherited methods

From class java.lang.Object
From interface java.util.Map

Protected constructors

AbstractMap

Added in API level 1
AbstractMap ()

Public methods

clear

Added in API level 1
void clear ()

Removes all elements from this Map, leaving it empty.

This implementation calls entrySet().clear().

containsKey

Added in API level 1
boolean containsKey (Object key)

Returns whether this Map contains the specified key.

This implementation iterates its key set, looking for a key that key equals.

Parameters
key Object: the key to search for.
Returns
boolean true if this map contains the specified key, false otherwise.

containsValue

Added in API level 1
boolean containsValue (Object value)

Returns whether this Map contains the specified value.

This implementation iterates its entry set, looking for an entry with a value that value equals.

Parameters
value Object: the value to search for.
Returns
boolean true if this map contains the specified value, false otherwise.

entrySet

Added in API level 1
Set<Entry<K, V>> entrySet ()

Returns a Set containing all of the mappings in this Map. Each mapping is an instance of Map.Entry. As the Set is backed by this Map, changes in one will be reflected in the other.

Returns
Set<Entry<K, V>> a set of the mappings

equals

Added in API level 1
boolean equals (Object object)

Compares this instance with the specified object and indicates if they are equal. In order to be equal, 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 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.

Parameters
object Object: the object to compare this instance with.
Returns
boolean true if the specified object is equal to this Object; false otherwise.

get

Added in API level 1
V get (Object key)

Returns the value of the mapping with the specified key.

This implementation iterates its entry set, looking for an entry with a key that key equals.

Parameters
key Object: the key.
Returns
V the value of the mapping with the specified key, or null if no mapping for the specified key is found.

hashCode

Added in API level 1
int hashCode ()

Returns an integer hash code for this object. By contract, any two objects for which equals(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.

Returns
int this object's hash code.

isEmpty

Added in API level 1
boolean isEmpty ()

Returns whether this map is empty.

This implementation compares size() to 0.

Returns
boolean true if this map has no elements, false otherwise.

keySet

Added in API level 1
Set<K> keySet ()

Returns a set of the keys contained in this Map. The Set is backed by this Map so changes to one are reflected by the other. The Set does not support adding.

This implementation returns a view that calls through this to map. Its iterator transforms this map's entry set iterator to return keys.

Returns
Set<K> a set of the keys.

put

Added in API level 1
V put (K key, 
                V value)

Maps the specified key to the specified value.

This base implementation throws UnsupportedOperationException.

Parameters
key K: the key.
value V: the value.
Returns
V the value of any previous mapping with the specified key or null if there was no mapping.

putAll

Added in API level 1
void putAll (Map<? extends K, ? extends V> map)

Copies every mapping in the specified Map to this Map.

This implementation iterates through map's entry set, calling put() for each.

Parameters
map Map: the Map to copy mappings from.

remove

Added in API level 1
V remove (Object key)

Removes a mapping with the specified key from this Map.

This implementation iterates its entry set, removing the entry with a key that key equals.

Parameters
key Object: the key of the mapping to remove.
Returns
V the value of the removed mapping or null if no mapping for the specified key was found.

size

Added in API level 1
int size ()

Returns the number of mappings in this Map.

This implementation returns its entry set's size.

Returns
int the number of mappings in this Map.

toString

Added in API level 1
String toString ()

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   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.

Returns
String a printable representation of this object.

values

Added in API level 1
Collection<V> values ()

Returns a Collection of the values contained in this Map. The Collection is backed by this Map so changes to one are reflected by the other. The Collection supports remove(Object), removeAll(Collection), retainAll(Collection), and clear() operations, and it does not support add(E) or addAll(Collection) operations.

This method returns a Collection which is the subclass of AbstractCollection. The iterator() method of this subclass returns a "wrapper object" over the iterator of this Map's entrySet(). The size() method wraps this Map's size() method and the contains(Object) method wraps this Map's containsValue(Object) method.

The collection is created when this method is called at first time and returned in response to all subsequent calls. This method may return different Collection when multiple calls to this method, since it has no synchronization performed.

This implementation returns a view that calls through this to map. Its iterator transforms this map's entry set iterator to return values.

Returns
Collection<V> a collection of the values contained in this map.

Protected methods

clone

Added in API level 1
Object clone ()

Creates and returns a copy of this Object. 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.

Returns
Object a copy of this object.
Throws
CloneNotSupportedException
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.