Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Map

public interface Map

java.util.Map<K, V>
Known Indirect Subclasses


A Map is a data structure consisting of a set of keys and values in which each key is mapped to a single value. The class of the objects used as keys is declared when the Map is declared, as is the class of the corresponding values.

A Map provides helper methods to iterate through all of the keys contained in it, as well as various methods to access and update the key/value pairs.

Summary

Nested classes

interface Map.Entry<K, V>

Map.Entry is a key/value mapping contained in a Map

Public methods

abstract void clear()

Removes all elements from this Map, leaving it empty.

abstract boolean containsKey(Object key)

Returns whether this Map contains the specified key.

abstract boolean containsValue(Object value)

Returns whether this Map contains the specified value.

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

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

abstract boolean equals(Object object)

Compares the argument to the receiver, and returns true if the specified object is a Map and both Maps contain the same mappings.

abstract V get(Object key)

Returns the value of the mapping with the specified key.

abstract int hashCode()

Returns an integer hash code for the receiver.

abstract boolean isEmpty()

Returns whether this map is empty.

abstract Set<K> keySet()

Returns a set of the keys contained in this Map.

abstract V put(K key, V value)

Maps the specified key to the specified value.

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

Copies every mapping in the specified Map to this Map.

abstract V remove(Object key)

Removes a mapping with the specified key from this Map.

abstract int size()

Returns the number of mappings in this Map.

abstract Collection<V> values()

Returns a Collection of the values contained in this Map.

Public methods

clear

Added in API level 1
void clear ()

Removes all elements from this Map, leaving it empty.

Throws
UnsupportedOperationException if removing elements from this Map is not supported.

See also:

containsKey

Added in API level 1
boolean containsKey (Object key)

Returns whether this Map contains the specified key.

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.

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 the argument to the receiver, and returns true if the specified object is a Map and both Maps contain the same mappings.

Parameters
object Object: the Object to compare with this Object.
Returns
boolean boolean true if the Object is the same as this Object false if it is different from this Object.

See also:

get

Added in API level 1
V get (Object key)

Returns the value of the mapping with the specified key.

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 the receiver. Objects which are equal return the same value for this method.

Returns
int the receiver's hash.

See also:

isEmpty

Added in API level 1
boolean isEmpty ()

Returns whether this map is empty.

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

See also:

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.

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.

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.
Throws
UnsupportedOperationException if adding to this Map is not supported.
ClassCastException if the class of the key or value is inappropriate for this Map.
IllegalArgumentException if the key or value cannot be added to this Map.
NullPointerException if the key or value is null and this Map does not support null keys or values.

putAll

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

Copies every mapping in the specified Map to this Map.

Parameters
map Map: the Map to copy mappings from.
Throws
UnsupportedOperationException if adding to this Map is not supported.
ClassCastException if the class of a key or a value of the specified Map is inappropriate for this Map.
IllegalArgumentException if a key or value cannot be added to this Map.
NullPointerException if a key or value is null and this Map does not support null keys or values.

remove

Added in API level 1
V remove (Object key)

Removes a mapping with the specified key from this Map.

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.
Throws
UnsupportedOperationException if removing from this Map is not supported.

size

Added in API level 1
int size ()

Returns the number of mappings in this Map.

Returns
int the number of mappings in this Map.

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.

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