Class ImmutableNavigableSet<E>
- Type Parameters:
E- element type
- All Implemented Interfaces:
Iterable<E>,Collection<E>,NavigableSet<E>,Set<E>,SortedSet<E>
NavigableSet implementation optimized for read efficiency.
Because the elements are stored in an array, it's also possible to get the element by index; see get().
-
Field Summary
Fields inherited from class io.permazen.util.AbstractNavigableSet
bounds -
Constructor Summary
ConstructorsConstructorDescriptionImmutableNavigableSet(E[] elems, int minIndex, int maxIndex, Comparator<? super E> comparator) Primary constructor.ImmutableNavigableSet(E[] elems, Comparator<? super E> comparator) Convenience constructor.ImmutableNavigableSet(NavigableSet<E> source) Constructor.ImmutableNavigableSet(NavigableSet<E> source, Comparator<? super E> comparator) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionintbinarySearch(E elem) Search for the given element in the underlying array.protected Spliterator<E>buildSpliterator(Iterator<E> iterator) Build aSpliteratorappropriate for this set from the given instance iterator.Comparator<? super E>booleanprotected ImmutableNavigableSet<E>createSubSet(boolean reverse, Bounds<E> newBounds) Create a (possibly reversed) view of this instance with (possibly) tighter lower and/or upper bounds.first()get(int index) Get the element at the specified index.booleanisEmpty()Overridden inAbstractIterationSetto minimize the use ofAbstractIterationSet.size().iterator()last()pollLast()intsize()Calculate size.Overridden inAbstractIterationSetto avoid the use ofAbstractIterationSet.size().stream()Methods inherited from class io.permazen.util.AbstractNavigableSet
descendingSet, getBounds, getComparator, headSet, headSet, isWithinLowerBound, isWithinUpperBound, remove, searchAbove, searchBelow, subSet, subSet, tailSet, tailSetMethods inherited from class io.permazen.util.AbstractIterationSet
equals, hashCode, toArray, toArrayMethods inherited from class java.util.AbstractSet
removeAllMethods inherited from class java.util.AbstractCollection
add, addAll, clear, containsAll, retainAll, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, toArray
-
Constructor Details
-
ImmutableNavigableSet
Convenience constructor.Equivalent to:
ImmutableNavigableSet(elems, 0, elems.length, comparator).- Parameters:
elems- sorted element array; this array is not copied and must be already sortedcomparator- element comparator, or null for natural ordering- Throws:
IllegalArgumentException- ifelemsis null
-
ImmutableNavigableSet
public ImmutableNavigableSet(E[] elems, int minIndex, int maxIndex, Comparator<? super E> comparator) Primary constructor.- Parameters:
elems- sorted element array; this array is not copied and must be already sortedminIndex- minimum index into array (inclusive)maxIndex- maximum index into array (exclusive)comparator- element comparator, or null for natural ordering- Throws:
IllegalArgumentException- ifelemsis nullIllegalArgumentException- ifelemshas length less thanmaxIndexIllegalArgumentException- ifminIndex > maxIndex
-
Method Details
-
get
Get the element at the specified index.- Parameters:
index- index into the ordered element array- Returns:
- the element at the specified index
- Throws:
IndexOutOfBoundsException- ifindexis negative or greater than or equal tosize()
-
binarySearch
Search for the given element in the underlying array.This method works like
Arrays.binarySearch(), returning either the index ofelemin the underlying array given to the constructor if found, or else the one's complement ofelem's insertion point.The array searched is the array given to the constructor, or if
ImmutableNavigableSet(NavigableSet)was used, an array containing all of the elements in this set.- Parameters:
elem- element to search for- Returns:
- index of
elem, or-(insertion point) - 1if not found
-
comparator
-
isEmpty
public boolean isEmpty()Description copied from class:AbstractIterationSetOverridden inAbstractIterationSetto minimize the use ofAbstractIterationSet.size().- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classAbstractIterationSet<E>
-
size
public int size()Description copied from class:AbstractIterationSetCalculate size.The implementation in
AbstractIterationSetiterates through all of the elements.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Overrides:
sizein classAbstractIterationSet<E>
-
contains
- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>
-
first
-
last
-
pollFirst
- Specified by:
pollFirstin interfaceNavigableSet<E>- Overrides:
pollFirstin classAbstractNavigableSet<E>
-
pollLast
- Specified by:
pollLastin interfaceNavigableSet<E>- Overrides:
pollLastin classAbstractNavigableSet<E>
-
lower
- Specified by:
lowerin interfaceNavigableSet<E>- Overrides:
lowerin classAbstractNavigableSet<E>
-
floor
- Specified by:
floorin interfaceNavigableSet<E>- Overrides:
floorin classAbstractNavigableSet<E>
-
higher
- Specified by:
higherin interfaceNavigableSet<E>- Overrides:
higherin classAbstractNavigableSet<E>
-
ceiling
- Specified by:
ceilingin interfaceNavigableSet<E>- Overrides:
ceilingin classAbstractNavigableSet<E>
-
iterator
- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin interfaceNavigableSet<E>- Specified by:
iteratorin interfaceSet<E>- Specified by:
iteratorin classAbstractIterationSet<E>
-
descendingIterator
- Specified by:
descendingIteratorin interfaceNavigableSet<E>- Overrides:
descendingIteratorin classAbstractNavigableSet<E>
-
spliterator
Description copied from class:AbstractIterationSetOverridden inAbstractIterationSetto avoid the use ofAbstractIterationSet.size().Note: the underlying
CloseableIteratoris not closed when this method is used. Prefer usingAbstractIterationSet.buildSpliterator(java.util.Iterator<E>)and arranging for the iterator to be closed separately.- Specified by:
spliteratorin interfaceCollection<E>- Specified by:
spliteratorin interfaceIterable<E>- Specified by:
spliteratorin interfaceSet<E>- Specified by:
spliteratorin interfaceSortedSet<E>- Overrides:
spliteratorin classAbstractIterationSet<E>
-
stream
Description copied from class:AbstractIterationSetThe implementation in
AbstractIterationSetbuild a stream from the results fromAbstractIterationSet.iterator()andbuildSpliterator(), and marks the iterator for close viaStream.onClose().- Specified by:
streamin interfaceCollection<E>- Overrides:
streamin classAbstractIterationSet<E>
-
buildSpliterator
Description copied from class:AbstractIterationSetBuild aSpliteratorappropriate for this set from the given instance iterator.Implementations should probably use
Spliterators.spliteratorUnknownSize()unless the size is known.- Overrides:
buildSpliteratorin classAbstractNavigableSet<E>- Parameters:
iterator- a new iterator returned from {#link #iterator}
-
createSubSet
Description copied from class:AbstractNavigableSetCreate a (possibly reversed) view of this instance with (possibly) tighter lower and/or upper bounds. ThenewBoundsare consistent with the new ordering (i.e., reversed relative to this instance's ordering ifreverseis true) and have already been range-checked against this instance's current bounds.- Specified by:
createSubSetin classAbstractNavigableSet<E>- Parameters:
reverse- whether the new set's ordering should be reversed relative to this instance's orderingnewBounds- new bounds- Returns:
- restricted and/or reversed view of this instance
-