Package android.util

Class ArraySet<E>

  • All Implemented Interfaces:
    Iterable<E>, Collection<E>, Set<E>

    public final class ArraySet<E>
    extends Object
    implements Collection<E>, Set<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.

    • Constructor Summary

      Constructors 
      Constructor Description
      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)
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(E value)
      Adds the specified object to this set.
      void addAll​(ArraySet<? extends E> array)
      Perform a add(Object) of all values in array
      boolean addAll​(Collection<? extends E> collection)
      Perform an add(Object) of all values in collection
      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.
      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​(ArraySet<? extends E> array)
      Perform a remove(Object) of all values in array
      boolean removeAll​(Collection<?> collection)
      Remove all values in the array set that exist in the given collection.
      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.
      Object[] toArray()
      Returns a new array containing all elements contained in this Collection.
      <T> T[] toArray​(T[] array)
      Returns an array containing all elements contained in this Collection.
      String toString()
      Returns a string containing a concise, human-readable description of this object.
      E valueAt​(int index)
      Return the value at the given index in the array.
    • Constructor Detail

      • 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.
      • ArraySet

        public ArraySet​(ArraySet<E> set)
        Create a new ArraySet with the mappings from the given ArraySet.
    • Method Detail

      • ensureCapacity

        public void ensureCapacity​(int minimumCapacity)
        Ensure the array map can hold at least minimumCapacity items.
      • contains

        public boolean contains​(Object key)
        Check whether a value exists in the set.
        Specified by:
        contains in interface Collection<E>
        Specified by:
        contains in interface Set<E>
        Parameters:
        key - The value to search for.
        Returns:
        Returns true if the value exists, else false.
      • indexOf

        public int indexOf​(Object key)
        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.
      • valueAt

        public E valueAt​(int index)
        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.
      • isEmpty

        public boolean isEmpty()
        Return true if the array map contains no items.
        Specified by:
        isEmpty in interface Collection<E>
        Specified by:
        isEmpty in interface Set<E>
        Returns:
        true if this Collection has no elements, false otherwise.
        See Also:
        Collection.size()
      • add

        public boolean add​(E value)
        Adds the specified object to this set. The set is not modified if it already contains the object.
        Specified by:
        add in interface Collection<E>
        Specified by:
        add in interface Set<E>
        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.
      • addAll

        public void addAll​(ArraySet<? extends E> array)
        Perform a add(Object) of all values in array
        Parameters:
        array - The array whose contents are to be retrieved.
      • remove

        public boolean remove​(Object object)
        Removes the specified object from this set.
        Specified by:
        remove in interface Collection<E>
        Specified by:
        remove in interface Set<E>
        Parameters:
        object - the object to remove.
        Returns:
        true if this set was modified, false otherwise.
      • removeAt

        public E removeAt​(int index)
        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.
      • removeAll

        public boolean removeAll​(ArraySet<? extends E> array)
        Perform a remove(Object) of all values in array
        Parameters:
        array - The array whose contents are to be removed.
      • size

        public int size()
        Return the number of items in this array map.
        Specified by:
        size in interface Collection<E>
        Specified by:
        size in interface Set<E>
        Returns:
        how many objects this Collection contains, or Integer.MAX_VALUE if there are more than Integer.MAX_VALUE elements in this Collection.
      • toArray

        public Object[] toArray()
        Description copied from interface: Collection
        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.
        Specified by:
        toArray in interface Collection<E>
        Specified by:
        toArray in interface Set<E>
        Returns:
        an array of the elements from this Collection.
      • toArray

        public <T> T[] toArray​(T[] array)
        Description copied from interface: Collection
        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.
        Specified by:
        toArray in interface Collection<E>
        Specified by:
        toArray in interface Set<E>
        Parameters:
        array - the array.
        Returns:
        an array of the elements from this Collection.
        See Also:
        Collection.toArray(Object[])
      • equals

        public boolean equals​(Object object)
        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 Object.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.

        Specified by:
        equals in interface Collection<E>
        Specified by:
        equals in interface Set<E>
        Overrides:
        equals in class Object
        Parameters:
        object - the object to compare this instance with.
        Returns:
        true if the specified object is equal to this Object; false otherwise.
        See Also:
        Object.hashCode()
      • toString

        public String toString()
        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.

        Overrides:
        toString in class Object
        Returns:
        a printable representation of this object.
      • 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.

        Specified by:
        iterator in interface Collection<E>
        Specified by:
        iterator in interface Iterable<E>
        Specified by:
        iterator in interface Set<E>
        Returns:
        an iterator for accessing the Collection contents.
        See Also:
        Iterator
      • containsAll

        public boolean containsAll​(Collection<?> collection)
        Determine if the array set contains all of the values in the given collection.
        Specified by:
        containsAll in interface Collection<E>
        Specified by:
        containsAll in interface Set<E>
        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.
      • addAll

        public boolean addAll​(Collection<? extends E> collection)
        Perform an add(Object) of all values in collection
        Specified by:
        addAll in interface Collection<E>
        Specified by:
        addAll in interface Set<E>
        Parameters:
        collection - The collection whose contents are to be retrieved.
        Returns:
        true if this Collection is modified, false otherwise.
      • removeAll

        public boolean removeAll​(Collection<?> collection)
        Remove all values in the array set that exist in the given collection.
        Specified by:
        removeAll in interface Collection<E>
        Specified by:
        removeAll in interface Set<E>
        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.
      • retainAll

        public boolean retainAll​(Collection<?> collection)
        Remove all values in the array set that do not exist in the given collection.
        Specified by:
        retainAll in interface Collection<E>
        Specified by:
        retainAll in interface Set<E>
        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.