# SparseArrayCompat

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

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  open class SparseArrayCompat<E : Any?>
  ```

*** ** * ** ***

SparseArrays map integers to Objects. Unlike a normal array of Objects, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Integers to Objects, both because it avoids auto-boxing keys and its data structure doesn't rely on an extra entry object for each mapping.

Note that this container keeps its mappings in an array data structure, using a binary search to find keys. The 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%.

To help with performance, the container includes an optimization when removing keys: instead of compacting its array immediately, it leaves the removed entry marked as deleted. The entry can then be re-used for the same key, or compacted later in a single garbage collection step of all removed entries. This garbage collection will need to be performed at any time the array needs to be grown or the map size or entry values are retrieved.

It is possible to iterate over the items in this container using `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#keyAt(kotlin.Int)` and `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#valueAt(kotlin.Int)`. Iterating over the keys using `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#keyAt(kotlin.Int)` with ascending values of the index will return the keys in ascending order, or the values corresponding to the keys in ascending order in the case of `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#valueAt(kotlin.Int)`.

## Summary

| ### Public constructors |
|---|---|
| `<E : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#SparseArrayCompat(kotlin.Int)(initialCapacity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Creates a new SparseArray containing no mappings that will not require any additional memory allocation to store the specified number of mappings. | Cmn android |

| ### Public functions |
|---|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#append(kotlin.Int,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, value: E)` Puts a key/value pair into the array, optimizing for the case where the key is greater than all existing keys in the array. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#clear()()` Removes all key-value mappings from this SparseArray. | Cmn android |
| `open https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat<E>` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#clone()()` | android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#containsKey(kotlin.Int)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns true if the specified key is mapped. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#containsValue(kotlin.Any)(value: E)` Returns true if the specified value is mapped from any key. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[delete](https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#delete(kotlin.Int))(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` **This function is deprecated.** Alias for remove(int). | android |
| `open operator E?` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#get(kotlin.Int)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Gets the Object mapped from the specified key, or `null` if no such mapping has been made. | Cmn android |
| `open E` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#get(kotlin.Int,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, defaultValue: E)` Gets the Object mapped from the specified `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#get(kotlin.Int,kotlin.Any)`, or `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#get(kotlin.Int,kotlin.Any)` if no such mapping has been made. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#indexOfKey(kotlin.Int)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns the index for which `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#keyAt(kotlin.Int)` would return the specified `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#indexOfKey(kotlin.Int)`, or a negative number if the specified `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#indexOfKey(kotlin.Int)` is not mapped. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#indexOfValue(kotlin.Any)(value: E)` Returns an index for which `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#valueAt(kotlin.Int)` would return the specified key, or a negative number if no keys map to the specified `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#indexOfValue(kotlin.Any)`. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#isEmpty()()` Return true if `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#size()` is 0. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#keyAt(kotlin.Int)(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Given an index in the range `0...[size]-1`, returns the key from the `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#keyAt(kotlin.Int)`th key-value mapping that this SparseArray stores. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#put(kotlin.Int,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, value: E)` Adds a mapping from the specified key to the specified `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#put(kotlin.Int,kotlin.Any)`, replacing the previous mapping from the specified `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#put(kotlin.Int,kotlin.Any)` if there was one. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#putAll(androidx.collection.SparseArrayCompat)(other: https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat<E>)` Copies all of the mappings from the `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#putAll(androidx.collection.SparseArrayCompat)` to this map. | Cmn android |
| `open E?` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#putIfAbsent(kotlin.Int,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, value: E)` Add a new value to the array map only if the key does not already have a value or it is mapped to `null`. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#remove(kotlin.Int)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Removes the mapping from the specified key, if there was any. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#remove(kotlin.Int,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, value: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?)` Remove an existing key from the array map only if it is currently mapped to `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#remove(kotlin.Int,kotlin.Any)`. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#removeAt(kotlin.Int)(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Removes the mapping at the specified index. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#removeAtRange(kotlin.Int,kotlin.Int)(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, size: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Remove a range of mappings as a batch. | Cmn android |
| `open E?` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#replace(kotlin.Int,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, value: E)` Replace the mapping for `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#replace(kotlin.Int,kotlin.Any)` only if it is already mapped to a value. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#replace(kotlin.Int,kotlin.Any,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, oldValue: E, newValue: E)` Replace the mapping for `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#replace(kotlin.Int,kotlin.Any,kotlin.Any)` only if it is already mapped to a value. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#setValueAt(kotlin.Int,kotlin.Any)(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, value: E)` Given an index in the range `0...size()-1`, sets a new value for the `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#setValueAt(kotlin.Int,kotlin.Any)`th key-value mapping that this SparseArray stores. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#size()()` Returns the number of key-value mappings that this SparseArray currently stores. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#toString()()` Returns a string representation of the object. | Cmn android |
| `open E` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#valueAt(kotlin.Int)(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Given an index in the range `0...[size]-1`, returns the value from the `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#valueAt(kotlin.Int)`th key-value mapping that this SparseArray stores. | Cmn android |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#isEmpty()` Return true if `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#size()` is 0. | android |

