Android APIs
public final class

ArrayMap

extends Object
implements Map<K, V>
java.lang.Object
   ↳ android.util.ArrayMap<K, V>

Class Overview

ArrayMap is a generic key->value mapping data structure that is designed to be more memory efficient than a traditional HashMap. It keeps its mappings in an array data structure -- an integer array of hash codes for each item, and an Object array of the key/value pairs. This allows it to avoid having to create an extra object for every entry put in to the map, and it also tries to control the growth of the size of these arrays more aggressively (since growing them only requires copying the entries in the array, not rebuilding a hash map).

Note that this implementation is not intended to be appropriate for data structures that may contain large numbers of items. It is generally slower than a traditional HashMap, since lookups require a binary search and adds and removes require inserting and deleting entries in the array. For containers holding up to hundreds of items, the performance difference is not significant, less than 50%.

Because this container is intended to better balance memory use, unlike most other standard Java containers it will shrink its array as items are removed from it. Currently you have no control over this shrinking -- if you set a capacity and then remove an item, it may reduce the capacity to better match the current size. In the future an explicit call to set the capacity should turn off this aggressive shrinking behavior.

Summary

Public Constructors
ArrayMap()
Create a new empty ArrayMap.
ArrayMap(int capacity)
Create a new ArrayMap with a given initial capacity.
ArrayMap(ArrayMap map)
Create a new ArrayMap with the mappings from the given ArrayMap.
Public Methods
void clear()
Make the array map empty.
boolean containsAll(Collection<?> collection)
Determine if the array map contains all of the keys in the given collection.
boolean containsKey(Object key)
Check whether a key exists in the array.
boolean containsValue(Object value)
Check whether a value exists in the array.
void ensureCapacity(int minimumCapacity)
Ensure the array map can hold at least minimumCapacity items.
Set<Entry<K, V>> entrySet()
Return a Set for iterating over and interacting with all mappings in the array map.
boolean equals(Object object)
Compares this instance with the specified object and indicates if they are equal.

This implementation returns false if the object is not a map, or if the maps have different sizes.

V get(Object key)
Retrieve a value from the array.
int hashCode()
Returns an integer hash code for this object.
int indexOfKey(Object key)
Returns the index of a key in the set.
boolean isEmpty()
Return true if the array map contains no items.
K keyAt(int index)
Return the key at the given index in the array.
Set<K> keySet()
Return a Set for iterating over and interacting with all keys in the array map.
V put(K key, V value)
Add a new value to the array map.
void putAll(ArrayMap<? extends K, ? extends V> array)
Perform a put(Object, Object) of all key/value pairs in array
void putAll(Map<? extends K, ? extends V> map)
Perform a put(Object, Object) of all key/value pairs in map
V remove(Object key)
Remove an existing key from the array map.
boolean removeAll(Collection<?> collection)
Remove all keys in the array map that exist in the given collection.
V removeAt(int index)
Remove the key/value mapping at the given index.
boolean retainAll(Collection<?> collection)
Remove all keys in the array map that do not exist in the given collection.
V setValueAt(int index, V value)
Set the value at a given index in the array.
int size()
Return the number of items in this array map.
String toString()
Returns a string containing a concise, human-readable description of this object.

This implementation composes a string by iterating over its mappings.

V valueAt(int index)
Return the value at the given index in the array.
Collection<V> values()
Return a Collection for iterating over and interacting with all values in the array map.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.util.Map

Public Constructors

public ArrayMap ()

Added in API level 19

Create a new empty ArrayMap. The default capacity of an array map is 0, and will grow once items are added to it.

public ArrayMap (int capacity)

Added in API level 19

Create a new ArrayMap with a given initial capacity.

public ArrayMap (ArrayMap map)

Added in API level 19

Create a new ArrayMap with the mappings from the given ArrayMap.

Public Methods

public void clear ()

Added in API level 19

Make the array map empty. All storage is released.

public boolean containsAll (Collection<?> collection)

Added in API level 19

Determine if the array map contains all of the keys in the given collection.

Parameters
collection The collection whose contents are to be checked against.
Returns
  • Returns true if this array map contains a key for every entry in collection, else returns false.

public boolean containsKey (Object key)

Added in API level 19

Check whether a key exists in the array.

