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

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  class ArraySet<E : Any?> : MutableCollection<E>, MutableSet<E>
  ```

*** ** * ** ***

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 : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/ArraySet#ArraySet(kotlin.Array)(array: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<E>?)` Create a new ArraySet with items from the given array. | Cmn android |
| `<E : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/ArraySet#ArraySet(kotlin.Int)(capacity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Creates a new empty ArraySet. | Cmn android |
| `<E : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/ArraySet#ArraySet(androidx.collection.ArraySet)(set: https://developer.android.com/reference/kotlin/androidx/collection/ArraySet<E>?)` Create a new ArraySet with the mappings from the given ArraySet. | Cmn android |
| `<E : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/collection/ArraySet#ArraySet(kotlin.collections.Collection)(set: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html<E>?)` Create a new ArraySet with the mappings from the given `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html`. | Cmn android |

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

| ### Public properties |
|---|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/collection/ArraySet#size()` | Cmn android |

### Inherited functions

From [kotlin.collections.Collection](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html) `open https://developer.android.com/reference/java/util/stream/Stream.html<E>` `@https://developer.android.com/reference/kotlin/androidx/annotation/RecentlyNonNull
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/parallel-stream.html()` android `open https://developer.android.com/reference/java/util/Spliterator.html<E>` `@https://developer.android.com/reference/kotlin/androidx/annotation/RecentlyNonNull
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/spliterator.html()` android `open https://developer.android.com/reference/java/util/stream/Stream.html<E>` `@https://developer.android.com/reference/kotlin/androidx/annotation/RecentlyNonNull
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/stream.html()` android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<T>` `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/to-array.html(generator: https://developer.android.com/reference/java/util/function/IntFunction.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<T>>)` android From [kotlin.collections.Iterable](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterable/index.html) `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterable/for-each.html(@https://developer.android.com/reference/kotlin/androidx/annotation/RecentlyNonNull action: https://developer.android.com/reference/java/util/function/Consumer.html<E>)` android From [kotlin.collections.MutableCollection](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-collection/index.html) `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-collection/remove-if.html(@https://developer.android.com/reference/kotlin/androidx/annotation/RecentlyNonNull filter: https://developer.android.com/reference/java/util/function/Predicate.html<E>)` android

## Public constructors

### ArraySet

Cmn android

```
<E : Any?> ArraySet(array: Array<E>?)
```

Create a new ArraySet with items from the given array.

### ArraySet

Cmn android

```
<E : Any?> ArraySet(capacity: Int = 0)
```

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

### ArraySet

Cmn android

```
<E : Any?> ArraySet(set: ArraySet<E>?)
```

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

### ArraySet

Cmn android

```
<E : Any?> ArraySet(set: Collection<E>?)
```

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

## Public functions

### add

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

```
open fun add(element: E): Boolean
```

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

| Parameters |
|---|---|
| `element: E` | the object to add. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `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

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

```
fun addAll(array: ArraySet<E>): Unit
```

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

| Parameters |
|---|---|
| `array: https://developer.android.com/reference/kotlin/androidx/collection/ArraySet<E>` | 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

Cmn android

```
open fun addAll(elements: Collection<E>): Boolean
```

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

| Parameters |
|---|---|
| `elements: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html<E>` | The collection whose contents are to be retrieved. |

### clear

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

```
open fun clear(): Unit
```

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

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

```
open operator fun contains(element: E): Boolean
```

Check whether a value exists in the set.

| Parameters |
|---|---|
| `element: E` | The value to search for. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Returns true if the value exists, else false. |

### containsAll

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

```
open fun containsAll(elements: Collection<E>): Boolean
```

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

| Parameters |
|---|---|
| `elements: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html<E>` | The collection whose contents are to be checked against. |

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

### ensureCapacity

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

```
fun ensureCapacity(minimumCapacity: Int): Unit
```

Ensure the array map can hold at least `https://developer.android.com/reference/kotlin/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

Cmn android

```
open operator 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.

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

### hashCode

Cmn android

```
open fun hashCode(): Int
```

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

### indexOf

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

```
fun indexOf(key: Any?): Int
```

Returns the index of a value in the set.

| Parameters |
|---|---|
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?` | The value to search for. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Returns the index of the value if it exists, else a negative integer. |

### isEmpty

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

```
open fun isEmpty(): Boolean
```

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

### iterator

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

```
open operator fun iterator(): MutableIterator<E>
```

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/kotlin/androidx/collection/ArraySet#size()` and calling `https://developer.android.com/reference/kotlin/androidx/collection/ArraySet#valueAt(kotlin.Int)`.

### remove

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

```
open fun remove(element: E): Boolean
```

Removes the specified object from this set.

| Parameters |
|---|---|
| `element: E` | the object to remove. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `true` if this set was modified, `false` otherwise. |

### removeAll

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

```
fun removeAll(array: ArraySet<E>): Boolean
```

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

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

### removeAll

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

```
open fun removeAll(elements: Collection<E>): Boolean
```

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

| Parameters |
|---|---|
| `elements: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html<E>` | The collection whose contents are to be used to remove values. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Returns true if any values were removed from the array set, else false. |

### removeAt

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

```
fun removeAt(index: Int): E
```

Remove the key/value mapping at the given index.

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

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

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

```
open fun retainAll(elements: Collection<E>): Boolean
```

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

| Parameters |
|---|---|
| `elements: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html<E>` | The collection whose contents are to be used to determine which values to keep. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Returns true if any values were removed from the array set, else false. |

### toArray

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

```
fun toArray(): Array<Any?>
```

### toArray

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

```
fun <T : Any?> toArray(array: Array<T>): Array<T>
```

### toString

Cmn android

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

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

```
fun valueAt(index: Int): E
```

Return the value at the given index in the array.

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

| Returns |
|---|---|
| `E` | Returns the value stored at the given index. |

## Public properties

### size

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

```
open val size: Int
```