java.lang.Object
org.apache.jena.atlas.iterator.Iter<T>
- Type Parameters:
T- the type of element over which an instance ofIteriterates,
- All Implemented Interfaces:
Iterator<T>,IteratorCloseable<T>,Closeable
Iter provides general utilities for working with Iterators.
This class provides functionality similar to
Stream
except for iterators (and hence single threaded).
Style 1: functional style using statics.
import static org.apache.jena.atlas.iterator.Iter.*; filter(map(iterator, function), predicate)Style 2: Stream-like: The class
Iter provides methods to call on an iterator.
import static org.apache.jena.atlas.iterator.Iter.iter; iter(iterator).map(...).filter(...)The operations attempt to close iterators - see
close(Iterator).
Caution - not all Iterators in this package provide close or passdown close operations
IteratorOnClose adds a Runnable action called once on close.
Iterators do not guarantee .remove.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionbooleanstatic <T> booleanReturn true if every element of stream passes the filter (reads the stream until the first element not passing the filter)booleanstatic <T> booleanReturn true if one or more elements of stream passes the filter (reads the stream to first element passing the filter)static <T> Iterator<T>Join two iterators.Join on anIterator..voidApply an action to every element of an iteratorstatic <T> voidAct on elements of an iterator.static <T> Stream<T>static <T> Stream<T>static <T> StringasString(Iterator<T> stream, CharSequence sep, CharSequence prefix, CharSequence suffix) Create a string from an iterator, using the separator, prefix and suffix.static <T> StringCreate a string from an iterator, using the separator.voidclose()static <T> voidClose iterator if marked withCloseable.<R> Rcollect(Supplier<R> supplier, BiConsumer<R, T> accumulator) SeeStream.collect(Supplier, BiConsumer, BiConsumer), except without theBiConsumer<R, R> combinerstatic <T,R> R collect(Iterator<T> iter, Supplier<R> supplier, BiConsumer<R, ? super T> accumulator) SeeStream.collect(Supplier, BiConsumer, BiConsumer), except without theBiConsumer<R, R> combinerstatic <T,R, A> R <R,A> R static <T> Iter<T>AnIteratorof 2Iterator's.static <T> Iter<T>AnIterof 2Iter'sstatic <T> voidConsume the iteratorlongcount()Count the iterator (this is destructive on the iterator)static <T> longCount the iterator (this is destructive on the iterator)static <T> Iterator<T>debug(PrintStream out, Iterator<T> stream) Print an iterator, return a copy of the iterator.static <T> Iterator<T>Print an iterator to stdout, return a copy of the iterator.distinct()Return anIterthat will see each element of the underlying iterator only once.static <T> Iterator<T>Return an iterator that will see each element of the underlying iterator only once.Remove adjacent duplicates.static <T> Iterator<T>distinctAdjacent(Iterator<T> iter) Remove adjacent duplicates.Create anItersuch that elements from the front until a predicate test become true are dropped then return all remaining elements are iterated over.static <T> Iterator<T>Create an iterator such that elements from the front until a predicate test become true are dropped then return all remaining elements are iterated over.Create anItersuch that elements from the front while a predicate test become true are dropped then return all remaining elements are iterated over.static <T> Iterator<T>Create an iterator such that elements from the front while a predicate test become true are dropped then return all remaining elements are iterated over.static <T> Iter<T>empty()Filter by predicatestatic <T> Iterator<T>static <T> Iterator<T>filterDrop(Iterator<? extends T> stream, Predicate<T> filter) static <T> Optional<T>Return an Optional with an element of an iterator that matches the predicate.static <T> Optional<T>Return an Optional with the first element of an iterator that matches the predicate.static <T> Optional<T>Return an Optional with the last element of an iterator that matches the predicate.first()Skip to the first element meeting a condition and return that element.static <T> TReturn the first element of an iterator or null if no such element.static <T> TSkip to the first element meeting a condition and return that element.intfirstIndex(Predicate<T> filter) Skip to the first element meeting a condition and return that element's index (zero-based).static <T> intfirstIndex(Iterator<T> iter, Predicate<T> filter) Skip to the first element meeting a condition and return that element's index (zero-based).<R> Iter<R>FlatMap each element using given function of element to iterator of mapped elements.sstatic <T,R> Iterator<R> Apply a function to every element of an iterator, to produce possibly multiple mapping each time.static <T,R> R foldLeft(Iterator<? extends T> stream, R value, Iter.Folder<T, R> function) <R> RfoldLeft(R initial, Iter.Folder<T, R> accumulator) static <T,R> R foldRight(Iterator<? extends T> stream, R value, Iter.Folder<T, R> function) <R> RfoldRight(R initial, Iter.Folder<T, R> accumulator) voidApply the Consumer to each element of the iteratorstatic <T> voidShorter form of "forEachRemaining"voidforEachRemaining(Consumer<? super T> action) booleanhasNext()static <T> Iter<T>iter(Collection<T> collection) static <T> Iter<T>static <T> Iter<T>static <T> Iterator<T>Create another iterator without risk of concurrent modification exceptions.last()static <T> TReturn the last element or null, if no elements.static <T> TReturn the last element satisfying a predicate.static <T> intFind the last occurrence, defined by a predicate, in a list.limit(long N) Limit the number of elements.static <X> Iterator<X>Return an iterator that is limited to the given number of elements.static <T> Iterator<T>log(PrintStream out, Iterator<T> stream) Print an iterator to stdout, return a copy of the iterator.static <T> Iterator<T>Print an iterator to stdout, return a copy of the iterator.<R> Iter<R>Map each element using given functionstatic <T,R> Iterator<R> Apply a function to every element of an iterator, transforming it from aTto anR.static <T> Iterator<T>materialize(Iterator<T> iter) Materialize an iterator, that is, force it to run now - useful in debugging or when iteration may modify underlying datastructures.max(Comparator<T> comparator) static <T> Optional<T>max(Iterator<T> iter, Comparator<T> comparator) min(Comparator<T> comparator) static <T> Optional<T>min(Iterator<T> iter, Comparator<T> comparator) next()booleanstatic <T> booleanReturn true if none of the elements of the iterator passes the predicate test reads the stream to first element passing the filter)static <T> Iterator<T>Return an iterator that does not permit remove.static <T> Iterator<T>Deprecated.static <T> Iter<T>nullIter()static <T> Iterator<T>static <T> Iter<T>of(T item) static <T> Iter<T>of(T... items) static <T> Iter<T>ofNullable(T t) static <T> IteratorOnClose<T>Run an action when an iterator is closed.static <T> IteratorCloseable<T>Run an action when an iterator is closed.Apply an action to everything in the stream, yielding a stream of the original items.static <T> Iterator<T>Apply an action to everything in stream, yielding a stream of the same items.static <T> voidprint(PrintStream out, Iterator<T> stream) Print an iterator (destructive)static <T> voidPrint an iterator (destructive)static <T> Iterator<T>printWrapper(PrintStream out, Iterator<? extends T> stream) Print an iterator as it gets used - this adds a printing wrapperstatic <T> Iterator<T>printWrapper(Iterator<? extends T> stream) Print an iterator as it gets used - this adds a printing wrapperreduce(BinaryOperator<T> accumulator) Reduce.static <T> Optional<T>reduce(Iterator<T> iter, BinaryOperator<T> accumulator) static <T> Treduce(Iterator<T> iter, T identity, BinaryOperator<T> accumulator) reduce(T identity, BinaryOperator<T> accumulator) voidremove()Remove nullsstatic <T> Iterator<T>removeNulls(Iterator<T> iter) Remove nulls from an iteratorstatic <T> Iterator<T>reverseIterate(List<T> list) reverse iterator for a liststatic <T> voidreverseIterate(List<T> list, Consumer<? super T> action) reverse for each on a list.static <T> voidsendToSink(Iterator<T> iter, Sink<T> sink) Send the elements of the iterator to a sink - consumes the iteratorvoidsendToSink(Sink<T> sink) static <T> Iterator<T>singleton(T item) Deprecated.static <T> Iter<T>singletonIter(T item) static <T> Iterator<T>singletonIterator(T item) skip(long N) Skip over a number of elements.static <X> Iterator<X>Skip over a number of elements of an iteratorstatic intStep forward up tostepsplaces.take(int N) Return an Iter that yields at most the first N itemsstatic <T> List<T>Take the first N elements of an iterator - stop early if too fewCreate anItersuch that it yields elements until a predicate test on the elements becomes true, end the iteration.static <T> Iterator<T>Create an iterator such that it yields elements until a predicate test on the elements becomes true, end the iteration.Create anItersuch that it yields elements while a predicate test on the elements is true, end the iteration.static <T> Iterator<T>Create an iterator such that it yields elements while a predicate test on the elements is true, end the iteration.toList()Consume theIterand produce aListstatic <T> List<T>Collect an iterator into a list.toSet()Consume theIterand produce aSetstatic <T> Set<T>Collect an iterator into a set.
-
Method Details
-
forEach
Shorter form of "forEachRemaining" -
asStream
-
asStream
-
singleton
Deprecated. -
singletonIterator
-
nullIterator
-
empty
-
of
-
of
-
ofNullable
-
noRemove
Return an iterator that does not permit remove. This makes an "UnmodifiableIterator". -
toSet
Collect an iterator into a set. -
toList
Collect an iterator into a list. -
iterator
Create another iterator without risk of concurrent modification exceptions. This materializes the input iterator. -
foldLeft
-
foldRight
-
reduce
-
reduce
-
min
-
max
-
collect
public static <T,R> R collect(Iterator<T> iter, Supplier<R> supplier, BiConsumer<R, ? super T> accumulator) SeeStream.collect(Supplier, BiConsumer, BiConsumer), except without theBiConsumer<R, R> combiner -
collect
-
apply
Act on elements of an iterator.- See Also:
-
filter
-
filterDrop
-
notFilter
@Deprecated public static <T> Iterator<T> notFilter(Iterator<? extends T> stream, Predicate<T> filter) -
allMatch
Return true if every element of stream passes the filter (reads the stream until the first element not passing the filter) -
anyMatch
Return true if one or more elements of stream passes the filter (reads the stream to first element passing the filter) -
noneMatch
Return true if none of the elements of the iterator passes the predicate test reads the stream to first element passing the filter) -
findFirst
Return an Optional with the first element of an iterator that matches the predicate. ReturnOptional.emptyif none match. Reads the iterator until the first match. -
findLast
Return an Optional with the last element of an iterator that matches the predicate. ReturnOptional.emptyif no match. Reads the iterator to completion. -
findAny
Return an Optional with an element of an iterator that matches the predicate. ReturnOptional.emptyif none match. The element returned is not specified by the API contract. -
map
Apply a function to every element of an iterator, transforming it from aTto anR. -
flatMap
Apply a function to every element of an iterator, to produce possibly multiple mapping each time. SeeStream.flatMap(java.util.function.Function<? super T, ? extends java.util.stream.Stream<? extends R>>) -
operate
Apply an action to everything in stream, yielding a stream of the same items. -
printWrapper
Print an iterator as it gets used - this adds a printing wrapper -
printWrapper
Print an iterator as it gets used - this adds a printing wrapper -
append
Join two iterators. If there, potentially, going to be many iterators, it is better to create anIteratorConcatexplicitly and add each iterator. -
distinct
Return an iterator that will see each element of the underlying iterator only once. Note that this need working memory to remember the elements already seen. -
distinctAdjacent
Remove adjacent duplicates. This operation does not need working memory to remember the all elements already seen, just a slot for the last element seen. -
removeNulls
Remove nulls from an iterator -
step
Step forward up tostepsplaces.
Return number of steps taken.- API Note:
- The iterator is moved at most
stepsplaces with no overshoot. The iterator can be used afterwards.
-
take
Take the first N elements of an iterator - stop early if too few- See Also:
-
takeWhile
Create an iterator such that it yields elements while a predicate test on the elements is true, end the iteration.- See Also:
-
takeUntil
Create an iterator such that it yields elements until a predicate test on the elements becomes true, end the iteration.- See Also:
-
dropWhile
Create an iterator such that elements from the front while a predicate test become true are dropped then return all remaining elements are iterated over. The first element where the predicted becomes true is the first element of the returned iterator. -
dropUntil
Create an iterator such that elements from the front until a predicate test become true are dropped then return all remaining elements are iterated over. The first element where the predicate becomes true is the first element of the returned iterator. -
limit
Return an iterator that is limited to the given number of elements. If it is shorter than the limit, stop at the end.- See Also:
-
skip
Skip over a number of elements of an iterator -
count
Count the iterator (this is destructive on the iterator) -
consume
Consume the iterator -
asString
Create a string from an iterator, using the separator. Note: this consumes the iterator. -
asString
public static <T> String asString(Iterator<T> stream, CharSequence sep, CharSequence prefix, CharSequence suffix) Create a string from an iterator, using the separator, prefix and suffix. Note: this consumes the iterator. -
close
Close iterator if marked withCloseable. -
onCloseIO
Run an action when an iterator is closed. This assumes the iterator closed withclose(java.util.Iterator<T>).Convenience function for closing a
Closeableafter use when passing an iterator out of scope. -
onClose
Run an action when an iterator is closed. This assumes the iterator closed withclose(java.util.Iterator<T>). -
log
Print an iterator to stdout, return a copy of the iterator. Printing occurs now. Seedebug(java.util.Iterator<T>)for an operation to print as the iterator is used. -
log
Print an iterator to stdout, return a copy of the iterator. Printing occurs now. Seedebug(java.util.Iterator<T>)for an operation to print as the iterator is used. -
debug
Print an iterator to stdout, return a copy of the iterator. Printing occurs when the iterator is used. Seelog(java.util.Iterator<T>)for an operation to print now. -
debug
Print an iterator, return a copy of the iterator. Printing occurs as the returned iterator is used. -
print
Print an iterator (destructive) -
print
Print an iterator (destructive) -
sendToSink
Send the elements of the iterator to a sink - consumes the iterator -
iter
-
iter
-
iter
-
singletonIter
-
nullIter
-
materialize
Materialize an iterator, that is, force it to run now - useful in debugging or when iteration may modify underlying datastructures. -
concat
AnIterof 2Iter's -
concat
-
first
Return the first element of an iterator or null if no such element.- Parameters:
iter-- Returns:
- An item or null.
-
first
Skip to the first element meeting a condition and return that element. -
firstIndex
Skip to the first element meeting a condition and return that element's index (zero-based). -
last
Return the last element or null, if no elements. This operation consumes the iterator. -
last
Return the last element satisfying a predicate. This operation consumes the whole iterator. -
lastIndex
Find the last occurrence, defined by a predicate, in a list. -
reverseIterate
reverse iterator for a list -
reverseIterate
reverse for each on a list. -
close
public void close() -
forEach
Apply the Consumer to each element of the iterator -
forEachRemaining
- Specified by:
forEachRemainingin interfaceIterator<T>
-
toSet
Consume theIterand produce aSet -
toList
Consume theIterand produce aList -
sendToSink
-
first
-
last
-
first
Skip to the first element meeting a condition and return that element. -
firstIndex
Skip to the first element meeting a condition and return that element's index (zero-based). -
filter
Filter by predicate -
allMatch
-
anyMatch
-
noneMatch
-
findFirst
-
findAny
-
findLast
-
removeNulls
Remove nulls -
map
Map each element using given function -
flatMap
FlatMap each element using given function of element to iterator of mapped elements.s -
operate
Apply an action to everything in the stream, yielding a stream of the original items. -
foldLeft
-
foldRight
-
reduce
Reduce. This reduce is fold-left (take first element, apply to rest of list) -
reduce
-
min
-
max
-
collect
SeeStream.collect(Supplier, BiConsumer, BiConsumer), except without theBiConsumer<R, R> combiner -
collect
-
apply
Apply an action to every element of an iterator -
append
Join on anIterator.. If there are going to be many iterators, it is better to create anIteratorConcatand.addeach iterator. The overheads are much lower. -
take
Return an Iter that yields at most the first N items -
takeWhile
Create anItersuch that it yields elements while a predicate test on the elements is true, end the iteration.- See Also:
-
takeUntil
Create anItersuch that it yields elements until a predicate test on the elements becomes true, end the iteration.- See Also:
-
dropWhile
Create anItersuch that elements from the front while a predicate test become true are dropped then return all remaining elements are iterated over. The first element where the predicted becomes true is the first element of the returned iterator. -
dropUntil
Create anItersuch that elements from the front until a predicate test become true are dropped then return all remaining elements are iterated over. The first element where the predicate becomes true is the first element of the returned iterator. -
limit
Limit the number of elements. -
skip
Skip over a number of elements. -
count
public long count()Count the iterator (this is destructive on the iterator) -
distinct
Return anIterthat will see each element of the underlying iterator only once. Note that this need working memory to remember the elements already seen. -
distinctAdjacent
Remove adjacent duplicates. This operation does not need working memory to remember the all elements already seen, just a slot for the last element seen. -
hasNext
public boolean hasNext() -
next
-
remove
public void remove()
-
filterDrop(java.util.Iterator<? extends T>, java.util.function.Predicate<T>)