to top
Android APIs
public abstract class

AbstractMap

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

Class Overview

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.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.util.Map

Protected Constructors

protected AbstractMap ()

Since: API Level 1

Public Methods

public void clear ()

Since: API Level 1

Removes all elements from this Map, leaving it empty.

This implementation calls entrySet().clear().

public boolean containsKey (Object key)

Since: API Level 1

Returns whether this Map contains the specified key.

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

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

public boolean containsValue (Object value)

Since: API Level 1

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 the value to search for.
Returns
  • true if this map contains the specified value, false otherwise.

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

Since: API Level 1

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
  • a set of the mappings

public boolean equals (Object object)

Since: API Level 1

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 the object to compare this instance with.
Returns
  • true if the specified object is equal to this Object; false otherwise.

public V get (Object key)

Since: API Level 1

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 the key.
Returns
  • the value of the mapping with the specified key, or null if no mapping for the specified key is found.

public int hashCode ()

Since: API Level 1

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
  • this object's hash code.

public boolean isEmpty ()

Since: API Level 1

Returns whether this map is empty.

This implementation compares size() to 0.

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

public Set<K> keySet ()

Since: API Level 1

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
  • a set of the keys.

public V put (K key, V value)

Since: API Level 1

Maps the specified key to the specified value.

This base implementation throws UnsupportedOperationException.

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

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

Since: API Level 1

Copies every mapping in the specified Map to this Map.

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

Parameters
map the Map to copy mappings from.

public V remove (Object key)

Since: API Level 1

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 the key of the mapping to remove.
Returns
  • the value of the removed mapping or null if no mapping for the specified key was found.

public int size ()

Since: API Level 1

Returns the number of mappings in this Map.

This implementation returns its entry set's size.

Returns
  • the number of mappings in this Map.

public String toString ()

Since: API Level 1

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
  • a printable representation of this object.

public Collection<V> values ()

Since: API Level 1

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
  • a collection of the values contained in this map.

Protected Methods

protected Object clone ()

Since: API Level 1

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
  • a copy of this object.