ArraySet


ArraySet is a generic set data structure that is designed to be more memory efficient than a traditional 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.

This structure is NOT thread-safe.

Summary

Public constructors

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

Create a new ArraySet with items from the given array.

Cmn
android
N
<E : Any?> ArraySet(capacity: Int)

Creates a new empty ArraySet.

Cmn
android
N
<E : Any?> ArraySet(set: ArraySet<E>?)

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

Cmn
android
N
<E : Any?> ArraySet(set: Collection<E>?)

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

Cmn
android
N

Public functions

open Boolean
add(element: E)

Adds the specified object to this set.

Cmn
android
N
Unit
addAll(array: ArraySet<E>)

Perform a add of all values in array

Cmn
android
N
open Boolean
addAll(elements: Collection<E>)

Perform an add of all values in elements

Cmn
android
N
open Unit

Make the array map empty.

Cmn
android
N
open operator Boolean
contains(element: E)

Check whether a value exists in the set.

Cmn
android
N
open Boolean
containsAll(elements: Collection<E>)

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

Cmn
android
N
Unit
ensureCapacity(minimumCapacity: Int)

Ensure the array map can hold at least minimumCapacity items.

Cmn
android
N
open operator Boolean
equals(other: Any?)

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

Cmn
android
N
open Int
Cmn
android
N
Int
indexOf(key: Any?)

Returns the index of a value in the set.

Cmn
android
N
open Boolean

Return true if the array map contains no items.

Cmn
android
N
open operator MutableIterator<E>

Return a MutableIterator over all values in the set.

Cmn
android
N
open Boolean
remove(element: E)

Removes the specified object from this set.

Cmn
android
N
Boolean
removeAll(array: ArraySet<E>)

Perform a remove of all values in array

Cmn
android
N
open Boolean
removeAll(elements: Collection<E>)

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

Cmn
android
N
E
removeAt(index: Int)

Remove the key/value mapping at the given index.

Cmn
android
N
open Boolean
retainAll(elements: Collection<E>)

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

Cmn
android
N
Array<Any?>
android
Array<T>
<T : Any?> toArray(array: Array<T>)
android
open String

This implementation composes a string by iterating over its values.

Cmn
android
N
E
valueAt(index: Int)

Return the value at the given index in the array.

Cmn
android
N

Public properties

open Int
Cmn
android
N

Inherited functions

From kotlin.collections.Collection
open Stream<E>
android
open Spliterator<E>
android
open Stream<E>
android
open Array<T>

This function is deprecated. This member is not fully supported by Kotlin compiler, so it may be absent or have different signature in next major version

android
From kotlin.collections.Iterable
open Unit
android
From kotlin.collections.MutableCollection
open Boolean
android

Public constructors

ArraySet

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

Create a new ArraySet with items from the given array.

ArraySet

<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

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

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

ArraySet

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

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

Public functions

add

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
Boolean

true if this set is modified, false otherwise.

Throws
kotlin.ConcurrentModificationException

if concurrent modifications detected.

addAll

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

Perform a add of all values in array

Parameters
array: ArraySet<E>

The array whose contents are to be retrieved.

Throws
kotlin.ConcurrentModificationException

if concurrent modifications detected.

addAll

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

Perform an add of all values in elements

Parameters
elements: Collection<E>

The collection whose contents are to be retrieved.

clear

open fun clear(): Unit

Make the array map empty. All storage is released.

Throws
kotlin.ConcurrentModificationException

if concurrent modifications detected.

contains

open operator fun contains(element: E): Boolean

Check whether a value exists in the set.

Parameters
element: E

The value to search for.

Returns
Boolean

Returns true if the value exists, else false.

containsAll

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

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

Parameters
elements: Collection<E>

The collection whose contents are to be checked against.

Returns
Boolean

Returns true if this array set contains a value for every entry in elements else returns false.

ensureCapacity

fun ensureCapacity(minimumCapacity: Int): Unit

Ensure the array map can hold at least minimumCapacity items.

Throws
kotlin.ConcurrentModificationException

if concurrent modifications detected.

equals

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
equals

hashCode

open fun hashCode(): Int
See also
hashCode

indexOf

fun indexOf(key: Any?): Int

Returns the index of a value in the set.

Parameters
key: Any?

The value to search for.

Returns
Int

Returns the index of the value if it exists, else a negative integer.

isEmpty

open fun isEmpty(): Boolean

Return true if the array map contains no items.

iterator

open operator fun iterator(): MutableIterator<E>

Return a MutableIterator over all values in the set.

Note: this is a less efficient way to access the array contents compared to looping from 0 until size and calling valueAt.

remove

open fun remove(element: E): Boolean

Removes the specified object from this set.

Parameters
element: E

the object to remove.

Returns
Boolean

true if this set was modified, false otherwise.

removeAll

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

Perform a remove of all values in array

Parameters
array: ArraySet<E>

The array whose contents are to be removed.

removeAll

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

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

Parameters
elements: Collection<E>

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

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.

Returns
E

Returns the value that was stored at this index.

Throws
kotlin.ConcurrentModificationException

if concurrent modifications detected.

retainAll

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

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

Parameters
elements: Collection<E>

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

fun toArray(): Array<Any?>

toArray

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

toString

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

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.

Returns
E

Returns the value stored at the given index.

Public properties

size

open val sizeInt