Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

androidx.collection

Classes

ArrayMap

ArrayMap is a generic key->value mapping data structure that is designed to be more memory efficient than a traditional java.util.HashMap, this implementation is a version of the platform's android.util.ArrayMap that can be used on older versions of the platform.

ArraySet

ArraySet is a generic set data structure that is designed to be more memory efficient than a traditional java.util.HashSet.

CircularArray

CircularArray is a generic circular array data structure that provides O(1) random read, O(1) prepend and O(1) append.

CircularIntArray

CircularIntArray is a circular integer array data structure that provides O(1) random read, O(1) prepend and O(1) append.

LongSparseArray

SparseArray mapping longs to Objects, a version of the platform's android.util.LongSparseArray that can be used on older versions of the platform.

LruCache

Static library version of android.util.LruCache.

SimpleArrayMap

Base implementation of ArrayMap that doesn't include any standard Java container API interoperability.

SparseArrayCompat

SparseArrays map integers to Objects.

Top-level functions summary

ArrayMap<K, V>

Returns an empty new ArrayMap.

ArrayMap<K, V>
arrayMapOf(vararg pairs: Pair<K, V>)

Returns a new ArrayMap with the specified contents, given as a list of pairs where the first component is the key and the second component is the value.

ArraySet<T>

Returns an empty new ArraySet.

ArraySet<T>
arraySetOf(vararg values: T)

Returns a new ArraySet with the specified contents.

LruCache<K, V>
lruCache(maxSize: Int, crossinline sizeOf: (key: K, value: V) -> Int = { _, _ -> 1 }, crossinline create: (key: K) -> V? = { null as V? }, crossinline onEntryRemoved: (evicted: Boolean, key: K, oldValue: V, newValue: V?) -> Unit = { _, _, _, _ -> })

Creates an LruCache with the given parameters.

Extension functions summary

For LongSparseArray
operator Boolean

Returns true if the collection contains key.

operator Unit
LongSparseArray<T>.set(key: Long, value: T)

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

operator LongSparseArray<T>

Creates a new collection by adding or replacing entries from other.

Boolean

Returns true if the collection contains key.

Boolean

Returns true if the collection contains value.

T
LongSparseArray<T>.getOrDefault(key: Long, defaultValue: T)

Return the value corresponding to key, or defaultValue when not present.

T
LongSparseArray<T>.getOrElse(key: Long, defaultValue: () -> T)

Return the value corresponding to key, or from defaultValue when not present.

Boolean

Return true when the collection contains elements.

Boolean
LongSparseArray<T>.remove(key: Long, value: T)

Removes the entry for key only if it is mapped to value.

Unit

Update this collection by adding or replacing entries from other.

Unit
LongSparseArray<T>.forEach(action: (key: Long, value: T) -> Unit)

Performs the given action for each key/value entry.

LongIterator

Return an iterator over the collection's keys.

Iterator<T>

Return an iterator over the collection's values.

For SparseArrayCompat
operator Boolean

Returns true if the collection contains key.

operator Unit
SparseArrayCompat<T>.set(key: Int, value: T)

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

operator SparseArrayCompat<T>

Creates a new collection by adding or replacing entries from other.

Boolean

Returns true if the collection contains key.

Boolean

Returns true if the collection contains value.

T
SparseArrayCompat<T>.getOrDefault(key: Int, defaultValue: T)

Return the value corresponding to key, or defaultValue when not present.

T
SparseArrayCompat<T>.getOrElse(key: Int, defaultValue: () -> T)

Return the value corresponding to key, or from defaultValue when not present.

Boolean

Return true when the collection contains elements.

Boolean
SparseArrayCompat<T>.remove(key: Int, value: T)

Removes the entry for key only if it is mapped to value.

Unit

Update this collection by adding or replacing entries from other.

Unit
SparseArrayCompat<T>.forEach(action: (key: Int, value: T) -> Unit)

Performs the given action for each key/value entry.

IntIterator

Return an iterator over the collection's keys.

Iterator<T>

Return an iterator over the collection's values.

Extension properties summary

For LongSparseArray
Int

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

