Package org.plumelib.util
Class ArraySet<E extends @UnknownSignedness Object>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
org.plumelib.util.ArraySet<E>
- Type Parameters:
E
- the type of the set elements
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,Set<E>
A set backed by an array. It permits null values and its iterator has deterministic ordering.
Compared to a HashSet or LinkedHashSet: For very small sets, this uses much less space, has comparable performance, and (like a LinkedHashSet) is deterministic, with elements returned in the order they were inserted. For large sets, this is significantly less performant than other set implementations.
Compared to a TreeSet: This uses somewhat less space, and it does not require defining a comparator. This isn't sorted but does have deterministic ordering. For large sets, this is significantly less performant than other set implementations.
Other ArraySet implementations include:
- https://docs.oracle.com/en/database/oracle/oracle-database/19/olapi/oracle/olapi/ArraySet.html
- https://developer.android.com/reference/android/util/ArraySet
- https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/CopyOnWriteArraySet.html
-
Constructor Summary
ConstructorDescriptionArraySet()
Constructs an emptyArraySet
with the default initial capacity.ArraySet
(int initialCapacity) Constructs an emptyArraySet
with the specified initial capacity.ArraySet
(Collection<? extends E> m) Constructs a newArraySet
with the same elements as the given collection. -
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
addAll
(Collection<? extends E> c) void
clear()
clone()
Returns a copy of this.boolean
void
boolean
isEmpty()
iterator()
static <E> Set<E>
newArraySetOrHashSet
(int capacity) Returns a new ArraySet or HashSet with the given capacity.static <E> Set<E>
Returns a new ArraySet or HashSet with the given elements.static <E> Set<E>
newArraySetOrLinkedHashSet
(int capacity) Returns a new ArraySet or LinkedHashSet with the given capacity.static <E> Set<E>
Returns a new ArraySet or LinkedHashSet with the given elements.boolean
boolean
removeAll
(Collection<?> c) @org.checkerframework.checker.index.qual.NonNegative int
size()
void
sort()
Sorts the internal representation of this.void
sort
(Comparator<? super E> comparator) Sorts the internal representation of this, using the given comparator.Methods inherited from class java.util.AbstractSet
equals, hashCode
Methods inherited from class java.util.AbstractCollection
containsAll, retainAll, toArray, toArray, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.Set
containsAll, retainAll, spliterator, toArray, toArray
-
Constructor Details
-
ArraySet
@SideEffectFree public ArraySet(int initialCapacity) Constructs an emptyArraySet
with the specified initial capacity.- Parameters:
initialCapacity
- the initial capacity- Throws:
IllegalArgumentException
- if the initial capacity is negative
-
ArraySet
@SideEffectFree public ArraySet()Constructs an emptyArraySet
with the default initial capacity. -
ArraySet
Constructs a newArraySet
with the same elements as the given collection.- Parameters:
m
- the collection whose elements are to be placed in the new set- Throws:
NullPointerException
- if the given set is null
-
-
Method Details
-
newArraySetOrHashSet
Returns a new ArraySet or HashSet with the given capacity. Uses an ArraySet if the capacity is small, and a HashSet otherwise.- Type Parameters:
E
- the type of the elements- Parameters:
capacity
- the expected maximum number of elements in the set- Returns:
- a new ArraySet or HashSet with the given capacity
-
newArraySetOrHashSet
Returns a new ArraySet or HashSet with the given elements. Uses an ArraySet if the capacity is small, and a HashSet otherwise.- Type Parameters:
E
- the type of the elements- Parameters:
s
- the elements to put in the returned set- Returns:
- a new ArraySet or HashSet with the given elements
-
newArraySetOrLinkedHashSet
Returns a new ArraySet or LinkedHashSet with the given capacity. Uses an ArraySet if the capacity is small, and a LinkedHashSet otherwise.- Type Parameters:
E
- the type of the elements- Parameters:
capacity
- the expected maximum number of elements in the set- Returns:
- a new ArraySet or LinkedHashSet with the given capacity
-
newArraySetOrLinkedHashSet
Returns a new ArraySet or LinkedHashSet with the given elements. Uses an ArraySet if the capacity is small, and a LinkedHashSet otherwise.- Type Parameters:
E
- the type of the elements- Parameters:
s
- the elements to put in the returned set- Returns:
- a new ArraySet or LinkedHashSet with the given elements
-
size
@Pure public @org.checkerframework.checker.index.qual.NonNegative int size() -
isEmpty
@Pure public boolean isEmpty() -
contains
-
add
-
remove
-
addAll
-
removeAll
-
clear
public void clear() -
iterator
- Specified by:
iterator
in interfaceCollection<E extends @UnknownSignedness Object>
- Specified by:
iterator
in interfaceIterable<E extends @UnknownSignedness Object>
- Specified by:
iterator
in interfaceSet<E extends @UnknownSignedness Object>
- Specified by:
iterator
in classAbstractCollection<E extends @UnknownSignedness Object>
-
forEach
-
clone
Returns a copy of this. -
sort
public void sort()Sorts the internal representation of this. Side-effects the representation, but not the abstract value, of this. Requires that the elements of this are comparable. -
sort
Sorts the internal representation of this, using the given comparator. Side-effects the representation, but not the abstract value, of this.- Parameters:
comparator
- imposes an ordering on the elements of this
-