Android APIs
public final class

ArraySet

extends Object
implements Collection<E> Set<E>
java.lang.Object
   ↳ android.util.ArraySet<E>

Class Overview

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.
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)
Compares this instance with the specified object and indicates if they are equal.

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

int hashCode()
Returns an integer hash code for this object.
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)
Returns an array containing all elements contained in this Collection.
Object[] toArray()
Returns a new array containing all elements contained in this Collection.
String toString()
Returns a string containing a concise, human-readable description of this object.

This implementation composes a string by iterating over its values.

E valueAt(int index)
Return the value at the given index in the array.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.util.Collection
From interface java.util.Set
From interface java.lang.Iterable

Public Constructors

public ArraySet ()

Added in API level 23

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

public ArraySet (int capacity)

Added in API level 23

Create a new ArraySet with a given initial capacity.

public ArraySet (ArraySet<E> set)

Added in API level 23

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

Public Methods

public boolean add (E value)

Added in API level 23

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

Parameters
value the object to add.
Returns
  • true if this set is modified, false otherwise.
Throws
ClassCastException when the class of the object is inappropriate for this set.

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

Added in API level 23

Perform an add(Object) of all values in collection

Parameters
collection The collection whose contents are to be retrieved.
Returns
  • true if this Collection is modified, false otherwise.

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

Added in API level 23

Perform a add(Object) of all values in array

Parameters
array The array whose contents are to be retrieved.

public void clear ()

Added in API level 23

Make the array map empty. All storage is released.

public boolean contains (Object key)

Added in API level 23

Check whether a value exists in the set.

Parameters
key The value to search for.
Returns
  • Returns true if the value exists, else false.

public boolean containsAll (Collection<?> collection)

Added in API level 23

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

Parameters
collection The collection whose contents are to be checked against.
Returns
  • Returns true if this array set contains a value for every entry in collection, else returns false.

public void ensureCapacity (int minimumCapacity)

Added in API level 23

Ensure the array map can hold at least minimumCapacity items.

public boolean equals (Object object)

Added in API level 23

Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.

The default implementation returns true only if this == o. See Writing a correct equals method if you intend implementing your own equals method.

The general contract for the equals and hashCode() methods is that if equals returns true for any two objects, then hashCode() must return the same value for these objects. This means that subclasses of Object usually override either both methods or neither of them.

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 the object to compare this instance with.
Returns
  • true if the specified object is equal to this Object; false otherwise.

public int hashCode ()

Added in API level 23

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Returns
  • this object's hash code.

public int indexOf (Object key)

Added in API level 23

Returns the index of a value in the set.

Parameters
key The value to search for.
Returns
  • Returns the index of the value if it exists, else a negative integer.

public boolean isEmpty ()

Added in API level 23

Return true if the array map contains no items.

Returns
  • true if this Collection has no elements, false otherwise.

public Iterator<E> iterator ()

Added in API level 23

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
  • an iterator for accessing the Collection contents.

public boolean remove (Object object)

Added in API level 23

Removes the specified object from this set.

Parameters
object the object to remove.
Returns
  • true if this set was modified, false otherwise.

public boolean removeAll (Collection<?> collection)

Added in API level 23

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

Parameters
collection The collection whose contents are to be used to remove values.
Returns
  • Returns true if any values were removed from the array set, else false.

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

Added in API level 23

Perform a remove(Object) of all values in array

Parameters
array The array whose contents are to be removed.

public E removeAt (int index)

Added in API level 23

Remove the key/value mapping at the given index.

Parameters
index The desired index, must be between 0 and size()-1.
Returns
  • Returns the value that was stored at this index.

public boolean retainAll (Collection<?> collection)

Added in API level 23

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

Parameters
collection The collection whose contents are to be used to determine which values to keep.
Returns
  • Returns true if any values were removed from the array set, else false.

public int size ()

Added in API level 23

Return the number of items in this array map.

Returns
  • how many objects this Collection contains, or Integer.MAX_VALUE if there are more than Integer.MAX_VALUE elements in this Collection.

public T[] toArray (T[] array)

Added in API level 23

Returns an array containing all elements contained in this Collection. If the specified array is large enough to hold the elements, the specified array is used, otherwise an array of the same type is created. If the specified array is used and is larger than this Collection, the array element following the Collection elements is set to null. If the implementation has ordered elements it will return the element array in the same order as an iterator would return them. toArray(new Object[0]) behaves exactly the same way as toArray() does.

Parameters
array the array.
Returns
  • an array of the elements from this Collection.

public Object[] toArray ()

Added in API level 23

Returns a new array containing all elements contained in this Collection. If the implementation has ordered elements it will return the element array in the same order as an iterator would return them. The array returned does not reflect any changes of the Collection. A new array is created even if the underlying data structure is already an array.

Returns
  • an array of the elements from this Collection.

public String toString ()

Added in API level 23

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

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
  • a printable representation of this object.

public E valueAt (int index)

Added in API level 23

Return the value at the given index in the array.

Parameters
index The desired index, must be between 0 and size()-1.
Returns
  • Returns the value stored at the given index.