# SimpleArrayMap

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

*** ** * ** ***

<br />

```
public class SimpleArrayMap<K extends Object, V extends Object>
```

<br />

Known direct subclasses [ArrayMap](https://developer.android.com/reference/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. |

*** ** * ** ***

Base implementation of androidx.collection.ArrayMap that doesn't include any standard Java container API interoperability. These features are generally heavier-weight ways to interact with the container, so discouraged, but they can be useful to make it easier to use as a drop-in replacement for HashMap. If you don't need them, this class can be preferable since it doesn't bring in any of the implementation of those APIs, allowing that code to be stripped by ProGuard.

**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 a new object per entry but offers better performance characteristics. If a `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-map/index.html` interface is required, see `https://developer.android.com/reference/androidx/collection/MutableScatterMap#asMap()`. If a `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-map/index.html` interface is required, see `https://developer.android.com/reference/androidx/collection/MutableScatterMap#asMutableMap()`. A `https://developer.android.com/reference/androidx/collection/MutableScatterMap` can also be passed as a `https://developer.android.com/reference/androidx/collection/ScatterMap` to provide a read-only API surface.

## Summary

| ### Public constructors |
|---|
| `<K extends https://developer.android.com/reference/java/lang/Object.html, V extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/collection/SimpleArrayMap#SimpleArrayMap(kotlin.Int)(int capacity)` Create a new `https://developer.android.com/reference/androidx/collection/SimpleArrayMap` with a given initial capacity. |
| `<K extends https://developer.android.com/reference/java/lang/Object.html, V extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/collection/SimpleArrayMap#SimpleArrayMap(androidx.collection.SimpleArrayMap)( https://developer.android.com/reference/androidx/collection/SimpleArrayMap<@https://developer.android.com/reference/androidx/annotation/NonNull K, @https://developer.android.com/reference/androidx/annotation/NonNull V> map )` Create a new `https://developer.android.com/reference/androidx/collection/SimpleArrayMap` with the mappings from the given `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#(androidx.collection.SimpleArrayMap)`. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#clear()()` Make the array map empty. |
| `boolean` | `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#containsKey(kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull K key)` Check whether a key exists in the array. |
| `boolean` | `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#containsValue(kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull V value)` Check whether a value exists in the array. |
| `void` | `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#ensureCapacity(kotlin.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(kotlin.Any)(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#get(kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull K key)` Retrieve a value from the array. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull V` | `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#getOrDefault(kotlin.Any,kotlin.Any)(https://developer.android.com/reference/java/lang/Object.html key, @https://developer.android.com/reference/androidx/annotation/NonNull 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(kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull 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. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull K` | `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#keyAt(kotlin.Int)(int index)` Return the key at the given index in the array. |
| `V` | `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#put(kotlin.Any,kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull K key, @https://developer.android.com/reference/androidx/annotation/NonNull V value)` Add a new value to the array map. |
| `void` | `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#putAll(androidx.collection.SimpleArrayMap)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/collection/SimpleArrayMap<@https://developer.android.com/reference/androidx/annotation/NonNull K, @https://developer.android.com/reference/androidx/annotation/NonNull 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(kotlin.Any,kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull K key, @https://developer.android.com/reference/androidx/annotation/NonNull 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`. |
| `V` | `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#remove(kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull K key)` Remove an existing key from the array map. |
| `boolean` | `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#remove(kotlin.Any,kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull K key, @https://developer.android.com/reference/androidx/annotation/NonNull 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)`. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull V` | `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#removeAt(kotlin.Int)(int index)` Remove the key/value mapping at the given index. |
| `V` | `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#replace(kotlin.Any,kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull K key, @https://developer.android.com/reference/androidx/annotation/NonNull 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(kotlin.Any,kotlin.Any,kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull K key, @https://developer.android.com/reference/androidx/annotation/NonNull V oldValue, @https://developer.android.com/reference/androidx/annotation/NonNull 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)`. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull V` | `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#setValueAt(kotlin.Int,kotlin.Any)(int index, @https://developer.android.com/reference/androidx/annotation/NonNull 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/androidx/annotation/NonNull 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. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull V` | `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#valueAt(kotlin.Int)(int index)` Return the value at the given index in the array. |

## Public constructors

### SimpleArrayMap

```
public <K extends Object, V extends Object> SimpleArrayMap(int capacity)
```

Create a new `https://developer.android.com/reference/androidx/collection/SimpleArrayMap` with a given initial capacity. The default capacity of an array map is 0, and will grow once items are added to it.

### SimpleArrayMap

```
public <K extends Object, V extends Object> SimpleArrayMap(
    SimpleArrayMap<@NonNull K, @NonNull V> map
)
```

Create a new `https://developer.android.com/reference/androidx/collection/SimpleArrayMap` with the mappings from the given `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#(androidx.collection.SimpleArrayMap)`.

## Public methods

### clear

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

```
public void clear()
```

Make the array map empty. All storage is released.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-concurrent-modification-exception/index.html` | if it was detected that this `https://developer.android.com/reference/androidx/collection/SimpleArrayMap` was written to while this operation was running. |

### containsKey

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

```
public boolean containsKey(@NonNull K key)
```

Check whether a key exists in the array.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull K key` | The key to search for. |

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

### containsValue

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

```
public boolean containsValue(@NonNull V value)
```

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

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull V value` | The value to search for. |

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

### ensureCapacity

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

```
public void ensureCapacity(int minimumCapacity)
```

Ensure the array map can hold at least `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#ensureCapacity(kotlin.Int)` items.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-concurrent-modification-exception/index.html` | if it was detected that this `https://developer.android.com/reference/androidx/collection/SimpleArrayMap` was written to while this operation was running. |

### equals

```
public boolean equals(Object 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. 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`.

### get

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

```
public V get(@NonNull K key)
```

Retrieve a value from the array.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull K key` | The key of the value to retrieve. |

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

### getOrDefault

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

```
public @NonNull V getOrDefault(Object key, @NonNull 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.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/Object.html key` | The key of the value to retrieve. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull V defaultValue` | The default mapping of the key |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull V` | Returns the value associated with the given key, or `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#getOrDefault(kotlin.Any,kotlin.Any)` if there is no mapping for the key. |

### hashCode

```
public int hashCode()
```

### indexOfKey

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

```
public int indexOfKey(@NonNull K key)
```

Returns the index of a key in the set.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull K key` | The key to search for. |

| Returns |
|---|---|
| `int` | Returns the index of the key if it exists, else a negative integer. |

### isEmpty

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

```
public boolean isEmpty()
```

Return `true` if the array map contains no items.

### keyAt

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

```
public @NonNull K keyAt(int index)
```

Return the key at the given index in the array.

| Parameters |
|---|---|
| `int index` | The desired index, must be between 0 and `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#size()`-1 (inclusive). |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull K` | Returns the key stored at the given index. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#keyAt(kotlin.Int)` is not between 0 and `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#size()`-1 |

### put

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

```
public V put(@NonNull K key, @NonNull V value)
```

Add a new value to the array map.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull K key` | The key under which to store the value. If this key already exists in the array, its value will be replaced. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull V value` | The value to store for the given key. |

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

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-concurrent-modification-exception/index.html` | if it was detected that this `https://developer.android.com/reference/androidx/collection/SimpleArrayMap` was written to while this operation was running. |

### putAll

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

```
public void putAll(@NonNull SimpleArrayMap<@NonNull K, @NonNull 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)`

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/collection/SimpleArrayMap<@https://developer.android.com/reference/androidx/annotation/NonNull K, @https://developer.android.com/reference/androidx/annotation/NonNull V> map` | The array whose contents are to be retrieved. |

### putIfAbsent

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

```
public V putIfAbsent(@NonNull K key, @NonNull 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`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull K key` | The key under which to store the value. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull V value` | The value to store for the given key. |

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

### remove

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

```
public V remove(@NonNull K key)
```

Remove an existing key from the array map.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull K key` | The key of the mapping to remove. |

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

### remove

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

```
public boolean remove(@NonNull K key, @NonNull 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)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull K key` | The key of the mapping to remove. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull V value` | The value expected to be mapped to the key. |

| Returns |
|---|---|
| `boolean` | Returns `true` if the mapping was removed. |

### removeAt

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

```
public @NonNull V removeAt(int index)
```

Remove the key/value mapping at the given index.

| Parameters |
|---|---|
| `int index` | The desired index, must be between 0 and `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#size()`-1 (inclusive). |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull V` | Returns the value that was stored at this index. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-concurrent-modification-exception/index.html` | if it was detected that this `https://developer.android.com/reference/androidx/collection/SimpleArrayMap` was written to while this operation was running. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#removeAt(kotlin.Int)` is not between 0 and `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#size()`-1 |

### replace

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

```
public V replace(@NonNull K key, @NonNull 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.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull K key` | The key of the mapping to replace. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull V value` | The value to store for the given key. |

| Returns |
|---|---|
| `V` | Returns the previous mapped value or `null`. |

### replace

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

```
public boolean replace(@NonNull K key, @NonNull V oldValue, @NonNull 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)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull K key` | The key of the mapping to replace. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull V oldValue` | The value expected to be mapped to the key. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull V newValue` | The value to store for the given key. |

| Returns |
|---|---|
| `boolean` | Returns `true` if the value was replaced. |

### setValueAt

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

```
public @NonNull V setValueAt(int index, @NonNull V value)
```

Set the value at a given index in the array.

| Parameters |
|---|---|
| `int index` | The desired index, must be between 0 and `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#size()`-1 (inclusive). |
| `@https://developer.android.com/reference/androidx/annotation/NonNull V value` | The new value to store at this index. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull V` | Returns the previous value at the given index. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#setValueAt(kotlin.Int,kotlin.Any)` is not between 0 and `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#size()`-1 |

### size

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

```
public int size()
```

Return the number of items in this array map.

### toString

```
public @NonNull String toString()
```

Returns a string representation of the object.

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.

### valueAt

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

```
public @NonNull V valueAt(int index)
```

Return the value at the given index in the array.

| Parameters |
|---|---|
| `int index` | The desired index, must be between 0 and `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#size()`-1 (inclusive). |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull V` | Returns the value stored at the given index. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#valueAt(kotlin.Int)` is not between 0 and `https://developer.android.com/reference/androidx/collection/SimpleArrayMap#size()`-1 |