Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/util/SparseArray#pubctors) \| [Methods](https://developer.android.com/reference/android/util/SparseArray#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/util/SparseArray#inhmethods)

# SparseArray

*** ** * ** ***


`
public


class
SparseArray
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


implements

https://developer.android.com/reference/java/lang/Cloneable


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.util.SparseArray\<E\> |

<br />

*** ** * ** ***

`SparseArray` maps integers to Objects and, unlike a normal array of Objects,
its indices can contain gaps. `SparseArray` is intended to be more memory-efficient
than a
[`HashMap`](https://developer.android.com/reference/java/util/HashMap), 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
`HashMap` because 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 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 of all removed entries. This garbage collection
must be performed whenever the array needs to be grown, or when 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/android/util/SparseArray#keyAt(int)` and `https://developer.android.com/reference/android/util/SparseArray#valueAt(int)`. Iterating over the keys using
`keyAt(int)` with ascending values of the index returns the
keys in ascending order. In the case of `valueAt(int)`, the
values corresponding to the keys are returned in ascending order.

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/util/SparseArray#SparseArray()() ` Creates a new SparseArray containing no mappings. |
| ` https://developer.android.com/reference/android/util/SparseArray#SparseArray(int)(int initialCapacity) ` Creates a new SparseArray containing no mappings that will not require any additional memory allocation to store the specified number of mappings. |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/util/SparseArray#append(int,%20E)(int key, E value) ` Puts a key/value pair into the array, optimizing for the case where the key is greater than all existing keys in the array. |
| ` void` | ` https://developer.android.com/reference/android/util/SparseArray#clear()() ` Removes all key-value mappings from this SparseArray. |
| ` https://developer.android.com/reference/android/util/SparseArray<E>` | ` https://developer.android.com/reference/android/util/SparseArray#clone()() ` Creates and returns a copy of this object. |
| ` E` | ` https://developer.android.com/reference/android/util/SparseArray#compute(int,%20android.util.function.IntObjFunction<?%20super%20E,?%20extends%20E>)(int key, https://developer.android.com/reference/android/util/function/IntObjFunction<? super E, ? extends E> function) ` Attempts to compute a new mapped value for the specified key based on its current mapping (or null if there is no current mapping). |
| ` boolean` | ` https://developer.android.com/reference/android/util/SparseArray#contains(int)(int key) ` Returns true if the key exists in the array. |
| ` boolean` | ` https://developer.android.com/reference/android/util/SparseArray#contentEquals(android.util.SparseArray<?>)(https://developer.android.com/reference/android/util/SparseArray<?> other) ` Compares the contents of this `https://developer.android.com/reference/android/util/SparseArray` to the specified `https://developer.android.com/reference/android/util/SparseArray`. |
| ` int` | ` https://developer.android.com/reference/android/util/SparseArray#contentHashCode()() ` Returns a hash code value for the contents of this `https://developer.android.com/reference/android/util/SparseArray`, combining the `https://developer.android.com/reference/java/util/Objects#hashCode(java.lang.Object)` result of all its keys and values. |
| ` void` | ` https://developer.android.com/reference/android/util/SparseArray#delete(int)(int key) ` Removes the mapping from the specified key, if there was any. |
| ` E` | ` https://developer.android.com/reference/android/util/SparseArray#get(int)(int key) ` Gets the Object mapped from the specified key, or `null` if no such mapping has been made. |
| ` E` | ` https://developer.android.com/reference/android/util/SparseArray#get(int,%20E)(int key, E valueIfKeyNotFound) ` Gets the Object mapped from the specified key, or the specified Object if no such mapping has been made. |
| ` int` | ` https://developer.android.com/reference/android/util/SparseArray#indexOfKey(int)(int key) ` Returns the index for which `https://developer.android.com/reference/android/util/SparseArray#keyAt(int)` would return the specified key, or a negative number if the specified key is not mapped. |
| ` int` | ` https://developer.android.com/reference/android/util/SparseArray#indexOfValue(E)(E value) ` Returns an index for which `https://developer.android.com/reference/android/util/SparseArray#valueAt(int)` would return the specified value, or a negative number if no keys map to the specified value. |
| ` int` | ` https://developer.android.com/reference/android/util/SparseArray#keyAt(int)(int index) ` Given an index in the range `0...size()-1`, returns the key from the `index`th key-value mapping that this SparseArray stores. |
| ` void` | ` https://developer.android.com/reference/android/util/SparseArray#put(int,%20E)(int key, E value) ` Adds a mapping from the specified key to the specified value, replacing the previous mapping from the specified key if there was one. |
| ` void` | ` https://developer.android.com/reference/android/util/SparseArray#remove(int)(int key) ` Alias for `https://developer.android.com/reference/android/util/SparseArray#delete(int)`. |
| ` void` | ` https://developer.android.com/reference/android/util/SparseArray#removeAt(int)(int index) ` Removes the mapping at the specified index. |
| ` void` | ` https://developer.android.com/reference/android/util/SparseArray#removeAtRange(int,%20int)(int index, int size) ` Remove a range of mappings as a batch. |
| ` void` | ` https://developer.android.com/reference/android/util/SparseArray#set(int,%20E)(int key, E value) ` Alias for `https://developer.android.com/reference/android/util/SparseArray#put(int,%20E)` to support Kotlin \[index\]= operator. |
| ` void` | ` https://developer.android.com/reference/android/util/SparseArray#setValueAt(int,%20E)(int index, E value) ` Given an index in the range `0...size()-1`, sets a new value for the `index`th key-value mapping that this SparseArray stores. |
| ` int` | ` https://developer.android.com/reference/android/util/SparseArray#size()() ` Returns the number of key-value mappings that this SparseArray currently stores. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/util/SparseArray#toString()() ` Returns a string representation of the object. This implementation composes a string by iterating over its mappings. |
| ` E` | ` https://developer.android.com/reference/android/util/SparseArray#valueAt(int)(int index) ` Given an index in the range `0...size()-1`, returns the value from the `index`th key-value mapping that this SparseArray stores. |

