# ArraySet

Artifact: [androidx.collection:collection](https://developer.android.com/jetpack/androidx/releases/collection) [View Source](https://cs.android.com/search?q=file:androidx/collection/ArraySet.+class:androidx.collection.ArraySet) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

*** ** * ** ***


```
public final class ArraySet<E extends Object> implements Collection<@NonNull E>, Set<@NonNull E>
```

<br />

*** ** * ** ***

ArraySet is a generic set data structure that is designed to be more memory efficient than a traditional `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-hash-set/index.html`. 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.

This structure is **NOT** thread-safe.

## Summary

| ### Public constructors |
|---|
| `<E extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/collection/ArraySet#ArraySet(kotlin.Array)(E[] array)` Create a new ArraySet with items from the given array. |
| `<E extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/collection/ArraySet#ArraySet(kotlin.Int)(int capacity)` Creates a new empty ArraySet. |
| `<E extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/collection/ArraySet#ArraySet(androidx.collection.ArraySet)(https://developer.android.com/reference/androidx/collection/ArraySet<@https://developer.android.com/reference/androidx/annotation/NonNull E> set)` Create a new ArraySet with the mappings from the given ArraySet. |
| `<E extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/collection/ArraySet#ArraySet(kotlin.collections.Collection)(https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull E> set)` Create a new ArraySet with the mappings from the given `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html`. |

| ### Public methods |
|---|---|
| `boolean` | `https://developer.android.com/reference/androidx/collection/ArraySet#add(kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull E element)` Adds the specified object to this set. |
| `final void` | `https://developer.android.com/reference/androidx/collection/ArraySet#addAll(androidx.collection.ArraySet)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/collection/ArraySet<@https://developer.android.com/reference/androidx/annotation/NonNull E> array)` Perform a `https://developer.android.com/reference/androidx/collection/ArraySet#add(kotlin.Any)` of all values in `https://developer.android.com/reference/androidx/collection/ArraySet#addAll(androidx.collection.ArraySet)` |
| `boolean` | `https://developer.android.com/reference/androidx/collection/ArraySet#addAll(kotlin.collections.Collection)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull E> elements)` Perform an `https://developer.android.com/reference/androidx/collection/ArraySet#add(kotlin.Any)` of all values in `https://developer.android.com/reference/androidx/collection/ArraySet#addAll(kotlin.collections.Collection)` |
| `void` | `https://developer.android.com/reference/androidx/collection/ArraySet#clear()()` Make the array map empty. |
| `boolean` | `https://developer.android.com/reference/androidx/collection/ArraySet#contains(kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull E element)` Check whether a value exists in the set. |
| `boolean` | `https://developer.android.com/reference/androidx/collection/ArraySet#containsAll(kotlin.collections.Collection)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull E> elements)` Determine if the array set contains all of the values in the given collection. |
| `final void` | `https://developer.android.com/reference/androidx/collection/ArraySet#ensureCapacity(kotlin.Int)(int minimumCapacity)` Ensure the array map can hold at least `https://developer.android.com/reference/androidx/collection/ArraySet#ensureCapacity(kotlin.Int)` items. |
| `boolean` | `https://developer.android.com/reference/androidx/collection/ArraySet#equals(kotlin.Any)(https://developer.android.com/reference/java/lang/Object.html other)` This implementation returns false if the object is not a set, or if the sets have different sizes. |
| `int` | `https://developer.android.com/reference/androidx/collection/ArraySet#getSize()()` |
| `int` | `https://developer.android.com/reference/androidx/collection/ArraySet#hashCode()()` |
| `final int` | `https://developer.android.com/reference/androidx/collection/ArraySet#indexOf(kotlin.Any)(https://developer.android.com/reference/java/lang/Object.html key)` Returns the index of a value in the set. |
| `boolean` | `https://developer.android.com/reference/androidx/collection/ArraySet#isEmpty()()` Return `true` if the array map contains no items. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Iterator.html<@https://developer.android.com/reference/androidx/annotation/NonNull E>` | `https://developer.android.com/reference/androidx/collection/ArraySet#iterator()()` Return a `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-iterator/index.html` over all values in the set. |
| `boolean` | `https://developer.android.com/reference/androidx/collection/ArraySet#remove(kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull E element)` Removes the specified object from this set. |
| `final boolean` | `https://developer.android.com/reference/androidx/collection/ArraySet#removeAll(androidx.collection.ArraySet)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/collection/ArraySet<@https://developer.android.com/reference/androidx/annotation/NonNull E> array)` Perform a `https://developer.android.com/reference/androidx/collection/ArraySet#remove(kotlin.Any)` of all values in `https://developer.android.com/reference/androidx/collection/ArraySet#removeAll(androidx.collection.ArraySet)` |
| `boolean` | `https://developer.android.com/reference/androidx/collection/ArraySet#removeAll(kotlin.collections.Collection)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull E> elements)` Remove all values in the array set that exist in the given collection. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull E` | `https://developer.android.com/reference/androidx/collection/ArraySet#removeAt(kotlin.Int)(int index)` Remove the key/value mapping at the given index. |
| `boolean` | `https://developer.android.com/reference/androidx/collection/ArraySet#retainAll(kotlin.collections.Collection)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull E> elements)` Remove all values in the array set that do **not** exist in the given collection. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull Object[]` | `https://developer.android.com/reference/androidx/collection/ArraySet#toArray()()` |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull T[]` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/collection/ArraySet#toArray(kotlin.Array)(@https://developer.android.com/reference/androidx/annotation/NonNull T[] array)` |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/collection/ArraySet#toString()()` This implementation composes a string by iterating over its values. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull E` | `https://developer.android.com/reference/androidx/collection/ArraySet#valueAt(kotlin.Int)(int index)` Return the value at the given index in the array. |

### Inherited methods

From [kotlin.collections.Collection](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html) `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/stream/Stream.html<@https://developer.android.com/reference/androidx/annotation/NonNull E>` `@https://developer.android.com/reference/androidx/annotation/RecentlyNonNull
https://developer.android.com/reference/java/util/Collection.html#parallelStream()()` `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Spliterator.html<@https://developer.android.com/reference/androidx/annotation/NonNull E>` `@https://developer.android.com/reference/androidx/annotation/RecentlyNonNull
https://developer.android.com/reference/java/util/Collection.html#spliterator()()` `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/stream/Stream.html<@https://developer.android.com/reference/androidx/annotation/NonNull E>` `@https://developer.android.com/reference/androidx/annotation/RecentlyNonNull
https://developer.android.com/reference/java/util/Collection.html#stream()()` `@https://developer.android.com/reference/androidx/annotation/NonNull T[]` `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/java/util/Collection.html#toArray(java.util.function.IntFunction&lt;T[]&gt;)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/IntFunction.html<@https://developer.android.com/reference/androidx/annotation/NonNull T[]> generator)` From [kotlin.collections.Iterable](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterable/index.html) `void` `https://developer.android.com/reference/java/lang/Iterable.html#forEach(java.util.function.Consumer&lt;? super T&gt;)(@https://developer.android.com/reference/androidx/annotation/RecentlyNonNull @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/Consumer.html<@https://developer.android.com/reference/androidx/annotation/NonNull E> action)` From [kotlin.collections.MutableCollection](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-collection/index.html) `boolean` `https://developer.android.com/reference/java/util/Collection.html#removeIf(java.util.function.Predicate&lt;? super E&gt;)(@https://developer.android.com/reference/androidx/annotation/RecentlyNonNull @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/Predicate.html<@https://developer.android.com/reference/androidx/annotation/NonNull E> filter)`

## Public constructors

### ArraySet

```
public <E extends Object> ArraySet(E[] array)
```

Create a new ArraySet with items from the given array.

### ArraySet

```
public <E extends Object> ArraySet(int capacity)
```

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

### ArraySet

```
public <E extends Object> ArraySet(ArraySet<@NonNull E> set)
```

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

### ArraySet

```
public <E extends Object> ArraySet(Collection<@NonNull E> set)
```

Create a new ArraySet with the mappings from the given `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html`.

## Public methods

### add

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public boolean add(@NonNull E element)
```

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

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull E element` | the object to add. |

| Returns |
|---|---|
| `boolean` | `true` if this set is modified, `false` otherwise. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-concurrent-modification-exception/index.html` | if concurrent modifications detected. |

### addAll

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final void addAll(@NonNull ArraySet<@NonNull E> array)
```

Perform a `https://developer.android.com/reference/androidx/collection/ArraySet#add(kotlin.Any)` of all values in `https://developer.android.com/reference/androidx/collection/ArraySet#addAll(androidx.collection.ArraySet)`

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/collection/ArraySet<@https://developer.android.com/reference/androidx/annotation/NonNull E> array` | The array whose contents are to be retrieved. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-concurrent-modification-exception/index.html` | if concurrent modifications detected. |

### addAll

```
public boolean addAll(@NonNull Collection<@NonNull E> elements)
```

Perform an `https://developer.android.com/reference/androidx/collection/ArraySet#add(kotlin.Any)` of all values in `https://developer.android.com/reference/androidx/collection/ArraySet#addAll(kotlin.collections.Collection)`

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull E> elements` | The collection whose contents are to be retrieved. |

### clear

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public void clear()
```

Make the array map empty. All storage is released.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-concurrent-modification-exception/index.html` | if concurrent modifications detected. |

### contains

Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/collection#1.3.0)

```
public boolean contains(@NonNull E element)
```

Check whether a value exists in the set.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull E element` | The value to search for. |

| Returns |
|---|---|
| `boolean` | Returns true if the value exists, else false. |

### containsAll

Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/collection#1.3.0)

```
public boolean containsAll(@NonNull Collection<@NonNull E> elements)
```

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

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull E> elements` | The collection whose contents are to be checked against. |

| Returns |
|---|---|
| `boolean` | Returns true if this array set contains a value for every entry in `https://developer.android.com/reference/androidx/collection/ArraySet#containsAll(kotlin.collections.Collection)` else returns false. |

### ensureCapacity

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final void ensureCapacity(int minimumCapacity)
```

Ensure the array map can hold at least `https://developer.android.com/reference/androidx/collection/ArraySet#ensureCapacity(kotlin.Int)` items.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-concurrent-modification-exception/index.html` | if concurrent modifications detected. |

### equals

```
public boolean equals(Object other)
```

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.

| See also |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/equals.html` |   |

### getSize

Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/collection#1.3.0)

```
public int getSize()
```

### hashCode

```
public int hashCode()
```

| See also |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/hash-code.html` |   |

### indexOf

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final int indexOf(Object key)
```

Returns the index of a value in the set.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/Object.html key` | The value to search for. |

| Returns |
|---|---|
| `int` | Returns the index of the value if it exists, else a negative integer. |

### isEmpty

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public boolean isEmpty()
```

Return `true` if the array map contains no items.

### iterator

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public @NonNull Iterator<@NonNull E> iterator()
```

Return a `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-iterator/index.html` over all values in the set.

**Note:** this is a less efficient way to access the array contents compared to looping from 0 until `https://developer.android.com/reference/androidx/collection/ArraySet#size()` and calling `https://developer.android.com/reference/androidx/collection/ArraySet#valueAt(kotlin.Int)`.

### remove

Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/collection#1.3.0)

```
public boolean remove(@NonNull E element)
```

Removes the specified object from this set.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull E element` | the object to remove. |

| Returns |
|---|---|
| `boolean` | `true` if this set was modified, `false` otherwise. |

### removeAll

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final boolean removeAll(@NonNull ArraySet<@NonNull E> array)
```

Perform a `https://developer.android.com/reference/androidx/collection/ArraySet#remove(kotlin.Any)` of all values in `https://developer.android.com/reference/androidx/collection/ArraySet#removeAll(androidx.collection.ArraySet)`

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/collection/ArraySet<@https://developer.android.com/reference/androidx/annotation/NonNull E> array` | The array whose contents are to be removed. |

### removeAll

Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/collection#1.3.0)

```
public boolean removeAll(@NonNull Collection<@NonNull E> elements)
```

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

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull E> elements` | The collection whose contents are to be used to remove values. |

| Returns |
|---|---|
| `boolean` | Returns true if any values were removed from the array set, else false. |

### removeAt

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final @NonNull E removeAt(int index)
```

Remove the key/value mapping at the given index.

| Parameters |
|---|---|
| `int index` | The desired index, must be between 0 and `https://developer.android.com/reference/androidx/collection/ArraySet#size()`-1. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull E` | Returns the value that was stored at this index. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-concurrent-modification-exception/index.html` | if concurrent modifications detected. |

### retainAll

Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/collection#1.3.0)

```
public boolean retainAll(@NonNull Collection<@NonNull E> elements)
```

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

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Collection.html<@https://developer.android.com/reference/androidx/annotation/NonNull E> elements` | The collection whose contents are to be used to determine which values to keep. |

| Returns |
|---|---|
| `boolean` | Returns true if any values were removed from the array set, else false. |

### toArray

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final @NonNull Object[] toArray()
```

### toArray

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final @NonNull T[] <T extends Object> toArray(@NonNull T[] array)
```

### toString

```
public @NonNull String toString()
```

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

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final @NonNull E valueAt(int index)
```

Return the value at the given index in the array.

| Parameters |
|---|---|
| `int index` | The desired index, must be between 0 and `https://developer.android.com/reference/androidx/collection/ArraySet#size()`-1. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull E` | Returns the value stored at the given index. |