Modifier and Type | Class and Description |
---|---|
static interface |
Iterators.ElementsSupplierStateHandler<E>
An instance of this is passed to the elements supplier at the time of
calculating the next element.
|
Modifier and Type | Method and Description |
---|---|
static <E> PeekableIterator<E> |
iterator(Function<Iterators.ElementsSupplierStateHandler<E>,E> elementsSupplier)
Wraps
elementsSupplier into a PeekableIterator . |
static <E> PeekableIterator<E> |
peekableIterator(Iterator<E> iterator)
Wraps
iterator into a PeekableIterator . |
static <E> Stream<E> |
stream(Function<Iterators.ElementsSupplierStateHandler<E>,E> elementsSupplier)
Returns a sequential
Stream with the elementsSupplier as its
source. |
static <E> Stream<E> |
stream(Iterator<E> iterator)
Returns a sequential
Stream with the iterator as its source. |
public static <E> PeekableIterator<E> iterator(Function<Iterators.ElementsSupplierStateHandler<E>,E> elementsSupplier)
elementsSupplier
into a PeekableIterator
. This is
probably the simplest way of writing an iterator.
Note: This iterator is not thread safe.
Usage example: The following shows how to create an iterator returning every second element only.
Iterator<E> oldIterator = ...; Iterator<E> newIterator = Iterators.iterator(state -> { if (!oldIterator.hasNext() && !oldIterator.hasNext()) { return state.endOfData(); } return oldIterator.next(); });
E
- the type of the iterator elementselementsSupplier
- the elements supplier with the state as parameterPeekableIterator
wrapping elementsSupplier
public static <E> PeekableIterator<E> peekableIterator(Iterator<E> iterator)
E
- the type of the iterator elementsiterator
- the iteratorPeekableIterator
wrapping iterator
public static <E> Stream<E> stream(Function<Iterators.ElementsSupplierStateHandler<E>,E> elementsSupplier)
Stream
with the elementsSupplier
as its
source.
Check out iterator(Function)
for more information about
elementsSupplier
.
E
- the type of the iterator elementselementsSupplier
- the elements supplier with the state as parameterStream
with the elements of elementsSupplier
Copyright © 2025. All rights reserved.