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 compressed hashtable of indices, using double hashing to reduce collisions. Small sets achieve a higher compression rate, as lookup indexes require fewer bits; a newly-allocated instance needs only 4 bits per bucket to index the default 10-element array. As the set grows, the element array grows in the same manner as a 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.
Memory overhead is a pointer and a half plus a handful of bits per element in the set. In contrast, HashSet allocates approximately five pointers plus 16 bytes per element, while LinkedHashSet allocates two more pointers on top of that; an ArrayList uses around a pointer and a half.
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) |
java.util.Iterator<E> |
iterator() |
boolean |
remove(java.lang.Object o) |
int |
size() |
java.util.Spliterator<E> |
spliterator() |
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 <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 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)