Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

ArraySet

class ArraySet<E : Any!> : MutableCollection<E>, MutableSet<E>
kotlin.Any
   ↳ androidx.collection.ArraySet

ArraySet is a generic set data structure that is designed to be more memory efficient than a traditional java.util.HashSet. The design is very similar to ArrayMap, with all of the caveats described there. This implementation is separate from ArrayMap, however, so the Object array contains only one item for each entry in the set (instead of a pair for a mapping).

Note that this implementation is not intended to be appropriate for data structures that may contain large numbers of items. It is generally slower than a traditional HashSet, 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%.

Because this container is intended to better balance memory use, unlike most other standard Java containers it will shrink its array as items are removed from it. Currently you have no control over this shrinking -- if you set a capacity and then remove an item, it may reduce the capacity to better match the current size. In the future an explicit call to set the capacity should turn off this aggressive shrinking behavior.

Summary

Public constructors

Create a new empty ArraySet.

<init>(capacity: Int)

Create a new ArraySet with a given initial capacity.

<init>(@Nullable set: ArraySet<E>?)

Create a new ArraySet with the mappings from the given ArraySet.

<init>(@Nullable set: MutableCollection<E>?)

Create a new ArraySet with the mappings from the given Collection.

Public methods

Boolean
add(@Nullable element: E?)

Adds the specified object to this set.

Unit
addAll(@NonNull array: ArraySet<out E>)

Perform a add(Object) of all values in array

Boolean
addAll(@NonNull elements: Collection<E>)

Perform an add(Object) of all values in collection

Unit

Make the array map empty.

Boolean
contains(@Nullable element: E?)

Check whether a value exists in the set.

Boolean
containsAll(@NonNull elements: Collection<E>)

Determine if the array set contains all of the values in the given collection.

Unit
ensureCapacity(minimumCapacity: Int)

Ensure the array map can hold at least minimumCapacity items.

Boolean
equals(other: Any?)

This implementation returns false if the object is not a set, or if the sets have different sizes.

Int

Int
indexOf(@Nullable key: Any?)

Returns the index of a value in the set.

Boolean

Return true if the array map contains no items.

MutableIterator<E>

Return an java.util.Iterator over all values in the set.

Boolean
remove(@Nullable element: E?)

Removes the specified object from this set.

Boolean
removeAll(@NonNull array: ArraySet<out E>)

Perform a remove(Object) of all values in array

Boolean
removeAll(@NonNull elements: Collection<E>)

Remove all values in the array set that exist in the given collection.

E
removeAt(index: Int)

Remove the key/value mapping at the given index.

Boolean
retainAll(@NonNull elements: Collection<E>)

Remove all values in the array set that do not exist in the given collection.

Array<Any!>

Array<T>
toArray(@NonNull array: Array<T>)

String

This implementation composes a string by iterating over its values.

E?
valueAt(index: Int)

Return the value at the given index in the array.

Properties

Int

Return the number of items in this array map.

Public constructors

<init>

ArraySet()

Create a new empty ArraySet. The default capacity of an array map is 0, and will grow once items are added to it.

<init>

ArraySet(capacity: Int)

Create a new ArraySet with a given initial capacity.

<init>

ArraySet(@Nullable set: ArraySet<E>?)

Create a new ArraySet with the mappings from the given ArraySet.

<init>

ArraySet(@Nullable set: MutableCollection<E>?)

Create a new ArraySet with the mappings from the given Collection.

Public methods

add

fun add(@Nullable element: E?): Boolean

Adds the specified object to this set. The set is not modified if it already contains the object.

Parameters
value E?: the object to add.
Return
Boolean: true if this set is modified, false otherwise.
Exceptions
ClassCastException when the class of the object is inappropriate for this set.

addAll

fun addAll(@NonNull array: ArraySet<out E>): Unit

Perform a add(Object) of all values in array

Parameters
array ArraySet<out E>: The array whose contents are to be retrieved.

addAll

fun addAll(@NonNull elements: Collection<E>): Boolean

Perform an add(Object) of all values in collection

Parameters
collection Collection<E>: The collection whose contents are to be retrieved.

clear

fun clear(): Unit

Make the array map empty. All storage is released.

contains

fun contains(@Nullable element: E?): Boolean

Check whether a value exists in the set.

Parameters
key E?: The value to search for.
Return
Boolean: Returns true if the value exists, else false.

containsAll

fun containsAll(@NonNull elements: Collection<E>): Boolean

Determine if the array set contains all of the values in the given collection.

Parameters
collection Collection<E>: The collection whose contents are to be checked against.
Return
Boolean: Returns true if this array set contains a value for every entry in collection, else returns false.

ensureCapacity

fun ensureCapacity(minimumCapacity: Int): Unit

Ensure the array map can hold at least minimumCapacity items.

equals

fun equals(other: Any?): Boolean

This implementation returns false if the object is not a set, or if the sets have different sizes. Otherwise, for each value in this set, it checks to make sure the value also exists in the other set. If any value doesn't exist, the method returns false; otherwise, it returns true.

hashCode

fun hashCode(): Int

indexOf

fun indexOf(@Nullable key: Any?): Int

Returns the index of a value in the set.

Parameters
key Any?: The value to search for.
Return
Int: Returns the index of the value if it exists, else a negative integer.

isEmpty

fun isEmpty(): Boolean

Return true if the array map contains no items.

iterator

fun iterator(): MutableIterator<E>

Return an java.util.Iterator over all values in the set.

Note: this is a fairly inefficient way to access the array contents, it requires generating a number of temporary objects and allocates additional state information associated with the container that will remain for the life of the container.

remove

fun remove(@Nullable element: E?): Boolean

Removes the specified object from this set.

Parameters
object E?: the object to remove.
Return
Boolean: true if this set was modified, false otherwise.

removeAll

fun removeAll(@NonNull array: ArraySet<out E>): Boolean

Perform a remove(Object) of all values in array

Parameters
array ArraySet<out E>: The array whose contents are to be removed.

removeAll

fun removeAll(@NonNull elements: Collection<E>): Boolean

Remove all values in the array set that exist in the given collection.

Parameters
collection Collection<E>: The collection whose contents are to be used to remove values.
Return
Boolean: Returns true if any values were removed from the array set, else false.

removeAt

fun removeAt(index: Int): E

Remove the key/value mapping at the given index.

Parameters
index Int: The desired index, must be between 0 and size()-1.
Return
E: Returns the value that was stored at this index.

retainAll

fun retainAll(@NonNull elements: Collection<E>): Boolean

Remove all values in the array set that do not exist in the given collection.

Parameters
collection Collection<E>: The collection whose contents are to be used to determine which values to keep.
Return
Boolean: Returns true if any values were removed from the array set, else false.

toArray

@NonNull fun toArray(): Array<Any!>

toArray

@NonNull fun <T : Any!> toArray(@NonNull array: Array<T>): Array<T>

toString

fun toString(): String

This implementation composes a string by iterating over its values. If this set contains itself as a value, the string "(this Set)" will appear in its place.

valueAt

@Nullable fun valueAt(index: Int): E?

Return the value at the given index in the array.

Parameters
index Int: The desired index, must be between 0 and size()-1.
Return
E?: Returns the value stored at the given index.

Properties

size

val size: Int

Return the number of items in this array map.