Parameters
key The key to search for.
Returns
  • Returns true if the key exists, else false.

public boolean containsValue (Object value)

Added in API level 19

Check whether a value exists in the array. This requires a linear search through the entire array.

Parameters
value The value to search for.
Returns
  • Returns true if the value exists, else false.

public void ensureCapacity (int minimumCapacity)

Added in API level 19

Ensure the array map can hold at least minimumCapacity items.

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

Added in API level 19

Return a Set for iterating over and interacting with all mappings in the array map.

Note: this is a very inefficient way to access the array contents, it requires generating a number of temporary objects.

Note:

the semantics of this Set are subtly different than that of a HashMap: most important, the Map.Entry object returned by its iterator is a single object that exists for the entire iterator, so you can not hold on to it after calling Iterator.next.

Returns
  • a set of the mappings

public boolean equals (Object object)

Added in API level 19

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 returns false if the object is not a map, or if the maps have different sizes. Otherwise, for each key in this map, values of both maps are compared. If the values for any key are not equal, the method 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)

Added in API level 19

Retrieve a value from the array.

Parameters
key The key of the value to retrieve.
Returns
  • Returns the value associated with the given key, or null if there is no such key.

public int hashCode ()

Added in API level 19

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.

Returns
  • this object's hash code.

public int indexOfKey (Object key)

Added in API level 21

Returns the index of a key in the set.

Parameters
key The key to search for.
Returns
  • Returns the index of the key if it exists, else a negative integer.

public boolean isEmpty ()

Added in API level 19

Return true if the array map contains no items.

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

public K keyAt (int index)

Added in API level 19

Return the key at the given index in the array.

Parameters
index The desired index, must be between 0 and size()-1.
Returns
  • Returns the key stored at the given index.

public Set<K> keySet ()

Added in API level 19

Return a Set for iterating over and interacting with all keys in the array map.

Note: this is a fairly inefficient way to access the array contents, it requires generating a number of temporary objects.

Returns
  • a set of the keys.

public V put (K key, V value)

Added in API level 19

Add a new value to the array map.

Parameters
key The key under which to store the value. If this key already exists in the array, its value will be replaced.
value The value to store for the given key.
Returns
  • Returns the old value that was stored for the given key, or null if there was no such key.

public void putAll (ArrayMap<? extends K, ? extends V> array)

Added in API level 19

Perform a put(Object, Object) of all key/value pairs in array

Parameters
array The array whose contents are to be retrieved.

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

Added in API level 19

Perform a put(Object, Object) of all key/value pairs in map

Parameters
map The map whose contents are to be retrieved.

public V remove (Object key)

Added in API level 19

Remove an existing key from the array map.

Parameters
key The key of the mapping to remove.
Returns
  • Returns the value that was stored under the key, or null if there was no such key.

public boolean removeAll (Collection<?> collection)

Added in API level 19

Remove all keys in the array map that exist in the given collection.

Parameters
collection The collection whose contents are to be used to remove keys.
Returns
  • Returns true if any keys were removed from the array map, else false.

public V removeAt (int index)

Added in API level 19

Remove the key/value mapping at the given index.

Parameters
index The desired index, must be between 0 and size()-1.
Returns
  • Returns the value that was stored at this index.

public boolean retainAll (Collection<?> collection)

Added in API level 19

Remove all keys in the array map that do not exist in the given collection.

Parameters
collection The collection whose contents are to be used to determine which keys to keep.
Returns
  • Returns true if any keys were removed from the array map, else false.

public V setValueAt (int index, V value)

Added in API level 19

Set the value at a given index in the array.

Parameters
index The desired index, must be between 0 and size()-1.
value The new value to store at this index.
Returns
  • Returns the previous value at the given index.

public int size ()

Added in API level 19

Return the number of items in this array map.

Returns
  • the number of mappings in this Map.

public String toString ()

Added in API level 19

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 over its mappings. 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 V valueAt (int index)

Added in API level 19

Return the value at the given index in the array.

Parameters
index The desired index, must be between 0 and size()-1.
Returns
  • Returns the value stored at the given index.

public Collection<V> values ()

Added in API level 19

Return a Collection for iterating over and interacting with all values in the array map.

Note: this is a fairly inefficient way to access the array contents, it requires generating a number of temporary objects.

Returns
  • a collection of the values contained in this map.