### Inherited methods

From class `

https://developer.android.com/reference/java/lang/Object

` `


https://developer.android.com/reference/java/lang/Object` `
https://developer.android.com/reference/java/lang/Object#clone()()
`

Creates and returns a copy of this object.


`


boolean` `
https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj)
`

Indicates whether some other object is "equal to" this one.


`


void` `
https://developer.android.com/reference/java/lang/Object#finalize()()
`

Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.


`


final

https://developer.android.com/reference/java/lang/Class<?>` `
https://developer.android.com/reference/java/lang/Object#getClass()()
`

Returns the runtime class of this `Object`.


`


int` `
https://developer.android.com/reference/java/lang/Object#hashCode()()
`

Returns a hash code value for the object.


`


final

void` `
https://developer.android.com/reference/java/lang/Object#notify()()
`

Wakes up a single thread that is waiting on this object's
monitor.


`


final

void` `
https://developer.android.com/reference/java/lang/Object#notifyAll()()
`

Wakes up all threads that are waiting on this object's monitor.


`


https://developer.android.com/reference/java/lang/String` `
https://developer.android.com/reference/java/lang/Object#toString()()
`

Returns a string representation of the object.


`


final

void` `
https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos)
`

Causes the current thread to wait until it is awakened, typically
by being *notified* or *interrupted*, or until a
certain amount of real time has elapsed.


`


final

void` `
https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis)
`

Causes the current thread to wait until it is awakened, typically
by being *notified* or *interrupted*, or until a
certain amount of real time has elapsed.


`


final

void` `
https://developer.android.com/reference/java/lang/Object#wait()()
`

Causes the current thread to wait until it is awakened, typically
by being *notified* or *interrupted*.


## Public constructors

### SparseArray

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public SparseArray ()
```

Creates a new SparseArray containing no mappings.

<br />

### SparseArray

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public SparseArray (int initialCapacity)
```

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.

<br />

| Parameters ||
|---|---|
| `initialCapacity` | `int` <br /> |

## Public methods

### append

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void append (int key, 
                E value)
```

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

<br />

| Parameters ||
|---|---|
| `key` | `int` <br /> |
| `value` | `E` <br /> |

### clear

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void clear ()
```

Removes all key-value mappings from this SparseArray.

<br />

