ArraySet

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

java.lang.Object
   ↳ androidx.collection.ArraySet<E>


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.

Summary

Public constructors

ArraySet()

Create a new empty ArraySet.

ArraySet(int capacity)

Create a new ArraySet with a given initial capacity.

ArraySet(ArraySet<E> set)

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

ArraySet(Collection<E> set)

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

Public methods

boolean add(E value)

Adds the specified object to this set.

boolean addAll(Collection<? extends E> collection)

Perform an add(Object) of all values in collection

void addAll(ArraySet<? extends E> array)

Perform a add(Object) of all values in array

void clear()

Make the array map empty.

boolean contains(Object key)

Check whether a value exists in the set.

boolean containsAll(Collection<?> collection)

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

void ensureCapacity(int minimumCapacity)

Ensure the array map can hold at least minimumCapacity items.

boolean equals(Object object)

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

int hashCode()

int indexOf(Object key)

Returns the index of a value in the set.

boolean isEmpty()

Return true if the array map contains no items.

Iterator<E> iterator()

Return an Iterator over all values in the set.

boolean remove(Object object)

Removes the specified object from this set.

boolean removeAll(Collection<?> collection)

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

boolean removeAll(ArraySet<? extends E> array)

Perform a remove(Object) of all values in array

E removeAt(int index)

Remove the key/value mapping at the given index.

boolean retainAll(Collection<?> collection)

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

int size()

Return the number of items in this array map.

<T> T[] toArray(T[] array)
Object[] toArray()
String toString()

This implementation composes a string by iterating over its values.

E valueAt(int index)

Return the value at the given index in the array.

Inherited methods

Public constructors

ArraySet

public ArraySet ()

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

ArraySet

public ArraySet (int capacity)

Create a new ArraySet with a given initial capacity.

Parameters
capacity int

ArraySet

public ArraySet (ArraySet<E> set)

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

Parameters
set ArraySet

ArraySet

public ArraySet (Collection<E> set)

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

Parameters
set Collection

Public methods

add

public boolean add (E value)

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.

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

Throws
ClassCastException when the class of the object is inappropriate for this set.

addAll

public boolean addAll (Collection<? extends E> collection)

Perform an add(Object) of all values in collection

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

Returns
boolean

addAll

public void addAll (ArraySet<? extends E> array)

Perform a add(Object) of all values in array

Parameters
array ArraySet: The array whose contents are to be retrieved.

clear

public void clear ()

Make the array map empty. All storage is released.

contains

public boolean contains (Object key)

Check whether a value exists in the set.

Parameters
key Object: The value to search for.

Returns
boolean Returns true if the value exists, else false.

containsAll

public boolean containsAll (Collection<?> collection)

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

Parameters
collection Collection: The collection whose contents are to be checked against.

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

ensureCapacity

public void ensureCapacity (int minimumCapacity)

Ensure the array map can hold at least minimumCapacity items.

Parameters
minimumCapacity int

equals

public boolean equals (Object object)

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.

Parameters
object Object

Returns
boolean

hashCode

public int hashCode ()

Returns
int

indexOf

public int indexOf (Object key)

Returns the index of a value in the set.

Parameters
key Object: The value to search for.

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

isEmpty

public boolean isEmpty ()

Return true if the array map contains no items.

Returns
boolean

iterator

public Iterator<E> iterator ()

Return an 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.

Returns
Iterator<E>

remove

public boolean remove (Object object)

Removes the specified object from this set.

Parameters
object Object: the object to remove.

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

removeAll

public boolean removeAll (Collection<?> collection)

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

Parameters
collection Collection: 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.

removeAll

public boolean removeAll (ArraySet<? extends E> array)

Perform a remove(Object) of all values in array

Parameters
array ArraySet: The array whose contents are to be removed.

Returns
boolean

removeAt

public E removeAt (int index)

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.

retainAll

public boolean retainAll (Collection<?> collection)

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

Parameters
collection Collection: 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.

size

public int size ()

Return the number of items in this array map.

Returns
int

toArray

public T[] toArray (T[] array)

Parameters
array T

Returns
T[]

toArray

public Object[] toArray ()

Returns
Object[]

toString

public 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.

Returns
String

valueAt

public E valueAt (int index)

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.