Class Iterators
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAn iterator that passes on only selected elements.static interfaceIterators that can look one element ahead.static interfaceIterators which allow arbitrary deep look-ahead.static interfaceIterators that can push an element back into the sequence. -
Method Summary
Modifier and TypeMethodDescriptionstatic <E> Optional<E> Returns an arbitrary remaining element, if available.static <A> Iterator<A> cacheHasNext(Iterator<A> i) Returns an iterator consisting of the same elements as the given iterator, but caching redundant queries.static <A> Iterator<A> Adjusts the element type of a given iterator.static <E> Iterator<E> Returns an iterator consisting of the combined elements of two given iterators in order.static <E> Iterator<E> conditional(boolean cond, E elem) Returns an iterator that produces the given element or nothing, depending on a condition.static <E> Iterator<E> conditional(boolean cond, Supplier<? extends E> elem) Returns an iterator that produces one element computed on the fly or nothing, depending on a condition.static <E> Iterator<E> conditional(boolean cond, Iterator<E> things) Returns an iterator that produces the given elements or nothing, depending on a condition.static <E> Iterator<E> Returns an iterator consisting of the given first element preceding the remaining elements of a given iterator.static <V,L> Iterator <L> depthFirst(V root, Function<? super V, ? extends Iterator<? extends L>> labels, Function<? super V, ? extends Iterator<? extends V>> branches) Returns an iterator that produces the labels of a search tree by depth-first traversal.static <A> Iterator<A> Returns a view on an iterator that has the same remaining elements, except the first ones.static <E> Iterator<E> empty()Returns an iterator consisting of no elements.static <E> Iterator<E> Returns an iterator consisting of the remaining elements of a given iterator that match the given predicate.static <A,B> Iterator <A> filterWithConstraint(BiPredicate<? super A, ? super B> rel, Iterator<A> things, Iterator<B> constraints) Returns an iterator consisting of the elements of another iterator that match a constraint from a parallel iterator.static <E> Optional<E> Returns the first remaining element, if available.static <E,F> Iterator <F> Returns a concatenation of subsequences computed with a function.static <E> Iterator<E> Returns an iterator consisting of the combined elements of all iterators returned by a given iterator in order.static <E> Iterator<E> Returns an iterator that produces unboundedly many elements computed on demand.static <S> Iterator<S> iterate(S seed, Predicate<? super S> hasNext, UnaryOperator<S> next) Returns an iterator that produces elements by iterative application of the given step function to an initial element, conditioned on satisfying the given predicate.static <E,S> Iterator <E> iterate(S seed, Predicate<? super S> hasNext, UnaryOperator<S> next, Function<? super S, ? extends E> view) Returns an iterator that produces elements by iterative application of the given step function to an initial state, conditioned on satisfying the given predicate, computing elements from states with the given view function.static <A> Iterators.LookaheadIterator<A> Returns an view with lookahead capability for a given iterator.static <A,B> Iterator <B> Returns a view on an iterator that has all elements transformed by a given function.static <A> Iterator<A> merge(Comparator<? super A> order, boolean removeDuplicates, Iterator<? extends A> left, Iterator<? extends A> right) Returns the merged combination of two ordered sequences.static <E> Iterators.MultipleLookaheadIterator<E> multipleLookahead(E... elems) Returns an iterator with deep lookahead over the given elements.static <E> Iterators.MultipleLookaheadIterator<E> multipleLookahead(Iterable<? extends E> elems) Returns an iterator with deep lookahead over the given elements.static <E> Iterators.MultipleLookaheadIterator<E> multipleLookahead(Iterator<? extends E> it) Returns an iterator with deep lookahead over the given elements.static <E> Iterator<E> of(E... things) Returns an iterator consisting of exactly the given elements.static <A> Iterators.PushbackIterator<A> Returns an view with pushback capability for a given iterator.static <E> Iterator<E> range(int start, int end, IntFunction<? extends E> gen) Returns an iterator that produces elements computed from the given integer interval in ascending order.static <E> Iterator<E> repeat(E value, int times) Returns an iterator consisting of one value repeated several times.static <E> Iterator<E> singleton(E value) Returns an iterator consisting of exactly one given element.static <E> Iterator<E> Returns an iterator consisting of exactly one given element computed on demand.static <A> Iterator<A> Returns a view on an iterator that has the same elements, up to a given length.static <A> Iterator<A> unmodifiableIterator(Iterator<A> i) Returns an unmodifiable view on an iterator.
-
Method Details
-
cast
Adjusts the element type of a given iterator.- Type Parameters:
A- the desired element type- Parameters:
iterator- an iterator- Returns:
- the same iterator
-
empty
Returns an iterator consisting of no elements.- Type Parameters:
E- the type of elements returned by the returned iterator- Returns:
- an iterator that has no elements
-
singleton
Returns an iterator consisting of exactly one given element. The element is not removable.- Type Parameters:
E- the type of elements returned by the returned iterator- Parameters:
value- the single element of the returned iterator- Returns:
- an iterator that has exactly the given element
-
repeat
Returns an iterator consisting of one value repeated several times.- Type Parameters:
E- the element type- Parameters:
value- the valuetimes- the number of repetitions- Returns:
- an iterator consisting of
timesrepetitions ofvalue - Throws:
IllegalArgumentException- iftimesis negative
-
of
Returns an iterator consisting of exactly the given elements. Elements are not removable.- Type Parameters:
E- the type of elements returned by the returned iterator- Parameters:
things- the elements of the returned iterator- Returns:
- an iterator that has exactly the given elements
-
cons
Returns an iterator consisting of the given first element preceding the remaining elements of a given iterator. The first element is not removable. Further removal requests are passed through to the underlying iterator.- Type Parameters:
E- the type of elements returned by the returned iterator- Parameters:
first- the first elementrest- the iterator of the succeeding elements- Returns:
- an iterator that returns the first element followed by the
remaining elements of
rest - Throws:
NullPointerException- ifrest == null
-
concat
Returns an iterator consisting of the combined elements of two given iterators in order. Removal requests are passed through to the underlying iterators.- Type Parameters:
E- the type of elements returned by the returned iterator- Parameters:
first- the first iterator to consult for elementssecond- the second iterator to consult for elements- Returns:
- an iterator that returns all remaining elements of
first, followed by all remaining elements ofsecond - Throws:
NullPointerException- iffirst == nullorsecond == null
-
flatten
Returns an iterator consisting of the combined elements of all iterators returned by a given iterator in order. Removal requests are passed through to the underlying iterators.- Type Parameters:
E- the type of elements returned by the returned iterator- Parameters:
iterators- the iterators to be combined- Returns:
- the combined interator
- Throws:
NullPointerException- ifiterators == nullor one in the list is==null.
-
flatMap
public static <E,F> Iterator<F> flatMap(Function<? super E, ? extends Iterator<? extends F>> fun, Iterator<E> things) Returns a concatenation of subsequences computed with a function.- Type Parameters:
E- the source element typeF- the target element type- Parameters:
fun- a function that computes a subsequence of target elements from a single source elementthings- the source elements- Returns:
- the concatenation of the subsequences obtained by applying
funto all source elements in order
-
filter
Returns an iterator consisting of the remaining elements of a given iterator that match the given predicate.Removal requests are passed through to the underlying iterator.
- Type Parameters:
E- the element type- Parameters:
pred- a predicate specifying which elements to includei- an iterator- Returns:
- an iterator consisting of those elements of
ifor whichpredreturnstrue - Throws:
NullPointerException- ifpredoriis null
-
map
Returns a view on an iterator that has all elements transformed by a given function.Changes to the backing iterator are reflected in the result.
Removal requests are delegated to the backing iterator.
- Type Parameters:
A- the source element typeB- the target element type- Parameters:
fun- a transformer functionit- some elements- Returns:
- an iterator where each element is computed on the fly by applying
funto the corresponding element ofit - Throws:
NullPointerException- iffunoritis null
-
unmodifiableIterator
Returns an unmodifiable view on an iterator.Changes to the underlying iterator are reflected in the result.
- Type Parameters:
A- the element type- Parameters:
i- an iterator- Returns:
- an iterator consisting of the same elements as
i, but not supporting theremoveoperation
-
multipleLookahead
public static <E> Iterators.MultipleLookaheadIterator<E> multipleLookahead(Iterator<? extends E> it) Returns an iterator with deep lookahead over the given elements.- Type Parameters:
E- the element type- Parameters:
it- the remaining elements- Returns:
- an iterator with deep lookahead over the given elements
-
multipleLookahead
public static <E> Iterators.MultipleLookaheadIterator<E> multipleLookahead(Iterable<? extends E> elems) Returns an iterator with deep lookahead over the given elements.- Type Parameters:
E- the element type- Parameters:
elems- the elements- Returns:
- an iterator with deep lookahead over the given elements
-
multipleLookahead
Returns an iterator with deep lookahead over the given elements.- Type Parameters:
E- the element type- Parameters:
elems- the elements- Returns:
- an iterator with deep lookahead over the given elements
-
lookahead
Returns an view with lookahead capability for a given iterator.Changes to the underlying iterator are reflected in the result.
Removal requests are passed through to the underlying iterator.
- Type Parameters:
A- the element type- Parameters:
things- the iterator- Returns:
- an iterator consisting of the same elements as
things, but with lookahead capability - See Also:
-
pushback
Returns an view with pushback capability for a given iterator.Changes to the underlying iterator are reflected in the result.
Removal requests are passed through to the underlying iterator.
- Type Parameters:
A- the element type- Parameters:
things- the iterator- Returns:
- an iterator consisting of the same elements as
things, but with pushback capability - See Also:
-
merge
public static <A> Iterator<A> merge(Comparator<? super A> order, boolean removeDuplicates, Iterator<? extends A> left, Iterator<? extends A> right) Returns the merged combination of two ordered sequences.- Type Parameters:
A- the element type- Parameters:
order- the ordering of both inputs and the resultremoveDuplicates- duplicates are removed iftrue, or retained iffalseleft- the first ordered sequence of elementsright- the second ordered sequence of elements- Returns:
- an ordered sequence that contains the elements of both inputs, possibly without duplicates
- Throws:
NullPointerException- iforder,leftorrightis null
-
filterWithConstraint
public static <A,B> Iterator<A> filterWithConstraint(BiPredicate<? super A, ? super B> rel, Iterator<A> things, Iterator<B> constraints) Returns an iterator consisting of the elements of another iterator that match a constraint from a parallel iterator.The elements of the given iterators
thingsandconstraintsare consumed in parallel, one element at a time. If one or both of them run out of elements, so does the resulting iterator.The pairs of successive elements are filtered through the given matching relation
rel. If a pair is accepted, then the corresponding element (fromthings) appears in the resulting iterator; the matched constraint is omitted. If a pair is not accepted, then it is omitted altogether.The resulting iterator does not support the operation
Iterator#remove().- Type Parameters:
A- the type of elementsB- the type of constraints- Parameters:
rel- the matching relation between elements and constraintsthings- the iterator of elementsconstraints- the iterator of constraints- Returns:
- an iterator that passes through elements that match their respective contstraint
-
cacheHasNext
Returns an iterator consisting of the same elements as the given iterator, but caching redundant queries.This is useful if a given iterator implementation performs an expensive computation on
Iterator.hasNext().- Type Parameters:
A- the element type- Parameters:
i- an iterator- Returns:
- an iterator consisting of the same elements, but which caches the
result of
hasNextbetween calls tonext
-
drop
Returns a view on an iterator that has the same remaining elements, except the first ones.Changes to the underlying iterator are reflected in the result.
Removal requests are passed through to the underlying iterator.
If more elements were to be dropped than available, the result is simply empty.
- Type Parameters:
A- the element type- Parameters:
n- the number of elements to discard from the start of the iterationthings- some elements- Returns:
- an iterator that has the same elements as
things, except fornmany that are discarded at the start of the iteration - Throws:
IllegalArgumentException- ifnis negativeNullPointerException- ifthingsis null- See Also:
-
take
Returns a view on an iterator that has the same elements, up to a given length.Changes to the underlying iterable are reflected in the result.
Removal requests are passed through to the underlying iterator.
If more elements were to be retained than available, the result is simply equivalent to the input.
- Type Parameters:
A- the element type- Parameters:
n- the number of elements to retain from the iterationthings- some elements- Returns:
- an iterator that has the same first elements as
things, but ends afternelements - Throws:
IllegalArgumentException- ifnis negativeNullPointerException- ifthingsis null- See Also:
-
firstOf
Returns the first remaining element, if available.- Type Parameters:
E- the element type- Parameters:
it- an iterator- Returns:
- an optional value that contains the first remaining (next) element if there is one, or the empty optional value if there are no more elements
-
anyOf
Returns an arbitrary remaining element, if available.- Type Parameters:
E- the element type- Parameters:
it- an iterator- Returns:
- an optional value that contains an arbitrarily chosen element if there is one, or the empty optional value if there are no elements
-
iterate
public static <E,S> Iterator<E> iterate(S seed, Predicate<? super S> hasNext, UnaryOperator<S> next, Function<? super S, ? extends E> view) Returns an iterator that produces elements by iterative application of the given step function to an initial state, conditioned on satisfying the given predicate, computing elements from states with the given view function. Iteration terminates as soon as thehasNextpredicate returnsfalse.The iterator returned by this method should produce the same sequence of elements as the corresponding for-loop:
for (T state = seed; hasNext.test(state); state = next.apply(state)) { final E elem = view.apply(state); ... }- Type Parameters:
E- the type of elementsS- the type of states- Parameters:
seed- the initial statehasNext- a predicate to determine before which state iteration should terminatenext- a function to produce the next stateview- a function to compute an element from a state- Returns:
- an iterator that produces elements iteratively
- Throws:
NullPointerException- ifcodeNext,nextorviewis null- Since:
- 1.2
- See Also:
-
iterate
Returns an iterator that produces elements by iterative application of the given step function to an initial element, conditioned on satisfying the given predicate. Iteration terminates as soon as thehasNextpredicate returnsfalse.The iterator returned by this method should produce the same sequence of elements as the corresponding for-loop:
for (T elem = seed; hasNext.test(elem); elem = next.apply(elem)) { ... }- Type Parameters:
S- the type of elements- Parameters:
seed- the initial elementhasNext- a predicate to determine before which element iteration should terminatenext- a function to produce the next element- Returns:
- an iterator that produces elements iteratively
- Throws:
NullPointerException- ifcodeNextornextis null- Since:
- 1.2
- See Also:
-
depthFirst
public static <V,L> Iterator<L> depthFirst(V root, Function<? super V, ? extends Iterator<? extends L>> labels, Function<? super V, ? extends Iterator<? extends V>> branches) Returns an iterator that produces the labels of a search tree by depth-first traversal.- Type Parameters:
V- the type of search tree nodesL- the type of labels- Parameters:
root- the root node of the search treelabels- a function to enumerate the labels of a nodebranches- a function to enumerate the children of a node- Returns:
- an enumeration of all labels encountered in depth-first search order
- Throws:
NullPointerException- iflabelsorbranchesis null- Since:
- 1.2
-
range
Returns an iterator that produces elements computed from the given integer interval in ascending order.The iterator returned by this method should produce the same sequence of elements as the corresponding for-loop:
for (int i = start; i < end; i++) final E elem = gen.apply(i); ... }- Type Parameters:
E- the type of elements- Parameters:
start- the start of the interval (inclusive)end- the end of the interval (exclusive)gen- a function to produce the element for an integer index- Returns:
- an iterator that produces elements computed from the interval
[
start,end) - Throws:
NullPointerException- ifgenis null- Since:
- 1.2
-
singleton
Returns an iterator consisting of exactly one given element computed on demand. The element is not removable.- Type Parameters:
E- the type of elements returned by the returned iterator- Parameters:
elem- a supplier that computes the single element of the returned iterator on demand- Returns:
- an iterator that has exactly the computed element
- Throws:
NullPointerException- ifelemis null- Since:
- 1.2
-
conditional
Returns an iterator that produces the given element or nothing, depending on a condition.- Type Parameters:
E- the type of elements- Parameters:
cond- a conditionelem- the element to produce conditionally- Returns:
- an iterator that produces just
elemifcondistrue; the empty iterator otherwise. - Since:
- 1.2
- See Also:
-
conditional
Returns an iterator that produces one element computed on the fly or nothing, depending on a condition.The given supplier is only used if the condition is
true.- Type Parameters:
E- the type of elements- Parameters:
cond- a conditionelem- a supplier for the element to produce conditionally- Returns:
- an iterator that produces just
elem.get()ifcondistrue; the empty iterator otherwise. - Throws:
NullPointerException- ifelemis null- Since:
- 1.2
- See Also:
-
conditional
Returns an iterator that produces the given elements or nothing, depending on a condition.The given iterator is only used if the condition is
true.This method is equivalent to
filter(Predicate<? super E>, Iterator<E>)with a constant predicate, but returns a more efficient iterator.- Type Parameters:
E- the type of elements- Parameters:
cond- a conditionthings- an iterator for the elements to produce conditionally- Returns:
- an iterator that forwards the elements of
thingsifcondistrue; the empty iterator otherwise. - Throws:
NullPointerException- ifthingsis null- Since:
- 1.2
- See Also:
-
forever
Returns an iterator that produces unboundedly many elements computed on demand.The
hasNextmethod of the returned iterator always returnstrue. Thenextmethod of the returned iterator does not actively throwNoSuchElementException, but it may throw any exception caused by the underlying supplier.The resulting iterator is not suitable for exhaustive consumption of all elements; such a loop does not terminate. Clients must stop consuming more elements by themselves, or iteration must be limited by other means.
- Type Parameters:
E- the type of elements returned by the returned iterator- Parameters:
things- a supplier that computes each element of the returned iterator on demand- Returns:
- an iterator that produces the computed elements
- Throws:
NullPointerException- ifthingsis null- Since:
- 1.2
- See Also:
-