# ArrayMap

Artifact: [androidx.collection:collection](https://developer.android.com/jetpack/androidx/releases/collection) [View Source](https://cs.android.com/search?q=file:androidx/collection/ArrayMap.java+class:androidx.collection.ArrayMap) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

*** ** * ** ***


```
public class ArrayMap<K, V> extends SimpleArrayMap<K, V> implements Map<K, V>
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.collection.SimpleArrayMap](https://developer.android.com/reference/androidx/collection/SimpleArrayMap)\<K, V\> ||
|   | ↳ | [androidx.collection.ArrayMap](https://developer.android.com/reference/androidx/collection/ArrayMap) |

*** ** * ** ***

ArrayMap is a generic key-\>value mapping data structure that is designed to be more memory efficient than a traditional `https://developer.android.com/reference/java/util/HashMap.html`, this implementation is a version of the platform's `android.util.ArrayMap` that can be used on older versions of the platform. 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).

If you don't need the standard Java container APIs provided here (iterators etc), consider using `https://developer.android.com/reference/androidx/collection/SimpleArrayMap` instead.

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.

**NOTE:** Consider using `https://developer.android.com/reference/androidx/collection/MutableScatterMap` instead of this class. `https://developer.android.com/reference/androidx/collection/MutableScatterMap` also avoids creating an extra object per entry but offers overall better performance characteristics. If a `https://developer.android.com/reference/java/util/Map.html`-like interface is required, please see `https://developer.android.com/reference/androidx/collection/ScatterMap#asMap()` and `https://developer.android.com/reference/androidx/collection/MutableScatterMap#asMutableMap()`.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/collection/ArrayMap#ArrayMap()()` |
| `https://developer.android.com/reference/androidx/collection/ArrayMap#ArrayMap(int)(int capacity)` Create a new ArrayMap with a given initial capacity. |
| `https://developer.android.com/reference/androidx/collection/ArrayMap#ArrayMap(androidx.collection.SimpleArrayMap)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/androidx/collection/SimpleArrayMap map)` Create a new ArrayMap with the mappings from the given ArrayMap. |

| ### Public methods |
|---|---|
| `boolean` | `https://developer.android.com/reference/androidx/collection/ArrayMap#containsAll(java.util.Collection<?>)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?> collection)` Determine if the array map contains all of the keys in the given collection. |
| `boolean` | `https://developer.android.com/reference/androidx/collection/ArrayMap#containsKey(java.lang.Object)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/Object.html key)` Check whether a key exists in the array. |
| `boolean` | `https://developer.android.com/reference/androidx/collection/ArrayMap#containsValue(java.lang.Object)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/Object.html value)` Check whether a value exists in the array. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Set.html<https://developer.android.com/reference/java/util/Map.Entry.html<K, V>>` | `https://developer.android.com/reference/androidx/collection/ArrayMap#entrySet()()` Return a `https://developer.android.com/reference/java/util/Set.html` for iterating over and interacting with all mappings in the array map. |
| `V` | `https://developer.android.com/reference/androidx/collection/ArrayMap#get(java.lang.Object)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/Object.html key)` Retrieve a value from the array. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Set.html<K>` | `https://developer.android.com/reference/androidx/collection/ArrayMap#keySet()()` Return a `https://developer.android.com/reference/java/util/Set.html` for iterating over and interacting with all keys in the array map. |
| `void` | `https://developer.android.com/reference/androidx/collection/ArrayMap#putAll(java.util.Map<? extends K,? extends V>)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Map.html<K, V> map)` Perform a `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#put(K,V)` of all key/value pairs in <var translate="no">map</var> |
| `V` | `https://developer.android.com/reference/androidx/collection/ArrayMap#remove(java.lang.Object)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/Object.html key)` Remove an existing key from the array map. |
| `boolean` | `https://developer.android.com/reference/androidx/collection/ArrayMap#removeAll(java.util.Collection<?>)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?> collection)` Remove all keys in the array map that exist in the given collection. |
| `boolean` | `https://developer.android.com/reference/androidx/collection/ArrayMap#retainAll(java.util.Collection<?>)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?> collection)` Remove all keys in the array map that do **not** exist in the given collection. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Collection.html<V>` | `https://developer.android.com/reference/androidx/collection/ArrayMap#values()()` Return a `https://developer.android.com/reference/java/util/Collection.html` for iterating over and interacting with all values in the array map. |

### Inherited methods

From [java.util.Map](https://developer.android.com/reference/java/util/Map.html) `abstract void` `https://developer.android.com/reference/java/util/Map.html#clear()()` `V` `https://developer.android.com/reference/java/util/Map.html#compute(K, java.util.function.BiFunction&lt;? super K,? super V,? extends V&gt;)(K key, https://developer.android.com/reference/java/util/function/BiFunction.html<K, V, V> remappingFunction)` `V` `https://developer.android.com/reference/java/util/Map.html#computeIfAbsent(K, java.util.function.Function&lt;? super K,? extends V&gt;)(K key, https://developer.android.com/reference/java/util/function/Function.html<K, V> mappingFunction)` `V` `https://developer.android.com/reference/java/util/Map.html#computeIfPresent(K, java.util.function.BiFunction&lt;? super K,? super V,? extends V&gt;)(K key, https://developer.android.com/reference/java/util/function/BiFunction.html<K, V, V> remappingFunction)` `static https://developer.android.com/reference/java/util/Map.html<K, V>` `<K, V> https://developer.android.com/reference/java/util/Map.html#copyOf(java.util.Map&lt;? extends K,? extends V&gt;)(https://developer.android.com/reference/java/util/Map.html<K, V> map)` `static https://developer.android.com/reference/java/util/Map.Entry.html<K, V>` `<K, V> https://developer.android.com/reference/java/util/Map.html#entry(K, V)(K k, V v)` `abstract boolean` `https://developer.android.com/reference/java/util/Map.html#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object.html p)` `void` `https://developer.android.com/reference/java/util/Map.html#forEach(java.util.function.BiConsumer&lt;? super K,? super V&gt;)(https://developer.android.com/reference/java/util/function/BiConsumer.html<K, V> action)` `V` `https://developer.android.com/reference/java/util/Map.html#getOrDefault(java.lang.Object, V)(https://developer.android.com/reference/java/lang/Object.html key, V defaultValue)` `abstract int` `https://developer.android.com/reference/java/util/Map.html#hashCode()()` `abstract boolean` `https://developer.android.com/reference/java/util/Map.html#isEmpty()()` `V` `https://developer.android.com/reference/java/util/Map.html#merge(K, V, java.util.function.BiFunction&lt;? super V,? super V,? extends V&gt;)(K key, V value, https://developer.android.com/reference/java/util/function/BiFunction.html<V, V, V> remappingFunction)` `static https://developer.android.com/reference/java/util/Map.html<K, V>` `<K, V> https://developer.android.com/reference/java/util/Map.html#of()()` `static https://developer.android.com/reference/java/util/Map.html<K, V>` `<K, V> https://developer.android.com/reference/java/util/Map.html#ofEntries(java.util.Map.Entry&lt;? extends K,? extends V&gt;...)(Map.Entry[] entries)` `abstract V` `https://developer.android.com/reference/java/util/Map.html#put(K, V)(K p, V p1)` `V` `https://developer.android.com/reference/java/util/Map.html#putIfAbsent(K, V)(K key, V value)` `boolean` `https://developer.android.com/reference/java/util/Map.html#remove(java.lang.Object, java.lang.Object)(https://developer.android.com/reference/java/lang/Object.html key, https://developer.android.com/reference/java/lang/Object.html value)` `V` `https://developer.android.com/reference/java/util/Map.html#replace(K, V)(K key, V value)` `boolean` `https://developer.android.com/reference/java/util/Map.html#replace(K, V, V)(K key, V oldValue, V newValue)` `void` `https://developer.android.com/reference/java/util/Map.html#replaceAll(java.util.function.BiFunction&lt;? super K,? super V,? extends V&gt;)(https://developer.android.com/reference/java/util/function/BiFunction.html<K, V, V> function)` `abstract int` `https://developer.android.com/reference/java/util/Map.html#size()()` From [androidx.collection.SimpleArrayMap](https://developer.android.com/reference/androidx/collection/SimpleArrayMap) `final int` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#__restricted$indexOfValue(V)(V value)` `void` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#clear()()`

Make the array map empty.
`void` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#ensureCapacity(int)(int minimumCapacity)`

Ensure the array map can hold at least `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#ensureCapacity(kotlin.Int)` items.
`boolean` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object.html other)`

This implementation returns `false` if the object is not a `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-map/index.html` or `https://developer.android.com/reference/androidx/collection/SimpleArrayMap`, or if the maps have different sizes.
`V` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#getOrDefault(java.lang.Object,V)(https://developer.android.com/reference/java/lang/Object.html key, V defaultValue)`

Retrieve a value from the array, or `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#getOrDefault(kotlin.Any,kotlin.Any)` if there is no mapping for the key.
`int` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#hashCode()()` `int` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#indexOfKey(K)(K key)`

Returns the index of a key in the set.
`boolean` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#isEmpty()()`

Return `true` if the array map contains no items.
`K` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#keyAt(int)(int index)`

Return the key at the given index in the array.
`V` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#put(K,V)(K key, V value)`

Add a new value to the array map.
`void` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#putAll(androidx.collection.SimpleArrayMap<? extends K,? extends V>)(https://developer.android.com/reference/androidx/collection/SimpleArrayMap<K, V> map)`

Perform a `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#put(kotlin.Any,kotlin.Any)` of all key/value pairs in `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#putAll(androidx.collection.SimpleArrayMap)`
`V` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#putIfAbsent(K,V)(K key, V value)`

Add a new value to the array map only if the key does not already have a value or it is mapped to `null`.
`boolean` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#remove(K,V)(K key, V value)`

Remove an existing key from the array map only if it is currently mapped to `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#remove(kotlin.Any,kotlin.Any)`.
`V` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#removeAt(int)(int index)`

Remove the key/value mapping at the given index.
`V` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#replace(K,V)(K key, V value)`

Replace the mapping for `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#replace(kotlin.Any,kotlin.Any)` only if it is already mapped to a value.
`boolean` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#replace(K,V,V)(K key, V oldValue, V newValue)`

Replace the mapping for `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#replace(kotlin.Any,kotlin.Any,kotlin.Any)` only if it is already mapped to `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#replace(kotlin.Any,kotlin.Any,kotlin.Any)`.
`V` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#setValueAt(int,V)(int index, V value)`

Set the value at a given index in the array.
`int` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#size()()`

Return the number of items in this array map.
`https://developer.android.com/reference/java/lang/String.html` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#toString()()`

Returns a string representation of the object.
`V` `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#valueAt(int)(int index)`

Return the value at the given index in the array.

## Public constructors

### ArrayMap

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public ArrayMap()
```

### ArrayMap

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public ArrayMap(int capacity)
```

Create a new ArrayMap with a given initial capacity.

### ArrayMap

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public ArrayMap(@Nullable SimpleArrayMap map)
```

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

## Public methods

### containsAll

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public boolean containsAll(@NonNull Collection<@NonNull ?> collection)
```

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?> collection` | The collection whose contents are to be checked against. |

| Returns |
|---|---|
| `boolean` | Returns true if this array map contains a key for every entry in <var translate="no">collection</var>, else returns false. |

### containsKey

```
public boolean containsKey(@Nullable Object key)
```

Check whether a key exists in the array.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/Object.html key` | The key to search for. \*\* This must be the same type as <var translate="no">K</var> \*\* |

| Returns |
|---|---|
| `boolean` | Returns `true` if the key exists, else `false`. |

### containsValue

```
public boolean containsValue(@Nullable Object value)
```

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/Object.html value` | The value to search for. \*\* This must be the same type as <var translate="no">V</var> \*\* |

| Returns |
|---|---|
| `boolean` | Returns `true` if the value exists, else `false`. |

### entrySet

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public @NonNull Set<Map.Entry<K, V>> entrySet()
```

Return a `https://developer.android.com/reference/java/util/Set.html` 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 `https://developer.android.com/reference/java/util/HashMap.html`: most important, the `https://developer.android.com/reference/java/util/Map.Entry.html` 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 `https://developer.android.com/reference/java/util/Iterator.html#next()`.

### get

```
public V get(@Nullable Object key)
```

Retrieve a value from the array.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/Object.html key` | The key of the value to retrieve. \*\* This must be the same type as <var translate="no">K</var> \*\* |

| Returns |
|---|---|
| `V` | Returns the value associated with the given key, or `null` if there is no such key. |

### keySet

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public @NonNull Set<K> keySet()
```

Return a `https://developer.android.com/reference/java/util/Set.html` 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.

### putAll

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public void putAll(@NonNull Map<K, V> map)
```

Perform a `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#put(K,V)` of all key/value pairs in <var translate="no">map</var>

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Map.html<K, V> map` | The map whose contents are to be retrieved. |

### remove

```
public V remove(@Nullable Object key)
```

Remove an existing key from the array map.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/Object.html key` | The key of the mapping to remove. \*\* This must be the same type as <var translate="no">V</var> \*\* |

| Returns |
|---|---|
| `V` | Returns the value that was stored under the key, or `null` if there was no such key. |

### removeAll

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public boolean removeAll(@NonNull Collection<@NonNull ?> collection)
```

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?> collection` | The collection whose contents are to be used to remove keys. |

| Returns |
|---|---|
| `boolean` | Returns true if any keys were removed from the array map, else false. |

### retainAll

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public boolean retainAll(@NonNull Collection<@NonNull ?> collection)
```

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?> collection` | The collection whose contents are to be used to determine which keys to keep. |

| Returns |
|---|---|
| `boolean` | Returns true if any keys were removed from the array map, else false. |

### values

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public @NonNull Collection<V> values()
```

Return a `https://developer.android.com/reference/java/util/Collection.html` 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.