For SparseArrayCompat
Int

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

Top-level functions

arrayMapOf

inline fun <K, V> arrayMapOf(): ArrayMap<K, V>

Returns an empty new ArrayMap.

arrayMapOf

fun <K, V> arrayMapOf(vararg pairs: Pair<K, V>): ArrayMap<K, V>

Returns a new ArrayMap with the specified contents, given as a list of pairs where the first component is the key and the second component is the value.

If multiple pairs have the same key, the resulting map will contain the value from the last of those pairs.

arraySetOf

inline fun <T> arraySetOf(): ArraySet<T>

Returns an empty new ArraySet.

arraySetOf

fun <T> arraySetOf(vararg values: T): ArraySet<T>

Returns a new ArraySet with the specified contents.

lruCache

inline fun <K : Any, V : Any> lruCache(maxSize: Int, crossinline sizeOf: (key: K, value: V) -> Int = { _, _ -> 1 }, crossinline create: (key: K) -> V? = { null as V? }, crossinline onEntryRemoved: (evicted: Boolean, key: K, oldValue: V, newValue: V?) -> Unit = { _, _, _, _ -> }): LruCache<K, V>

Creates an LruCache with the given parameters.

Parameters
maxSize for caches that do not specify sizeOf, this is the maximum number of entries in the cache. For all other caches, this is the maximum sum of the sizes of the entries in this cache.
sizeOf function that returns the size of the entry for key and value in user-defined units. The default implementation returns 1.
create a create called after a cache miss to compute a value for the corresponding key. Returns the computed value or null if no value can be computed. The default implementation returns null.
onEntryRemoved a function called for entries that have been evicted or removed.

Extension functions

contains

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

Returns true if the collection contains key.

contains

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

Returns true if the collection contains key.

containsKey

inline fun <T> LongSparseArray<T>.containsKey(key: Long): Boolean

Returns true if the collection contains key.

containsKey

inline fun <T> SparseArrayCompat<T>.containsKey(key: Int): Boolean

Returns true if the collection contains key.

containsValue

inline fun <T> LongSparseArray<T>.containsValue(value: T): Boolean

Returns true if the collection contains value.

containsValue

inline fun <T> SparseArrayCompat<T>.containsValue(value: T): Boolean

Returns true if the collection contains value.

forEach

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

Performs the given action for each key/value entry.

forEach

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

Performs the given action for each key/value entry.

getOrDefault

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

Return the value corresponding to key, or defaultValue when not present.

getOrDefault

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

Return the value corresponding to key, or defaultValue when not present.

getOrElse

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

Return the value corresponding to key, or from defaultValue when not present.

getOrElse

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

Return the value corresponding to key, or from defaultValue when not present.

isNotEmpty

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

Return true when the collection contains elements.

isNotEmpty

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

Return true when the collection contains elements.

keyIterator

fun <T> LongSparseArray<T>.keyIterator(): LongIterator

Return an iterator over the collection's keys.

keyIterator

fun <T> SparseArrayCompat<T>.keyIterator(): IntIterator

Return an iterator over the collection's keys.

plus

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

Creates a new collection by adding or replacing entries from other.

plus

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

Creates a new collection by adding or replacing entries from other.

putAll

fun <T> LongSparseArray<T>.putAll(other: LongSparseArray<T>): Unit

Update this collection by adding or replacing entries from other.

putAll

fun <T> SparseArrayCompat<T>.putAll(other: SparseArrayCompat<T>): Unit

Update this collection by adding or replacing entries from other.

remove

fun <T> LongSparseArray<T>.remove(key: Long, value: T): Boolean

Removes the entry for key only if it is mapped to value.

remove

fun <T> SparseArrayCompat<T>.remove(key: Int, value: T): Boolean

Removes the entry for key only if it is mapped to value.

set

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

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

set

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

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

valueIterator

fun <T> LongSparseArray<T>.valueIterator(): Iterator<T>

Return an iterator over the collection's values.

valueIterator

fun <T> SparseArrayCompat<T>.valueIterator(): Iterator<T>

Return an iterator over the collection's values.

Extension properties

size

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

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

size

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

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