| ### Extension functions |
|---|---|---|
| `inline operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat<T>.https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#(androidx.collection.SparseArrayCompat).contains(kotlin.Int)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns true if the collection contains `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.SparseArrayCompat).contains(kotlin.Int)`. | Cmn |
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat<T>.https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#(androidx.collection.SparseArrayCompat).forEach(kotlin.Function2)(action: (key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, value) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` Performs the given `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.SparseArrayCompat).forEach(kotlin.Function2)` for each key/value entry. | Cmn |
| `inline T` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat<T>.https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#(androidx.collection.SparseArrayCompat).getOrDefault(kotlin.Int,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, defaultValue: T)` Return the value corresponding to `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.SparseArrayCompat).getOrDefault(kotlin.Int,kotlin.Any)`, or `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.SparseArrayCompat).getOrDefault(kotlin.Int,kotlin.Any)` when not present. | Cmn |
| `inline T` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat<T>.https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#(androidx.collection.SparseArrayCompat).getOrElse(kotlin.Int,kotlin.Function0)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, defaultValue: () -> T)` Return the value corresponding to `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.SparseArrayCompat).getOrElse(kotlin.Int,kotlin.Function0)`, or from `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.SparseArrayCompat).getOrElse(kotlin.Int,kotlin.Function0)` when not present. | Cmn |
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat<T>.https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#(androidx.collection.SparseArrayCompat).isNotEmpty()()` Return true when the collection contains elements. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-int-iterator/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat<T>.https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#(androidx.collection.SparseArrayCompat).keyIterator()()` Return an iterator over the collection's keys. | Cmn |
| `operator https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat<T>` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat<T>.https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#(androidx.collection.SparseArrayCompat).plus(androidx.collection.SparseArrayCompat)(other: https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat<T>)` Creates a new collection by adding or replacing entries from `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.SparseArrayCompat).plus(androidx.collection.SparseArrayCompat)`. | Cmn |
| `inline operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat<T>.https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#(androidx.collection.SparseArrayCompat).set(kotlin.Int,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, value: T)` Allows the use of the index operator for storing values in the collection. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterator/index.html<T>` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat<T>.https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#(androidx.collection.SparseArrayCompat).valueIterator()()` Return an iterator over the collection's values. | Cmn |

| ### Extension properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat<T>.https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#(androidx.collection.SparseArrayCompat).size()` Returns the number of key/value pairs in the collection. | Cmn |

## Public constructors

### SparseArrayCompat

Cmn android

```
<E : Any?> SparseArrayCompat(initialCapacity: Int = 10)
```

Creates a new SparseArray containing no mappings that will not require any additional memory allocation to store the specified number of mappings. If you supply an initial capacity of 0, the sparse array will be initialized with a light-weight representation not requiring any additional array allocations.

| Parameters |
|---|---|
| `initialCapacity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = 10` | initial capacity of the array. The array will not require any additional memory allocation to store the specified number of mappings. If you supply an initialCapacity of 0, the sparse array will be initialized with a light-weight representation not requiring any additional array allocations. Default initialCapacity is 10. |

## Public functions

### append

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

```
open fun append(key: Int, value: E): Unit
```

Puts a key/value pair into the array, optimizing for the case where the key is greater than all existing keys in the array.

### clear

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

```
open fun clear(): Unit
```

Removes all key-value mappings from this SparseArray.

### clone

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

```
open fun clone(): SparseArrayCompat<E>
```

### containsKey

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

```
open fun containsKey(key: Int): Boolean
```

Returns true if the specified key is mapped.

### containsValue

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

```
open fun containsValue(value: E): Boolean
```

Returns true if the specified value is mapped from any key.

### delete

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

```
open fun [delete](https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#delete(kotlin.Int))(key: Int): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> Alias for remove(int), use this instead:
>
> ```kotlin
> remove(key)
> ```

### get

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

```
open operator fun get(key: Int): E?
```

Gets the Object mapped from the specified key, or `null` if no such mapping has been made.

### get

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

```
open fun get(key: Int, defaultValue: E): E
```

Gets the Object mapped from the specified `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#get(kotlin.Int,kotlin.Any)`, or `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#get(kotlin.Int,kotlin.Any)` if no such mapping has been made.

### indexOfKey

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

```
open fun indexOfKey(key: Int): Int
```

