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
-
Field Summary
-
Constructor Summary
ConstructorDescriptionArraySet()
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, removeAll
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAll, toArray, toArray, toString
Methods inherited from class java.lang.Object
clone, 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
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:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceSet<E>
- Overrides:
add
in 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:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceSet<E>
- Overrides:
clear
in classAbstractCollection<E>
-
contains
- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceSet<E>
- Overrides:
contains
in classAbstractCollection<E>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceSet<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
-
remove
- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceSet<E>
- Overrides:
remove
in classAbstractCollection<E>
-
size
public int size()- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceSet<E>
- Specified by:
size
in classAbstractCollection<E>
-
iterator
Supports remove. Supports comodification checks.
-