### clone

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public SparseArray<E> clone ()
```

Creates and returns a copy of this object. The precise meaning
of "copy" may depend on the class of the object. The general
intent is that, for any object `x`, the expression:
>
> ```
> x.clone() != x
> ```
will be true, and that the expression:
>
> ```
> x.clone().getClass() == x.getClass()
> ```
will be `true`, but these are not absolute requirements. While it is typically the case that:
>
> ```
> x.clone().equals(x)
> ```
will be `true`, this is not an absolute requirement.


By convention, the returned object should be obtained by calling
`super.clone`. If a class and all of its superclasses (except
`Object`) obey this convention, it will be the case that
`x.clone().getClass() == x.getClass()`.


By convention, the object returned by this method should be independent
of this object (which is being cloned). To achieve this independence,
it may be necessary to modify one or more fields of the object returned
by `super.clone` before returning it. Typically, this means
copying any mutable objects that comprise the internal "deep structure"
of the object being cloned and replacing the references to these
objects with references to the copies. If a class contains only
primitive fields or references to immutable objects, then it is usually
the case that no fields in the object returned by `super.clone`
need to be modified.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/util/SparseArray<E>` | a clone of this instance. <br /> |

### compute

Added in [version 37.2](https://developer.android.com/topic/libraries/support-library/revisions)

```
public E compute (int key, 
                IntObjFunction<? super E, ? extends E> function)
```

Attempts to compute a new mapped value for the specified key based on its current mapping (or
null if there is no current mapping). If the function returns null, the mapping is removed
(or remains absent if initially absent).

<br />

| Parameters ||
|---|---|
| `key` | `int`: The key to compute the value for. <br /> |
| `function` | `IntObjFunction`: The function to compute the new value. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `E` | The new mapped value, or null if none. <br /> |

### contains

Added in [API level 30](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean contains (int key)
```

Returns true if the key exists in the array. This is equivalent to
`https://developer.android.com/reference/android/util/SparseArray#indexOfKey(int)` \>= 0.

<br />

| Parameters ||
|---|---|
| `key` | `int`: Potential key in the mapping <br /> |

| Returns ||
|---|---|
| `boolean` | true if the key is defined in the mapping <br /> |

### contentEquals

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean contentEquals (SparseArray<?> other)
```

Compares the contents of this `https://developer.android.com/reference/android/util/SparseArray` to the specified `https://developer.android.com/reference/android/util/SparseArray`.

For backwards compatibility reasons, `https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)` cannot be implemented,
so this serves as a manually invoked alternative.

<br />

| Parameters ||
|---|---|
| `other` | `SparseArray`: This value may be `null`. <br /> |

| Returns ||
|---|---|
| `boolean` | <br /> |

### contentHashCode

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int contentHashCode ()
```

Returns a hash code value for the contents of this `https://developer.android.com/reference/android/util/SparseArray`, combining the
`https://developer.android.com/reference/java/util/Objects#hashCode(java.lang.Object)` result of all its keys and values.

For backwards compatibility, `https://developer.android.com/reference/java/lang/Object#hashCode()` cannot be implemented, so this serves
as a manually invoked alternative.

<br />

| Returns ||
|---|---|
| `int` | <br /> |

### delete

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void delete (int key)
```

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

<br />

| Parameters ||
|---|---|
| `key` | `int` <br /> |

### get

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public E get (int key)
```

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

<br />

| Parameters ||
|---|---|
| `key` | `int` <br /> |

| Returns ||
|---|---|
| `E` | <br /> |

### get

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public E get (int key, 
                E valueIfKeyNotFound)
```

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

<br />

| Parameters ||
|---|---|
| `key` | `int` <br /> |
| `valueIfKeyNotFound` | `E` <br /> |

| Returns ||
|---|---|
| `E` | <br /> |

### indexOfKey

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int indexOfKey (int key)
```

Returns the index for which `https://developer.android.com/reference/android/util/SparseArray#keyAt(int)` would return the
specified key, or a negative number if the specified
key is not mapped.

<br />

| Parameters ||
|---|---|
| `key` | `int` <br /> |

| Returns ||
|---|---|
| `int` | <br /> |

### indexOfValue

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int indexOfValue (E value)
```

Returns an index for which `https://developer.android.com/reference/android/util/SparseArray#valueAt(int)` would return the
specified value, or a negative number if no keys map to the
specified value.

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' `indexOf` methods,
this method compares values using `==` rather than `equals`.

<br />

| Parameters ||
|---|---|
| `value` | `E` <br /> |

| Returns ||
|---|---|
| `int` | <br /> |

### keyAt

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int keyAt (int index)
```

Given an index in the range `0...size()-1`, returns
the key from the `index`th key-value mapping that this
SparseArray 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.

For indices outside of the range `0...size()-1`,
the behavior is undefined for apps targeting `https://developer.android.com/reference/android/os/Build.VERSION_CODES#P` and
earlier, and an `https://developer.android.com/reference/java/lang/ArrayIndexOutOfBoundsException` is thrown for apps targeting
`https://developer.android.com/reference/android/os/Build.VERSION_CODES#Q` and later.

<br />

<br />

| Parameters ||
|---|---|
| `index` | `int` <br /> |

| Returns ||
|---|---|
| `int` | <br /> |

### put

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void put (int key, 
                E value)
```

Adds a mapping from the specified key to the specified value,
replacing the previous mapping from the specified key if there
was one.

<br />

| Parameters ||
|---|---|
| `key` | `int` <br /> |
| `value` | `E` <br /> |

### remove

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void remove (int key)
```

Alias for `https://developer.android.com/reference/android/util/SparseArray#delete(int)`.

<br />

| Parameters ||
|---|---|
| `key` | `int` <br /> |

### removeAt

Added in [API level 11](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void removeAt (int index)
```

Removes the mapping at the specified index.

For indices outside of the range `0...size()-1`,
the behavior is undefined for apps targeting `https://developer.android.com/reference/android/os/Build.VERSION_CODES#P` and
earlier, and an `https://developer.android.com/reference/java/lang/ArrayIndexOutOfBoundsException` is thrown for apps targeting
`https://developer.android.com/reference/android/os/Build.VERSION_CODES#Q` and later.

<br />

<br />

| Parameters ||
|---|---|
| `index` | `int` <br /> |

### removeAtRange

Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void removeAtRange (int index, 
                int size)
```

Remove a range of mappings as a batch.

<br />

| Parameters ||
|---|---|
| `index` | `int`: Index to begin at <br /> |
| `size` | `int`: Number of mappings to remove For indices outside of the range `0...size()-1`, the behavior is undefined. <br /> |

### set

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void set (int key, 
                E value)
```

Alias for `https://developer.android.com/reference/android/util/SparseArray#put(int,%20E)` to support Kotlin \[index\]= operator.

<br />

| Parameters ||
|---|---|
| `key` | `int` <br /> |
| `value` | `E` <br /> |

**See also:**

- `https://developer.android.com/reference/android/util/SparseArray#put(int,%20E)`

### setValueAt

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setValueAt (int index, 
                E value)
```

Given an index in the range `0...size()-1`, sets a new
value for the `index`th key-value mapping that this
SparseArray stores.

For indices outside of the range `0...size()-1`, the behavior is undefined for
apps targeting `https://developer.android.com/reference/android/os/Build.VERSION_CODES#P` and earlier, and an
`https://developer.android.com/reference/java/lang/ArrayIndexOutOfBoundsException` is thrown for apps targeting
`https://developer.android.com/reference/android/os/Build.VERSION_CODES#Q` and later.

<br />

<br />

| Parameters ||
|---|---|
| `index` | `int` <br /> |
| `value` | `E` <br /> |

### size

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int size ()
```

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

<br />

| Returns ||
|---|---|
| `int` | <br /> |

### toString

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public 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 value, the string "(this Map)"
will appear in its place.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a string representation of the object. <br /> |

### valueAt

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public E valueAt (int index)
```

Given an index in the range `0...size()-1`, returns
the value from the `index`th key-value mapping that this
SparseArray 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.

For indices outside of the range `0...size()-1`,
the behavior is undefined for apps targeting `https://developer.android.com/reference/android/os/Build.VERSION_CODES#P` and
earlier, and an `https://developer.android.com/reference/java/lang/ArrayIndexOutOfBoundsException` is thrown for apps targeting
`https://developer.android.com/reference/android/os/Build.VERSION_CODES#Q` and later.

<br />

<br />

| Parameters ||
|---|---|
| `index` | `int` <br /> |

| Returns ||
|---|---|
| `E` | <br /> |