# SparseArrayCompat

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  
Summary:[Ctors](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#pubctors)\|[Methods](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#)  

# SparseArrayCompat

| This package is part of the Android[support library](https://developer.android.com/topic/libraries/support-library)which is no longer maintained. The support library has been superseded by[AndroidX](https://developer.android.com/jetpack/androidx)which is part of[Jetpack](https://developer.android.com/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider[migrating](https://developer.android.com/jetpack/androidx/migrate)existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library[class mappings](https://developer.android.com/jetpack/androidx/migrate/class-mappings).

`
public


class
SparseArrayCompat
`  
`

extends Object


``


implements

Cloneable


`

|---|------------------------------------------------|
| java.lang.Object                                  ||
| ↳ | android.support.v4.util.SparseArrayCompat\<E\> |

<br />

*** ** * ** ***

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 the map size or entry values are retrieved.

It is possible to iterate over the items in this container using[keyAt(int)](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#keyAt(int))and[valueAt(int)](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#valueAt(int)). Iterating over the keys using`keyAt(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`valueAt(int)`.

<br />

## Summary

|                                                                                                                                            ### Public constructors                                                                                                                                             ||
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[SparseArrayCompat](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#SparseArrayCompat())`() ` Creates a new SparseArray containing no mappings.                                                                                                                        |
| ` `[SparseArrayCompat](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#SparseArrayCompat(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`                                                                                                        | ` `[append](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#append(int, E))`(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`                                                                                                        | ` `[clear](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#clear())`() ` Removes all key-value mappings from this SparseArray.                                                                                                                                                                                                     |
| ` `[SparseArrayCompat](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat)`<E>` | ` `[clone](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#clone())`() `                                                                                                                                                                                                                                                           |
| ` void`                                                                                                        | ` `[delete](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#delete(int))`(int key) ` Removes the mapping from the specified key, if there was any.                                                                                                                                                                                 |
| ` E`                                                                                                           | ` `[get](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#get(int))`(int key) ` Gets the Object mapped from the specified key, or`null`if no such mapping has been made.                                                                                                                                                            |
| ` E`                                                                                                           | ` `[get](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#get(int, E))`(int key, E valueIfKeyNotFound) ` Gets the Object mapped from the specified key, or the specified Object if no such mapping has been made.                                                                                                                   |
| ` int`                                                                                                         | ` `[indexOfKey](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#indexOfKey(int))`(int key) ` Returns the index for which[keyAt(int)](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#keyAt(int))would return the specified key, or a negative number if the specified key is not mapped.         |
| ` int`                                                                                                         | ` `[indexOfValue](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#indexOfValue(E))`(E value) ` Returns an index for which[valueAt(int)](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#valueAt(int))would return the specified key, or a negative number if no keys map to the specified value. |
| ` boolean`                                                                                                     | ` `[isEmpty](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#isEmpty())`() ` Return true if size() is 0.                                                                                                                                                                                                                           |
| ` int`                                                                                                         | ` `[keyAt](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#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`                                                                                                        | ` `[put](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#put(int, E))`(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`                                                                                                        | ` `[remove](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#remove(int))`(int key) ` Alias for[delete(int)](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#delete(int)).                                                                                                                        |
| ` void`                                                                                                        | ` `[removeAt](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#removeAt(int))`(int index) ` Removes the mapping at the specified index.                                                                                                                                                                                             |
| ` void`                                                                                                        | ` `[removeAtRange](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#removeAtRange(int, int))`(int index, int size) ` Remove a range of mappings as a batch.                                                                                                                                                                         |
| ` void`                                                                                                        | ` `[setValueAt](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#setValueAt(int, E))`(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`                                                                                                         | ` `[size](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#size())`() ` Returns the number of key-value mappings that this SparseArray currently stores.                                                                                                                                                                            |
| ` String`                                                                                                      | ` `[toString](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#toString())`() ` <br /> This implementation composes a string by iterating over its mappings.                                                                                                                                                                        |
| ` E`                                                                                                           | ` `[valueAt](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#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 |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#)From class` java.lang.Object ` |-------------------|-------------------------------| | ` Object`         | ` clone() `                   | | ` boolean`        | ` equals(Object arg0) `       | | ` void`           | ` finalize() `                | | ` final Class<?>` | ` getClass() `                | | ` int`            | ` hashCode() `                | | ` final void`     | ` notify() `                  | | ` final void`     | ` notifyAll() `               | | ` String`         | ` toString() `                | | ` final void`     | ` wait(long arg0, int arg1) ` | | ` final void`     | ` wait(long arg0) `           | | ` final void`     | ` wait() `                    | ||

## Public constructors

### SparseArrayCompat

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

```
SparseArrayCompat ()
```

Creates a new SparseArray containing no mappings.

<br />

### SparseArrayCompat

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

```
SparseArrayCompat (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[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
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[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void clear ()
```

Removes all key-value mappings from this SparseArray.

<br />

### clone

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

```
SparseArrayCompat<E> clone ()
```

<br />

<br />

|                                                       Returns                                                       ||
|-------------------------------------------------------------------------------------------------------------|--------|
| [SparseArrayCompat](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat)`<E>` | <br /> |

### delete

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

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

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

<br />

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

### get

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

```
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[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
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[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Returns the index for which[keyAt(int)](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#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[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Returns an index for which[valueAt(int)](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#valueAt(int))would return the specified key, 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 /> |

### isEmpty

```
boolean isEmpty ()
```

Return true if size() is 0.

<br />

|                Returns                 ||
|-----------|-----------------------------|
| `boolean` | true if size() is 0. <br /> |

### keyAt

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

```
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.

<br />

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

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

### put

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

```
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[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Alias for[delete(int)](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat#delete(int)).

<br />

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

### removeAt

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

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

Removes the mapping at the specified index.

<br />

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

### removeAtRange

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

```
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 <br /> |

### setValueAt

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

```
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.

<br />

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

### size

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

```
int size ()
```

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

<br />

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

### toString

```
String toString ()
```

<br />

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      ||
|----------|--------|
| `String` | <br /> |

### valueAt

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

```
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.

<br />

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

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

-

  ## Interfaces

  - [Pools.Pool](https://developer.android.com/reference/android/support/v4/util/Pools.Pool)
-

  ## Classes

  - [ArrayMap](https://developer.android.com/reference/android/support/v4/util/ArrayMap)
  - [ArraySet](https://developer.android.com/reference/android/support/v4/util/ArraySet)
  - [AtomicFile](https://developer.android.com/reference/android/support/v4/util/AtomicFile)
  - [CircularArray](https://developer.android.com/reference/android/support/v4/util/CircularArray)
  - [CircularIntArray](https://developer.android.com/reference/android/support/v4/util/CircularIntArray)
  - [LongSparseArray](https://developer.android.com/reference/android/support/v4/util/LongSparseArray)
  - [LruCache](https://developer.android.com/reference/android/support/v4/util/LruCache)
  - [ObjectsCompat](https://developer.android.com/reference/android/support/v4/util/ObjectsCompat)
  - [Pair](https://developer.android.com/reference/android/support/v4/util/Pair)
  - [PatternsCompat](https://developer.android.com/reference/android/support/v4/util/PatternsCompat)
  - [Pools](https://developer.android.com/reference/android/support/v4/util/Pools)
  - [Pools.SimplePool](https://developer.android.com/reference/android/support/v4/util/Pools.SimplePool)
  - [Pools.SynchronizedPool](https://developer.android.com/reference/android/support/v4/util/Pools.SynchronizedPool)
  - [SimpleArrayMap](https://developer.android.com/reference/android/support/v4/util/SimpleArrayMap)
  - [SparseArrayCompat](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat)