Class ArraySet<E>
- java.lang.Object
-
- android.util.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 traditionalHashSet. The design is very similar toArrayMap, 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.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(E value)Adds the specified object to this set.voidaddAll(ArraySet<? extends E> array)Perform aadd(Object)of all values in arraybooleanaddAll(Collection<? extends E> collection)Perform anadd(Object)of all values in collectionvoidclear()Make the array map empty.booleancontains(Object key)Check whether a value exists in the set.booleancontainsAll(Collection<?> collection)Determine if the array set contains all of the values in the given collection.voidensureCapacity(int minimumCapacity)Ensure the array map can hold at least minimumCapacity items.booleanequals(Object object)Compares this instance with the specified object and indicates if they are equal.inthashCode()Returns an integer hash code for this object.intindexOf(Object key)Returns the index of a value in the set.booleanisEmpty()Return true if the array map contains no items.Iterator<E>iterator()Return anIteratorover all values in the set.booleanremove(Object object)Removes the specified object from this set.booleanremoveAll(ArraySet<? extends E> array)Perform aremove(Object)of all values in arraybooleanremoveAll(Collection<?> collection)Remove all values in the array set that exist in the given collection.EremoveAt(int index)Remove the key/value mapping at the given index.booleanretainAll(Collection<?> collection)Remove all values in the array set that do not exist in the given collection.intsize()Return the number of items in this array map.Object[]toArray()Returns a new array containing all elements contained in thisCollection.<T> T[]toArray(T[] array)Returns an array containing all elements contained in thisCollection.StringtoString()Returns a string containing a concise, human-readable description of this object.EvalueAt(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.
-
ArraySet
public ArraySet(Collection<E> set)
-
-
Method Detail
-
clear
public void clear()
Make the array map empty. All storage is released.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- See Also:
Collection.isEmpty(),Collection.size()
-
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.
-
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 andsize()-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:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Returns:
trueif thisCollectionhas no elements,falseotherwise.- 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:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Parameters:
value- the object to add.- Returns:
trueif this set is modified,falseotherwise.- Throws:
ClassCastException- when the class of the object is inappropriate for this set.
-
addAll
public void addAll(ArraySet<? extends E> array)
Perform aadd(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.
-
removeAt
public E removeAt(int index)
Remove the key/value mapping at the given index.- Parameters:
index- The desired index, must be between 0 andsize()-1.- Returns:
- Returns the value that was stored at this index.
-
removeAll
public boolean removeAll(ArraySet<? extends E> array)
Perform aremove(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.
-
toArray
public Object[] toArray()
Description copied from interface:CollectionReturns a new array containing all elements contained in thisCollection. 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 theCollection. A new array is created even if the underlying data structure is already an array.
-
toArray
public <T> T[] toArray(T[] array)
Description copied from interface:CollectionReturns an array containing all elements contained in thisCollection. 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 thisCollection, the array element following theCollectionelements 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 astoArray()does.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<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,omust 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
trueonly ifthis == o. See Writing a correctequalsmethod if you intend implementing your ownequalsmethod.The general contract for the
equalsandObject.hashCode()methods is that ifequalsreturnstruefor any two objects, thenhashCode()must return the same value for these objects. This means that subclasses ofObjectusually 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:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceSet<E>- Overrides:
equalsin classObject- Parameters:
object- the object to compare this instance with.- Returns:
trueif the specified object is equal to thisObject;falseotherwise.- See Also:
Object.hashCode()
-
hashCode
public int hashCode()
Returns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually 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
hashCodemethod if you intend implementing your ownhashCodemethod.- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceSet<E>- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
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
toStringmethod if you intend implementing your owntoStringmethod.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.
-
iterator
public Iterator<E> iterator()
Return anIteratorover 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.
-
containsAll
public boolean containsAll(Collection<?> collection)
Determine if the array set contains all of the values in the given collection.- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceSet<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 anadd(Object)of all values in collection
-
removeAll
public boolean removeAll(Collection<?> collection)
Remove all values in the array set that exist in the given collection.
-
retainAll
public boolean retainAll(Collection<?> collection)
Remove all values in the array set that do not exist in the given collection.
-
-