Returns the index for which `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#keyAt(kotlin.Int)` would return the specified `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#indexOfKey(kotlin.Int)`, or a negative number if the specified `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#indexOfKey(kotlin.Int)` is not mapped.

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | the key to search for |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | the index for which `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#keyAt(kotlin.Int)` would return the specified `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#indexOfKey(kotlin.Int)`, or a negative number if the specified `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#indexOfKey(kotlin.Int)` is not mapped |

### indexOfValue

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

```
open fun indexOfValue(value: E): Int
```

Returns an index for which `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#valueAt(kotlin.Int)` would return the specified key, or a negative number if no keys map to the specified `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#indexOfValue(kotlin.Any)`.

Beware that this is a linear search, unlike lookups by key, and that multiple keys can map to the same value and this will find only one of them.

Note also that unlike most collections' `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.ArraySet).indexOf(kotlin.Any,kotlin.Int)` methods, this method compares values using `===` rather than `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/equals.html`.

### isEmpty

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

```
open fun isEmpty(): Boolean
```

Return true if `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#size()` is 0.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#size()` is 0. |

### keyAt

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

```
open fun keyAt(index: Int): Int
```

Given an index in the range `0...[size]-1`, returns the key from the `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#keyAt(kotlin.Int)`th key-value mapping that this SparseArray stores.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-index-out-of-bounds-exception/index.html` | if index is not within `0..[size]-1` |

### put

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

```
open fun put(key: Int, value: E): Unit
```

Adds a mapping from the specified key to the specified `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#put(kotlin.Int,kotlin.Any)`, replacing the previous mapping from the specified `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#put(kotlin.Int,kotlin.Any)` if there was one.

### putAll

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

```
open fun putAll(other: SparseArrayCompat<E>): Unit
```

Copies all of the mappings from the `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#putAll(androidx.collection.SparseArrayCompat)` to this map. The effect of this call is equivalent to that of calling `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#put(kotlin.Int,kotlin.Any)` on this map once for each mapping from key to value in `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#putAll(androidx.collection.SparseArrayCompat)`.

### putIfAbsent

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

```
open fun putIfAbsent(key: Int, value: E): E?
```

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 |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The key under which to store the value. |
| `value: E` | The value to store for the given key. |

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

### remove

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

```
open fun remove(key: Int): Unit
```

Removes the mapping from the specified key, if there was any.

### remove

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

```
open fun remove(key: Int, value: Any?): Boolean
```

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

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The key of the mapping to remove. |
| `value: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?` | The value expected to be mapped to the key. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Returns `true` if the mapping was removed. |

### removeAt

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

```
open fun removeAt(index: Int): Unit
```

Removes the mapping at the specified index.

### removeAtRange

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

```
open fun removeAtRange(index: Int, size: Int): Unit
```

Remove a range of mappings as a batch.

| Parameters |
|---|---|
| `index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Index to begin at |
| `size: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Number of mappings to remove |

### replace

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

```
open fun replace(key: Int, value: E): E?
```

Replace the mapping for `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#replace(kotlin.Int,kotlin.Any)` only if it is already mapped to a value.

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The key of the mapping to replace. |
| `value: E` | The value to store for the given key. |

| Returns |
|---|---|
| `E?` | Returns the previous mapped value or `null`. |

### replace

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

```
open fun replace(key: Int, oldValue: E, newValue: E): Boolean
```

Replace the mapping for `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#replace(kotlin.Int,kotlin.Any,kotlin.Any)` only if it is already mapped to a value.

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The key of the mapping to replace. |
| `oldValue: E` | The value expected to be mapped to the key. |
| `newValue: E` | The value to store for the given key. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Returns `true` if the value was replaced. |

### setValueAt

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

```
open fun setValueAt(index: Int, value: E): Unit
```

Given an index in the range `0...size()-1`, sets a new value for the `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#setValueAt(kotlin.Int,kotlin.Any)`th key-value mapping that this SparseArray stores.

### size

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

```
open fun size(): Int
```

Returns the number of key-value mappings that this SparseArray currently stores.

### toString

Cmn android

```
open fun toString(): String
```

Returns a string representation of the object.

This implementation composes a string by iterating over its mappings. If this map contains itself as a value, the string "(this Map)" will appear in its place.

### valueAt

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

```
open fun valueAt(index: Int): E
```

Given an index in the range `0...[size]-1`, returns the value from the `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#valueAt(kotlin.Int)`th key-value mapping that this SparseArray stores.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-index-out-of-bounds-exception/index.html` | if index is not within `0..[size]-1` |

## Public properties

### isEmpty

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

```
val isEmpty: Boolean
```

Return true if `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#size()` is 0.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if `https://developer.android.com/reference/kotlin/androidx/collection/SparseArrayCompat#size()` is 0. |

