Collection

  
Reduce the memory impact of existing and new collections that are small.
Latest Update Stable Release Release Candidate Beta Release Alpha Release
June 29, 2022 1.2.0 - - 1.3.0-alpha01

Declaring dependencies

To add a dependency on Collection, you must add the Google Maven repository to your project. Read Google's Maven repository for more information.

Add the dependencies for the artifacts you need in the build.gradle file for your app or module:

Groovy

dependencies {
    def collection_version = "1.2.0"

    // Java language implementation
    implementation "androidx.collection:collection:$collection_version"
    // Kotlin
    implementation "androidx.collection:collection-ktx:$collection_version"
}

Kotlin

dependencies {
    val collection_version = "1.2.0"

    // Java language implementation
    implementation("androidx.collection:collection:$collection_version")
    // Kotlin
    implementation("androidx.collection:collection-ktx:$collection_version")
}

For more information about dependencies, see Add Build Dependencies.

Feedback

Your feedback helps make Jetpack better. Let us know if you discover new issues or have ideas for improving this library. Please take a look at the existing issues in this library before you create a new one. You can add your vote to an existing issue by clicking the star button.

Create a new issue

See the Issue Tracker documentation for more information.

Version 1.3.0

Version 1.3.0-alpha01

June 29, 2022

androidx.collection:collection:1.3.0-alpha01 and androidx.collection:collection-ktx:1.3.0-alpha01 are released. Version 1.3.0-alpha01 contains these commits.

New Features

  • collection-ktx extensions have been migrated into the main collections artifact, androidx.collection:collection. This change makes the pre-existing -ktx extensions accessible to Kotlin users without requiring the -ktx dependency, while still maintaining compatibility for existing users. Maintaining these extensions in a separate -ktx artifact is no longer beneficial since the main artifact has moved to Kotlin. (I6eef2)

