ArraySet
-
Cmn
class ArraySet<E : Any?> : MutableCollection, MutableSet
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 |
|
|---|---|
|
Create a new ArraySet with items from the given array. |
Cmn
android
N
|
|
Creates a new empty ArraySet. |
Cmn
android
N
|
|
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 |
Cmn
android
N
|
Public functions |
||
|---|---|---|
open Boolean |
add(element: E)Adds the specified object to this set. |
Cmn
android
N
|
Unit |
Cmn
android
N
|
|
open Boolean |
addAll(elements: Collection<E>) |
Cmn
android
N
|
open Unit |
clear()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 |
Cmn
android
N
|
open operator Boolean |
This implementation returns false if the object is not a set, or if the sets have different sizes. |
Cmn
android
N
|
open Int |
hashCode() |
Cmn
android
N
|
Int |
Returns the index of a value in the set. |
Cmn
android
N
|
open Boolean |
isEmpty()Return |
Cmn
android
N
|
open operator MutableIterator<E> |
iterator()Return a |
Cmn
android
N
|
open Boolean |
remove(element: E)Removes the specified object from this set. |
Cmn
android
N
|
Boolean |
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 |
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?> |
toArray() |
android
|
Array<T> |
android
|
|
open String |
toString()This implementation composes a string by iterating over its values. |
Cmn
android
N
|
E |
Return the value at the given index in the array. |
Cmn
android
N
|
Inherited functions |
||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||
|
||||||||||||
|
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 |
|
| 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. |
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 |
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. |
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 |
|
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.
| 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. |