public final class ArraySet<E> extends Object implements Collection<E>, Set<E>
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 and Description |
|---|
ArraySet()
Create a new empty ArraySet.
|
ArraySet(ArraySet<E> set)
Create a new ArraySet with the mappings from the given ArraySet.
|
ArraySet(Collection<E> set) |
ArraySet(int capacity)
Create a new ArraySet with a given initial capacity.
|
| Modifier and Type | Method and 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 an array containing all of the elements in this collection.
|
<T> T[] |
toArray(T[] array)
Returns an array containing all of the elements in this collection;
the runtime type of the returned array is that of the specified array.
|
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.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, waitcopyOf, of, of, of, of, of, of, of, of, of, of, of, of, spliteratorforEach, parallelStream, removeIf, stream, toArraypublic ArraySet()
public ArraySet(int capacity)
public ArraySet(ArraySet<E> set)
public ArraySet(Collection<E> set)
public void clear()
public void ensureCapacity(int minimumCapacity)
public boolean contains(Object key)
public int indexOf(Object key)
key - The value to search for.public E valueAt(int index)
index - The desired index, must be between 0 and size()-1.public boolean isEmpty()
public boolean add(E value)
add in interface Collection<E>add in interface Set<E>value - the object to add.true if this set is modified, false otherwise.ClassCastException - when the class of the object is inappropriate for this set.public void addAll(ArraySet<? extends E> array)
add(Object) of all values in arrayarray - The array whose contents are to be retrieved.public boolean remove(Object object)
public E removeAt(int index)
index - The desired index, must be between 0 and size()-1.public boolean removeAll(ArraySet<? extends E> array)
remove(Object) of all values in arrayarray - The array whose contents are to be removed.public int size()
public Object[] toArray()
CollectionThe returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
public <T> T[] toArray(T[] array)
CollectionIf this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of this collection only if the caller knows that this collection does not contain any null elements.)
If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
Like the Collection.toArray() method, this method acts as bridge between
array-based and collection-based APIs. Further, this method allows
precise control over the runtime type of the output array, and may,
under certain circumstances, be used to save allocation costs.
Suppose x is a collection known to contain only strings. The following code can be used to dump the collection into a newly allocated array of String:
String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to
toArray().toArray in interface Collection<E>toArray in interface Set<E>T - the runtime type of the array to contain the collectionarray - the array into which the elements of this collection are to be
stored, if it is big enough; otherwise, a new array of the same
runtime type is allocated for this purpose.public boolean equals(Object object)
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.
equals in interface Collection<E>equals in interface Set<E>equals in class Objectobject - the object to compare this instance with.true if the specified object is equal to this Object; false otherwise.Object.hashCode()public int hashCode()
Object.equals(java.lang.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.
hashCode in interface Collection<E>hashCode in interface Set<E>hashCode in class ObjectObject.equals(java.lang.Object)public String toString()
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.
public Iterator<E> iterator()
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.
public boolean containsAll(Collection<?> collection)
containsAll in interface Collection<E>containsAll in interface Set<E>collection - The collection whose contents are to be checked against.Collection.contains(Object)public boolean addAll(Collection<? extends E> collection)
add(Object) of all values in collectionaddAll in interface Collection<E>addAll in interface Set<E>collection - The collection whose contents are to be retrieved.Collection.add(Object)public boolean removeAll(Collection<?> collection)
removeAll in interface Collection<E>removeAll in interface Set<E>collection - The collection whose contents are to be used to remove values.Collection.remove(Object),
Collection.contains(Object)public boolean retainAll(Collection<?> collection)
retainAll in interface Collection<E>retainAll in interface Set<E>collection - The collection whose contents are to be used to determine which
values to keep.Collection.remove(Object),
Collection.contains(Object)