## Extension functions

### SparseArrayCompat.contains

Cmn Artifact: [androidx.collection:collection](https://developer.android.com/jetpack/androidx/releases/collection) [View Source](https://cs.android.com/search?q=file:androidx/collection/SparseArray.kt+function:contains)

```
inline operator fun <T : Any?> SparseArrayCompat<T>.contains(key: Int): Boolean
```

Returns true if the collection contains `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.SparseArrayCompat).contains(kotlin.Int)`.

### SparseArrayCompat.forEach

Cmn Artifact: [androidx.collection:collection](https://developer.android.com/jetpack/androidx/releases/collection) [View Source](https://cs.android.com/search?q=file:androidx/collection/SparseArray.kt+function:forEach)

```
inline fun <T : Any?> SparseArrayCompat<T>.forEach(action: (key: Int, value) -> Unit): Unit
```

Performs the given `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.SparseArrayCompat).forEach(kotlin.Function2)` for each key/value entry.

### SparseArrayCompat.getOrDefault

Cmn Artifact: [androidx.collection:collection](https://developer.android.com/jetpack/androidx/releases/collection) [View Source](https://cs.android.com/search?q=file:androidx/collection/SparseArray.kt+function:getOrDefault)

```
inline fun <T : Any?> SparseArrayCompat<T>.getOrDefault(key: Int, defaultValue: T): T
```

Return the value corresponding to `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.SparseArrayCompat).getOrDefault(kotlin.Int,kotlin.Any)`, or `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.SparseArrayCompat).getOrDefault(kotlin.Int,kotlin.Any)` when not present.

### SparseArrayCompat.getOrElse

Cmn Artifact: [androidx.collection:collection](https://developer.android.com/jetpack/androidx/releases/collection) [View Source](https://cs.android.com/search?q=file:androidx/collection/SparseArray.kt+function:getOrElse)

```
inline fun <T : Any?> SparseArrayCompat<T>.getOrElse(key: Int, defaultValue: () -> T): T
```

Return the value corresponding to `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.SparseArrayCompat).getOrElse(kotlin.Int,kotlin.Function0)`, or from `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.SparseArrayCompat).getOrElse(kotlin.Int,kotlin.Function0)` when not present.

### SparseArrayCompat.isNotEmpty

Cmn Artifact: [androidx.collection:collection](https://developer.android.com/jetpack/androidx/releases/collection) [View Source](https://cs.android.com/search?q=file:androidx/collection/SparseArray.kt+function:isNotEmpty)

```
inline fun <T : Any?> SparseArrayCompat<T>.isNotEmpty(): Boolean
```

Return true when the collection contains elements.

### SparseArrayCompat.keyIterator

Cmn Artifact: [androidx.collection:collection](https://developer.android.com/jetpack/androidx/releases/collection) [View Source](https://cs.android.com/search?q=file:androidx/collection/SparseArray.kt+function:keyIterator)

```
fun <T : Any?> SparseArrayCompat<T>.keyIterator(): IntIterator
```

Return an iterator over the collection's keys.

### SparseArrayCompat.plus

Cmn Artifact: [androidx.collection:collection](https://developer.android.com/jetpack/androidx/releases/collection) [View Source](https://cs.android.com/search?q=file:androidx/collection/SparseArray.kt+function:plus)

```
operator fun <T : Any?> SparseArrayCompat<T>.plus(other: SparseArrayCompat<T>): SparseArrayCompat<T>
```

Creates a new collection by adding or replacing entries from `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.SparseArrayCompat).plus(androidx.collection.SparseArrayCompat)`.

### SparseArrayCompat.set

Cmn Artifact: [androidx.collection:collection](https://developer.android.com/jetpack/androidx/releases/collection) [View Source](https://cs.android.com/search?q=file:androidx/collection/SparseArray.kt+function:set)

```
inline operator fun <T : Any?> SparseArrayCompat<T>.set(key: Int, value: T): Unit
```

Allows the use of the index operator for storing values in the collection.

### SparseArrayCompat.valueIterator

Cmn Artifact: [androidx.collection:collection](https://developer.android.com/jetpack/androidx/releases/collection) [View Source](https://cs.android.com/search?q=file:androidx/collection/SparseArray.kt+function:valueIterator)

```
fun <T : Any?> SparseArrayCompat<T>.valueIterator(): Iterator<T>
```

Return an iterator over the collection's values.

## Extension properties

### SparseArrayCompat.size

Cmn Artifact: [androidx.collection:collection](https://developer.android.com/jetpack/androidx/releases/collection) [View Source](https://cs.android.com/search?q=file:androidx/collection/SparseArray.kt+symbol:size)

```
val SparseArrayCompat<T>.size: Int
```

Returns the number of key/value pairs in the collection.