# LongSparseArray

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

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

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

*** ** * ** ***

SparseArray mapping longs 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 Longs 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/LongSparseArray#keyAt(kotlin.Int)` and `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#valueAt(kotlin.Int)`. Iterating over the keys using `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#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/LongSparseArray#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/LongSparseArray#LongSparseArray(kotlin.Int)(initialCapacity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Creates a new `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray` 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/LongSparseArray#append(kotlin.Long,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/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/LongSparseArray#clear()()` Removes all key-value mappings from this `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray`. | Cmn android |
| `open https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray<E>` | `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#clone()()` | android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#containsKey(kotlin.Long)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Returns `true` if the specified `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#containsKey(kotlin.Long)` is mapped. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#containsValue(kotlin.Any)(value: E)` Returns `true` if the specified `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#containsValue(kotlin.Any)` 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/LongSparseArray#delete(kotlin.Long))(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` **This function is deprecated.** Alias for \`remove(key)\`. | Cmn android |
| `open operator E?` | `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#get(kotlin.Long)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Gets the value mapped from the specified `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#get(kotlin.Long)`, or `null` if no such mapping has been made. | Cmn android |
| `open E` | `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#get(kotlin.Long,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, defaultValue: E)` Gets the value mapped from the specified `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#get(kotlin.Long,kotlin.Any)`, or `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#get(kotlin.Long,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/LongSparseArray#indexOfKey(kotlin.Long)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Returns the index for which `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#keyAt(kotlin.Int)` would return the specified key, or a negative number if the specified key 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/LongSparseArray#indexOfValue(kotlin.Any)(value: E)` Returns an index for which `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#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/LongSparseArray#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/LongSparseArray#isEmpty()()` Return `true` if `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#size()` is 0. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#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 `index`th key-value mapping that this `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray` stores. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#put(kotlin.Long,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, value: E)` Adds a mapping from the specified key to the specified value, replacing the previous mapping from the specified key 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/LongSparseArray#putAll(androidx.collection.LongSparseArray)(other: https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray<E>)` Copies all of the mappings from `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#putAll(androidx.collection.LongSparseArray)` to this map. | Cmn android |
| `open E?` | `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#putIfAbsent(kotlin.Long,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/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/LongSparseArray#remove(kotlin.Long)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Removes the mapping from the specified `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#remove(kotlin.Long)`, 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/LongSparseArray#remove(kotlin.Long,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, value: E)` Remove an existing key from the array map only if it is currently mapped to `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#remove(kotlin.Long,kotlin.Any)`. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#removeAt(kotlin.Int)(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Removes the mapping at the specified `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#removeAt(kotlin.Int)`. | Cmn android |
| `open E?` | `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#replace(kotlin.Long,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, value: E)` Replace the mapping for `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#replace(kotlin.Long,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/LongSparseArray#replace(kotlin.Long,kotlin.Any,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, oldValue: E, newValue: E)` Replace the mapping for `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#replace(kotlin.Long,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/LongSparseArray#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 `index`th key-value mapping that this `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray` stores. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#size()()` Returns the number of key-value mappings that this `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray` currently stores. | Cmn android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#toString()()` Returns a string representation of the object. | Cmn android |
| `open E` | `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#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 `index`th key-value mapping that this `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray` stores. | Cmn 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/LongSparseArray<T>.https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#(androidx.collection.LongSparseArray).contains(kotlin.Long)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Returns true if the collection contains `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.LongSparseArray).contains(kotlin.Long)`. | 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/LongSparseArray<T>.https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#(androidx.collection.LongSparseArray).forEach(kotlin.Function2)(action: (key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/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.LongSparseArray).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/LongSparseArray<T>.https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#(androidx.collection.LongSparseArray).getOrDefault(kotlin.Long,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, defaultValue: T)` Return the value corresponding to `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.LongSparseArray).getOrDefault(kotlin.Long,kotlin.Any)`, or `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.LongSparseArray).getOrDefault(kotlin.Long,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/LongSparseArray<T>.https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#(androidx.collection.LongSparseArray).getOrElse(kotlin.Long,kotlin.Function0)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, defaultValue: () -> T)` Return the value corresponding to `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.LongSparseArray).getOrElse(kotlin.Long,kotlin.Function0)`, or from `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.LongSparseArray).getOrElse(kotlin.Long,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/LongSparseArray<T>.https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#(androidx.collection.LongSparseArray).isNotEmpty()()` Return true when the collection contains elements. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-long-iterator/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray<T>.https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#(androidx.collection.LongSparseArray).keyIterator()()` Return an iterator over the collection's keys. | Cmn |
| `operator https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray<T>` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray<T>.https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#(androidx.collection.LongSparseArray).plus(androidx.collection.LongSparseArray)(other: https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray<T>)` Creates a new collection by adding or replacing entries from `https://developer.android.com/reference/kotlin/androidx/collection/package-summary#(androidx.collection.LongSparseArray).plus(androidx.collection.LongSparseArray)`. | 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/LongSparseArray<T>.https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#(androidx.collection.LongSparseArray).set(kotlin.Long,kotlin.Any)(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/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/LongSparseArray<T>.https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#(androidx.collection.LongSparseArray).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/LongSparseArray<T>.https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#(androidx.collection.LongSparseArray).size()` Returns the number of key/value pairs in the collection. | Cmn |