API Changes

  • ArraySet now extends AbstractMutableCollection and no longer directly implements the Collection interface. (If6da0, b/230860589)
  • Converted ArraySet to Kotlin. Due to stricter typing, some Kotlin calls may no longer compile. (Id68c1, b/230860589)
    • The following calls have the argument type T, not T?:
      • ArraySet<T>.contains(null)
      • ArraySet<T>.add(null)
      • ArraySet<T>.remove(null)
    • The following calls have the return type T?, not T!:
      • ArraySet<T?>.valueAt(n)
      • ArraySet<T?>.removeAt(n)
      • ArraySet<T?>().iterator().next()
    • The following calls are no longer possible from Kotlin:
      • set.toArray() - use set.toTypedArray()
      • set.toArray(array) - use set.forEachIndexed(array::set)
  • Combine overloaded constructor for SparseArrayCompat as an optional argument for Kotlin users. (If8407, b/227474719)
  • Remove operator syntax for SparseArrayCompat.get(key, defaultValue). Note this still allows operator syntax for .get(key), but better aligns the API surface of SparseArrayCompat with the other classes in this library. (I9a38d)
  • Migrate LongSparseArray extensions into the main artifact, androidx.collection:collection. This change makes the pre-existing -ktx extensions accessible to Kotlin users without requiring the -ktx dependency, while still maintaining compatibility for existing users. Maintaining these extensions in a separate -ktx artifact is no longer beneficial since the main artifact has moved to Kotlin. (I8659a)
  • Convert LongSparseArray to Kotlin. This change adds explicit nullity to its types, which is a binary compatible change, but may cause source incompatibilities. In particular: * .isEmpty is no longer accessible as a property, it must be accessed as a function call in Kotlin - .isEmpty() (Idfd0f)
  • Convert SimpleArrayMap to Kotlin. This change introduces a few incompatible changes, as a result of Java-Kotlin interop and the ability to correctly define nullity of types in the source.
    • The package private APIs, .mSize, .mArray, .mHashes, .indexOf(), .indexOfNull(), and .indexOfValue(), were made private - this is technically a binary incompatible change, but reflects the intended visibility of these fields and is the closest we can achieve in Kotlin since it does not include a way to specify package-private visibility.
    • The nullity of some types are now properly defined, the affected methods are: .getOrDefault, .keyAt, .valueAt, .setValueAt, .put, .putIfAbsent, .removeAt, .replace.
    • For Kotlin users, .isEmpty() is now only available as a function instead of also through property access. (I271b7, b/182813986)
  • Convert CircularArray to Kotlin. Notable changes:

    • Corrects the nullity of its generics to be non-null, which was previously impossible to declare, but was enforced in all of its public APIs.
    • Due to Kotlin's Java interop, it was previously possible to access certain methods as both properties or functions. However, to reduce duplication while maintaining binary compatibility, it was necessary to remove these, which is a source-incompatible change. The affectedn calls are: .isEmpty, .getLast() and .getFirst(). (Ifffac)
  • Convert CircularIntArray to Kotlin. Due to Kotlin's Java interop, it was previously possible to access certain methods as both properties or functions. However, to reduce duplication while maintaining binary compatibility, it was necessary to remove these, which is a source-incompatible change. The affected calls are: .isEmpty, .getLast() and .getFirst(). (Ie36ce)

  • Convert LruCache to Kotlin (Ia2f19)

  • Convert SparseArrayCompat to Kotlin. (If6fe8)

    A small behavior change is added to SparseArrayCompat.valueAt(), which no longer incorrectly returns null for un-initialized calls out of bounds of size() but within the provided initial capacity.

    This change also introduces the correct nullity for some types which was previously missing.

    • .get(): E? -> .get(): E
    • .replace(Int, Int?): Int? -> .replace(Int, Int): Int?
    • .replace(Int, Int?, Int?): Boolean -> .replace(Int, Int, Int): Boolean
    • .put(Int, Int?) -> .put(Int, Int)
    • .putIfAbsent(Int, Int?): Int? -> .putIfAbsent(Int, Int): Int?
    • .setValueAt(index: Int, value: Int?) -> .setValueAt(index: Int, value: Int)
    • .indexOfValue(value: Int?): Int -> .indexOfValue(value: Int): Int
    • .containsValue(value: Int?): Boolean -> .containsValue(value: Int): Boolean
    • .append(key: Int, value: Int?) -> .append(key: Int, value: Int)

Version 1.2.0

Version 1.2.0

December 1, 2021

androidx.collection:collection:1.2.0 and androidx.collection:collection-ktx:1.2.0 are released. Version 1.2.0 contains these commits.

Important changes since 1.1.0

  • Add an array constructor to ArraySet. (Id7f19)
  • Make entrySet() API-compliant by implementing entrySet().toArray(), remove(), removeAll(), and retainAll(), and removing implementation of entrySet().addAll() (I5d505)

Version 1.2.0-rc01

November 17, 2021

androidx.collection:collection:1.2.0-rc01 and androidx.collection:collection-ktx:1.2.0-rc01 are released. Version 1.2.0-rc01 contains these commits.

API Changes

  • No changes from previous beta.

Version 1.2.0-beta01

October 13, 2021

androidx.collection:collection:1.2.0-beta01 and androidx.collection:collection-ktx:1.2.0-beta01 are released. Version 1.2.0-beta01 contains these commits.

Bug Fixes

  • Make entrySet() API-compliant by implementing entrySet().toArray(), remove(), removeAll(), and retainAll(), and removing implementation of entrySet().addAll() (I5d505)

Version 1.2.0-alpha01

December 16, 2020

androidx.collection:collection:1.2.0-alpha01 and androidx.collection:collection-ktx:1.2.0-alpha01 are released. Version 1.2.0-alpha01 contains these commits.

API Changes

  • Add an array constructor to ArraySet. (Id7f19)

Version 1.1.0

Version 1.1.0

June 5, 2019

