Package net.i2p.util
Class ArraySet<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
net.i2p.util.ArraySet<E>
- 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(), and contains() are O(n).
Warning: addAll() and the Collection constructor are O(n**2).
- Since:
- 0.9.25, moved to net.i2p.util in 0.9.55
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
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.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
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAllMethods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.Set
addAll, containsAll, equals, hashCode, removeAll, retainAll, spliterator, toArray, toArray
-
Field Details
-
MAX_CAPACITY
public static final int MAX_CAPACITY- See Also:
-
_entries
-
-
Constructor Details
-
ArraySet
public ArraySet()A fixed capacity of MAX_CAPACITY. Adds over capacity will throw a SetFullException. -
ArraySet
A fixed capacity of max(MAX_CAPACITY, c.size()) Adds over capacity will throw a SetFullException.- Since:
- 0.9.55
-
ArraySet
A fixed capacity of max(capacity, c.size()) Adds over capacity will throw a SetFullException.- Since:
- 0.9.55
-
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). -
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).- Since:
- 0.9.55
-
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 size- Throws:
IllegalArgumentException- if capacity less than 1.
-
-
Method Details
-
indexOf
- Returns:
- -1 if not found or if o is null
-
add
- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<E>- 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- Since:
- 0.9.55
-
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>
-
size
public int size()- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<E>
-
iterator
Supports remove. Supports comodification checks.
-