java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
net.i2p.util.ArraySet<E>
- Type Parameters:
E- type of elements in this set
- All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>
- Direct Known Subclasses:
UnmodifiableSortedSet
A small, fast Set with a maximum size, backed by a fixed-size array. Much more space-efficient than HashSet. Unsynchronized, not thread-safe. Null elements are not permitted.
Not appropriate for large Sets that are modified. add(), remove(Object), and contains() are O(n). Warning: addAll() and the Collection constructor are O(n**2).
Nested Class Summary
Modifier and TypeClassDescriptionprivate classModified from CachedIteratorArrayList Iterator implementation for ArraySet.static classException thrown when attempting to add an element to a full ArraySet.Field Summary
Modifier and TypeFieldDescriptionprotected final Object[]The underlying array of entries.private intThe overflow index for overwrite mode.private intThe current size of the set.private final booleanWhether to throw on overflow.static final intThe maximum capacity for this set.private intThe modification count for fail-fast iterators.Constructor Summary
ConstructorsConstructorDescriptionArraySet()A fixed capacity of MAX_CAPACITY.ArraySet(int capacity) Adds over capacity will throw a SetFullException.ArraySet(int capacity, boolean throwOnFull) If throwOnFull is false, adds over capacity will overwrite starting at slot zero.A fixed capacity of arr.length.ArraySet(Collection<? extends E> c) A fixed capacity of max(MAX_CAPACITY, c.size()), which may be more than the resulting set size if there are duplicates in c.ArraySet(Collection<? extends E> c, int capacity) A fixed capacity of max(capacity, c.size()), which may be more than the resulting set size if there are duplicates in c.A fixed capacity of max(MAX_CAPACITY, c.size()) Adds over capacity will throw a SetFullException.A fixed capacity of max(capacity, c.size()) Adds over capacity will throw a SetFullException.Method Summary
Modifier and TypeMethodDescriptionbooleanAdd the element to the set.voidUnconditionally add o to the set.voidclear()booleanget(int index) Returns the element at the specified index.protected intFinds the index of the given object in the set.booleanisEmpty()iterator()Supports remove.booleanprivate voidremoveAt(int i) Remove the entry at the given array index and shift the tail down.intsize()Methods inherited from class AbstractSet
equals, hashCode, removeAllMethods inherited from class AbstractCollection
addAll, containsAll, retainAll, toArray, toArray, toStringMethods inherited from interface Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface Set
addAll, containsAll, retainAll, spliterator, toArray, toArray
Field Details
MAX_CAPACITY
public static final int MAX_CAPACITYThe maximum capacity for this set.- See Also:
_entries
The underlying array of entries._throwOnFull
private final boolean _throwOnFullWhether to throw on overflow._size
private int _sizeThe current size of the set._overflowIndex
private int _overflowIndexThe overflow index for overwrite mode.modCount
private transient int modCountThe modification count for fail-fast iterators.
Constructor Details
ArraySet
public ArraySet()A fixed capacity of MAX_CAPACITY. Adds over capacity will throw a SetFullException.ArraySet
ArraySet
ArraySet
A fixed capacity of max(MAX_CAPACITY, c.size()), which may be more than the resulting set size if there are duplicates in c. Adds over capacity will throw a SetFullException.Warning: O(n**2).
- Parameters:
c- the collection to copy
ArraySet
A fixed capacity of max(capacity, c.size()), which may be more than the resulting set size if there are duplicates in c. Adds over capacity will throw a SetFullException.Warning: O(n**2).
- Parameters:
c- the collection to copycapacity- the maximum capacity
ArraySet
A fixed capacity of arr.length. Adds over capacity will throw a SetFullException. arr must not contain duplicates, no checks are done. arr may contain nulls but they must be at the end.- Parameters:
arr- the array to copy
ArraySet
public ArraySet(int capacity) Adds over capacity will throw a SetFullException.- Parameters:
capacity- the maximum size- Throws:
IllegalArgumentException- if capacity less than 1.
ArraySet
public ArraySet(int capacity, boolean throwOnFull) If throwOnFull is false, adds over capacity will overwrite starting at slot zero. This breaks the AbstractCollection invariant that "a Collection will always contain the specified element after add() returns", but it prevents unexpected exceptions. If throwOnFull is true, adds over capacity will throw a SetFullException.- Parameters:
capacity- the maximum sizethrowOnFull- whether to throw on overflow- Throws:
IllegalArgumentException- if capacity less than 1.
Method Details
indexOf
Finds the index of the given object in the set.- Parameters:
o- the object to find the index of- Returns:
- -1 if not found or if o is null
add
Add the element to the set.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<E>- Parameters:
o- the element to add- Throws:
ArraySet.SetFullException- if throwOnFull was true in constructorNullPointerException- if o is null
addUnique
Unconditionally add o to the set. This avoids the O(n) time of add(), but it's the caller's responsibility to ensure that o is not a duplicate. Any duplicate added will appear in the iterator.- Parameters:
o- non-null, NPE will not be thrown- Throws:
ArraySet.SetFullException- if throwOnFull was true in constructor
clear
public void clear()- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin classAbstractCollection<E>
contains
- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classAbstractCollection<E>
remove
- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classAbstractCollection<E>
removeAt
private void removeAt(int i) Remove the entry at the given array index and shift the tail down. Index must be in [0, _size).- Parameters:
i- index of the entry to remove
size
public int size()- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<E>
get
Returns the element at the specified index.- Parameters:
index- the zero-based position to read- Returns:
- the element at the specified index
- Throws:
IndexOutOfBoundsException- if index is out of bounds
iterator