androidx.collection:collection:1.1.0 and androidx.collection:collection-ktx:1.1.0 are released. The commits included 1.1.0 can be found here.

Below is a summary of changes from 1.0.0 to 1.1.0:

New features

  • Use more efficient implementation for contains and isNotEmpty functions in 'collection-ktx' artifact. (aosp/866529)

API changes

  • Add putIfAbsent to LongSparseArray, SimpleArrayMap, and SparseArrayCompat (aosp/772608)
  • Add getOrDefault to SimpleArrayMap (aosp/772607)
  • Add two-argument remove to LongSparseArray, SimpleArrayMap, and SparseArrayCompat. Deprecate corresponding KTX extensions for this functionality. (aosp/772482)
  • Add two-argument replace to LongSparseArray, SimpleArrayMap, and SparseArrayCompat (aosp/772483)
  • Add three-argument replace to LongSparseArray, SimpleArrayMap, and SparseArrayCompat (aosp/772484)
  • Deprecate redundant delete methods. The remove method offers the same API and functionality and matches the API used by non-specialized Maps. (aosp/866053)

Bug fixes

  • Change SimpleArrayMap to synchronize its internal global cache of arrays on SimpleArrayMap.class instead of ArrayMap.class. This ensures that if you are only using SimpleArrayMap, ArrayMap can be removed by code shrinking tools. (aosp/934557)

Version 1.1.0-rc01

May 7, 2019

androidx.collection:collection:1.1.0-rc01 and androidx.collection:collection-ktx:1.1.0-rc01 are released. The commits included in this version can be found here.

New features

  • Use more efficient implementation for contains and isNotEmpty functions in 'collection-ktx' artifact. (aosp/866529)

Version 1.1.0-beta01

April 3, 2019

androidx.collection:collection:1.1.0-beta01 and androidx.collection:collection-ktx:1.1.0-beta01 are released. The commits included in this version can be found here.

API changes

  • The method mentioned in the alpha03 release notes as having its @RestrictTo value changed has been removed. It was unused by any AndroidX library (now or historically) and it did not offer any functionality that was missing from the public API.

Bug fixes

  • Change SimpleArrayMap to synchronize its internal global cache of arrays on SimpleArrayMap.class instead of ArrayMap.class. This ensures that if you are only using SimpleArrayMap, ArrayMap can be removed by code shrinking tools. (aosp/934557)

Version 1.1.0-alpha03

March 13, 2019

androidx.collection:collection:1.0.0-alpha03 and androidx.collection:collection-ktx:1.0.0-alpha03 are released. The full list of commits included in this version can be found here.

API changes

  • A method on ArraySet previously marked @RestrictTo(LIBRARY_GROUP) was changed to @RestrictTo(LIBRARY_GROUP_PREFIX). This is to support historical use by other AndroidX libraries which are now in different Maven group IDs. This method will either be made public or removed in 1.1.0-alpha04 because other AndroidX libraries should not get special APIs that other developers do not.

Version 1.1.0-alpha02

January 30, 2019

androidx.collection:collection 1.1.0-alpha02 and androidx.collection:collection-ktx 1.1.0-alpha02 are released.

API changes

  • Deprecate redundant delete methods. The remove methods offers the same API and functionality and matches the API used by non-specialized Maps. (aosp/866053)

Version 1.1.0-alpha01

December 3, 2018

API changes

  • Add putIfAbsent to LongSparseArray, SimpleArrayMap, and SparseArrayCompat (aosp/772608)
  • Add getOrDefault to SimpleArrayMap (aosp/772607)
  • Add two-argument remove to LongSparseArray, SimpleArrayMap, and SparseArrayCompat. Deprecate corresponding KTX extensions for this functionality. (aosp/772482)
  • Add two-argument replace to LongSparseArray, SimpleArrayMap, and SparseArrayCompat (aosp/772483)
  • Add three-argument replace to LongSparseArray, SimpleArrayMap, and SparseArrayCompat (aosp/772484)