# LongSparseArray

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/LongSparseArray#pubctors)\|[Methods](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#)  

# LongSparseArray

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

extends Object


``


implements

Cloneable


`

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

<br />

*** ** * ** ***

SparseArray mapping longs to Objects, a version of the platform's`android.util.LongSparseArray`that can be used on older versions of the platform. 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 the map size or entry values are retrieved.

<br />

## Summary

|                                                                                                                                           ### Public constructors                                                                                                                                            ||
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[LongSparseArray](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#LongSparseArray())`() ` Creates a new LongSparseArray containing no mappings.                                                                                                                        |
| ` `[LongSparseArray](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#LongSparseArray(int))`(int initialCapacity) ` Creates a new LongSparseArray 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/LongSparseArray#append(long, E))`(long 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/LongSparseArray#clear())`() ` Removes all key-value mappings from this LongSparseArray.                                                                                                                                                                                               |
| ` `[LongSparseArray](https://developer.android.com/reference/android/support/v4/util/LongSparseArray)`<E>` | ` `[clone](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#clone())`() `                                                                                                                                                                                                                                                         |
| ` void`                                                                                                    | ` `[delete](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#delete(long))`(long key) ` Removes the mapping from the specified key, if there was any.                                                                                                                                                                             |
| ` E`                                                                                                       | ` `[get](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#get(long))`(long 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/LongSparseArray#get(long, E))`(long 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/LongSparseArray#indexOfKey(long))`(long key) ` Returns the index for which[keyAt(int)](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#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/LongSparseArray#indexOfValue(E))`(E value) ` Returns an index for which[valueAt(int)](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#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/LongSparseArray#isEmpty())`() ` Return true if size() is 0.                                                                                                                                                                                                                         |
| ` long`                                                                                                    | ` `[keyAt](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#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 LongSparseArray stores.                                                                                                            |
| ` void`                                                                                                    | ` `[put](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#put(long, E))`(long 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/LongSparseArray#remove(long))`(long key) ` Alias for[delete(long)](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#delete(long)).                                                                                                                    |
| ` void`                                                                                                    | ` `[removeAt](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#removeAt(int))`(int index) ` Removes the mapping at the specified index.                                                                                                                                                                                           |
| ` void`                                                                                                    | ` `[setValueAt](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#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 LongSparseArray stores.                                                                                      |
| ` int`                                                                                                     | ` `[size](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#size())`() ` Returns the number of key-value mappings that this LongSparseArray currently stores.                                                                                                                                                                      |
| ` String`                                                                                                  | ` `[toString](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#toString())`() ` <br /> This implementation composes a string by iterating over its mappings.                                                                                                                                                                      |
| ` E`                                                                                                       | ` `[valueAt](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#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 LongSparseArray stores.                                                                                                      |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v4/util/LongSparseArray#)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

### LongSparseArray

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

```
LongSparseArray ()
```

Creates a new LongSparseArray containing no mappings.

<br />

### LongSparseArray

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

```
LongSparseArray (int initialCapacity)
```

Creates a new 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.

<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 (long 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`   | `long` <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 LongSparseArray.

<br />

### clone

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

```
LongSparseArray<E> clone ()
```

<br />

<br />

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

### delete

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

```
void delete (long key)
```

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

<br />

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

### get

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

```
E get (long key)
```

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

<br />

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

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

### get

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

```
E get (long 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`                | `long` <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 (long key)
```

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

<br />

|      Parameters      ||
|-------|---------------|
| `key` | `long` <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/LongSparseArray#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.

<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)  

```
long keyAt (int index)
```

Given an index in the range`0...size()-1`, returns the key from the`index`th key-value mapping that this LongSparseArray stores.

<br />

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

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

### put

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

```
void put (long 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`   | `long` <br /> |
| `value` | `E` <br />    |

### remove

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

```
void remove (long key)
```

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

<br />

|      Parameters      ||
|-------|---------------|
| `key` | `long` <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 /> |

### 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 LongSparseArray 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 LongSparseArray 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 LongSparseArray 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)