E - the type of elements maintained by this setpublic class ArraySet<E>
extends java.util.AbstractSet<E>
implements java.io.Serializable
Hash table and array implementation of the Set interface,
with predictable iteration order. This implementation is similar to
LinkedHashSet, but uses a more compact memory representation
originally pioneered by PyPy, and subsequently
adopted in Python 3.6.
This class provides all of the optional Set operations, and
permits null elements. Like HashSet, it provides constant-time
performance for the basic operations (add, contains and
remove), assuming the hash function disperses elements
properly among the buckets. Performance is typically within 5% of
HashSet, with only around a third of the memory overhead
(a quarter of LinkedHashSet, and close to a plain ArrayList).
Unlike HashSet and LinkedHashSet, this class does not cache the hash code value of its elements, as this is typically redundant: numeric types are a trivial transformation, while Strings already cache their hash values. This may however result in a significant negative performance impact if element hashCode/equality checks are expensive.
Note that this implementation is not synchronized.
If multiple threads access an ArraySet concurrently, and at least
one of the threads modifies the set, it must be synchronized
externally. This is typically accomplished by synchronizing on some
object that naturally encapsulates the set.
If no such object exists, the set should be "wrapped" using the
Collections.synchronizedSet
method. This is best done at creation time, to prevent accidental
unsynchronized access to the set:
Set<...> s = Collections.synchronizedSet(new ArraySet<>(...));
The iterators returned by this class's iterator method are
fail-fast: if the set is modified at any time after the iterator
is created, in any way except through the iterator's own remove
method, the iterator will throw a ConcurrentModificationException.
Thus, in the face of concurrent modification, the iterator fails quickly
and cleanly, rather than risking arbitrary, non-deterministic behavior at
an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
This section covers the current implementation; future releases may change some or all of the details.
This implementation stores all elements in an insertion-ordered array. Lookup is done via a hashtable of indices, using double hashing to reduce collisions. As the set grows, the element array grows in the same manner as an ArrayList, and the index hashtable is regenerated.
Iteration performance is similar to ArrayList, though if a lot of elements are deleted, the element array will not be shrunk, meaning iteration performance does not recover once a set has been large.
Empty and singleton sets do not allocate any arrays, taking 32B total. Arrays will be allocated once insert is called for a second time, bringing memory use up to 136B until the next resize (11 elements).
Set,
HashSet,
LinkedHashSet,
Serialized Form| Constructor and Description |
|---|
ArraySet()
Constructs an empty set with an initial capacity of ten.
|
ArraySet(java.util.Collection<? extends E> elements)
Constructs a set containing the elements of the specified
collection, in the order they are returned by the collection's
iterator.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E e) |
boolean |
contains(java.lang.Object o) |
void |
forEach(java.util.function.Consumer<? super E> action) |
java.util.Iterator<E> |
iterator() |
boolean |
remove(java.lang.Object o) |
int |
size() |
java.util.Spliterator<E> |
spliterator() |
static <T> java.util.stream.Collector<T,?,java.util.Set<T>> |
toArraySet() |
static <E> ArraySet<E> |
withInitialCapacity(int initialCapacity)
Constructs an empty set with the specified initial capacity.
|
addAll, clear, containsAll, isEmpty, retainAll, toArray, toArray, toStringclone, finalize, getClass, notify, notifyAll, wait, wait, waitpublic ArraySet()
public ArraySet(java.util.Collection<? extends E> elements)
elements - the collection whose elements are to be placed into this listjava.lang.NullPointerException - if the specified collection is nullpublic static <T> java.util.stream.Collector<T,?,java.util.Set<T>> toArraySet()
public static <E> ArraySet<E> withInitialCapacity(int initialCapacity)
E - the type of elements maintained by the setinitialCapacity - the initial capacity of the setjava.lang.IllegalArgumentException - if the specified initial capacity
is negativepublic void forEach(java.util.function.Consumer<? super E> action)
forEach in interface java.lang.Iterable<E>public int size()
public java.util.Iterator<E> iterator()
public java.util.Spliterator<E> spliterator()
public boolean contains(java.lang.Object o)
public boolean add(E e)