## Public constructors

### LongSparseArray

Cmn android

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

Creates a new `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray` 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.

## 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: Long, 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 `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray`.

### clone

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

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

### containsKey

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

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

Returns `true` if the specified `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#containsKey(kotlin.Long)` 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 `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#containsValue(kotlin.Any)` is mapped from any key.

### delete

Cmn 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/LongSparseArray#delete(kotlin.Long))(key: Long): Unit
```

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

Removes the mapping from the specified `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#delete(kotlin.Long)`, if there was any.

### get

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

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

Gets the value mapped from the specified `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#get(kotlin.Long)`, 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: Long, defaultValue: E): E
```

Gets the value mapped from the specified `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#get(kotlin.Long,kotlin.Any)`, or `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#get(kotlin.Long,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: Long): Int
```

Returns the index for which `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#keyAt(kotlin.Int)` would return the specified key, or a negative number if the specified key 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/LongSparseArray#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/LongSparseArray#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.

### 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/LongSparseArray#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/LongSparseArray#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): Long
```

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

The keys corresponding to indices in ascending order are guaranteed to be in ascending order, e.g., `keyAt(0)` will return the smallest key and `keyAt(size()-1)` will return the largest key.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#keyAt(kotlin.Int)` is not in the range `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: Long, value: E): Unit
```

Adds a mapping from the specified key to the specified value, replacing the previous mapping from the specified key 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: LongSparseArray<E>): Unit
```

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

### putIfAbsent

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

```
open fun putIfAbsent(key: Long, 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/-long/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: Long): Unit
```

Removes the mapping from the specified `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#remove(kotlin.Long)`, if there was any.

### remove

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

```
open fun remove(key: Long, value: E): Boolean
```

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

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The key of the mapping to remove. |
| `value: E` | 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 `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray#removeAt(kotlin.Int)`.

### replace

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

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

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

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/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: Long, oldValue: E, newValue: E): Boolean
```

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

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/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 `index`th key-value mapping that this `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray` stores.

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

### 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 `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray` 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 `index`th key-value mapping that this `https://developer.android.com/reference/kotlin/androidx/collection/LongSparseArray` stores.

The values corresponding to indices in ascending order are guaranteed to be associated with keys in ascending order, e.g., `valueAt(0)` will return the value associated with the smallest key and `valueAt(size()-1)` will return the value associated with the largest key.

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

## Extension functions

### LongSparseArray.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/LongSparseArray.kt+function:contains)

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

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

### LongSparseArray.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/LongSparseArray.kt+function:forEach)

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

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

### LongSparseArray.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/LongSparseArray.kt+function:getOrDefault)

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

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

### LongSparseArray.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/LongSparseArray.kt+function:getOrElse)

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

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

### LongSparseArray.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/LongSparseArray.kt+function:isNotEmpty)

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

Return true when the collection contains elements.

### LongSparseArray.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/LongSparseArray.kt+function:keyIterator)

```
fun <T : Any?> LongSparseArray<T>.keyIterator(): LongIterator
```

Return an iterator over the collection's keys.

### LongSparseArray.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/LongSparseArray.kt+function:plus)

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

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

### LongSparseArray.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/LongSparseArray.kt+function:set)

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

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

### LongSparseArray.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/LongSparseArray.kt+function:valueIterator)

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

Return an iterator over the collection's values.

## Extension properties

### LongSparseArray.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/LongSparseArray.kt+symbol:size)

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

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