Class Iter<T>

java.lang.Object
org.apache.jena.atlas.iterator.Iter<T>
Type Parameters:
T - the type of element over which an instance of Iter iterates,
All Implemented Interfaces:
Iterator<T>, IteratorCloseable<T>, Closeable

public class Iter<T> extends Object implements IteratorCloseable<T>
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.

  • Method Details

    • forEach

      public static <T> void forEach(Iterator<T> iter, Consumer<T> action)
      Shorter form of "forEachRemaining"
    • asStream

      public static <T> Stream<T> asStream(Iterator<T> iterator)
    • asStream

      public static <T> Stream<T> asStream(Iterator<T> iterator, boolean parallel)
    • singleton

      @Deprecated public static <T> Iterator<T> singleton(T item)
      Deprecated.
    • singletonIterator

      public static <T> Iterator<T> singletonIterator(T item)
    • nullIterator

      public static <T> Iterator<T> nullIterator()
    • empty

      public static <T> Iter<T> empty()
    • of

      public static <T> Iter<T> of(T item)
    • of

      @SafeVarargs public static <T> Iter<T> of(T... items)
    • ofNullable

      public static <T> Iter<T> ofNullable(T t)
    • noRemove

      public static <T> Iterator<T> noRemove(Iterator<T> iter)
      Return an iterator that does not permit remove. This makes an "UnmodifiableIterator".
    • toSet

      public static <T> Set<T> toSet(Iterator<? extends T> stream)
      Collect an iterator into a set.
    • toList

      public static <T> List<T> toList(Iterator<? extends T> stream)
      Collect an iterator into a list.
    • iterator

      public static <T> Iterator<T> iterator(Iterator<? extends T> iterator)
      Create another iterator without risk of concurrent modification exceptions. This materializes the input iterator.
    • foldLeft

      public static <T, R> R foldLeft(Iterator<? extends T> stream, R value, Iter.Folder<T,R> function)
    • foldRight

      public static <T, R> R foldRight(Iterator<? extends T> stream, R value, Iter.Folder<T,R> function)
    • reduce

      public static <T> Optional<T> reduce(Iterator<T> iter, BinaryOperator<T> accumulator)
    • reduce

      public static <T> T reduce(Iterator<T> iter, T identity, BinaryOperator<T> accumulator)
    • min

      public static <T> Optional<T> min(Iterator<T> iter, Comparator<T> comparator)
    • max

      public static <T> Optional<T> max(Iterator<T> iter, Comparator<T> comparator)
    • collect

      public static <T, R> R collect(Iterator<T> iter, Supplier<R> supplier, BiConsumer<R,? super T> accumulator)
      See Stream.collect(Supplier, BiConsumer, BiConsumer), except without the BiConsumer<R, R> combiner
    • collect

      public static <T, R, A> R collect(Iterator<T> iter, Collector<? super T,A,R> collector)
    • apply

      public static <T> void apply(Iterator<? extends T> stream, Consumer<T> action)
      Act on elements of an iterator.
      See Also:
    • filter

      public static <T> Iterator<T> filter(Iterator<? extends T> stream, Predicate<T> filter)
    • filterDrop

      public static <T> Iterator<T> filterDrop(Iterator<? extends T> stream, Predicate<T> filter)
    • notFilter

      @Deprecated public static <T> Iterator<T> notFilter(Iterator<? extends T> stream, Predicate<T> filter)
    • allMatch

      public static <T> boolean allMatch(Iterator<T> iter, Predicate<? super T> predicate)
      Return true if every element of stream passes the filter (reads the stream until the first element not passing the filter)
    • anyMatch

      public static <T> boolean anyMatch(Iterator<T> iter, Predicate<? super T> predicate)
      Return true if one or more elements of stream passes the filter (reads the stream to first element passing the filter)
    • noneMatch

      public static <T> boolean noneMatch(Iterator<T> iter, Predicate<? super T> predicate)
      Return true if none of the elements of the iterator passes the predicate test reads the stream to first element passing the filter)
    • findFirst

      public static <T> Optional<T> findFirst(Iterator<T> iter, Predicate<? super T> predicate)
      Return an Optional with the first element of an iterator that matches the predicate. Return Optional.empty if none match. Reads the iterator until the first match.
    • findLast

      public static <T> Optional<T> findLast(Iterator<T> iter, Predicate<? super T> predicate)
      Return an Optional with the last element of an iterator that matches the predicate. Return Optional.empty if no match. Reads the iterator to completion.
    • findAny

      public static <T> Optional<T> findAny(Iterator<T> iter, Predicate<? super T> predicate)
      Return an Optional with an element of an iterator that matches the predicate. Return Optional.empty if none match. The element returned is not specified by the API contract.
    • map

      public static <T, R> Iterator<R> map(Iterator<? extends T> stream, Function<T,R> converter)
      Apply a function to every element of an iterator, transforming it from a T to an R.
    • flatMap

      public static <T, R> Iterator<R> flatMap(Iterator<T> iter, Function<T,Iterator<R>> mapper)
      Apply a function to every element of an iterator, to produce possibly multiple mapping each time. See Stream.flatMap(java.util.function.Function<? super T, ? extends java.util.stream.Stream<? extends R>>)
    • operate

      public static <T> Iterator<T> operate(Iterator<? extends T> stream, Consumer<T> action)
      Apply an action to everything in stream, yielding a stream of the same items.
    • printWrapper

      public static <T> Iterator<T> printWrapper(Iterator<? extends T> stream)
      Print an iterator as it gets used - this adds a printing wrapper
    • printWrapper

      public static <T> Iterator<T> printWrapper(PrintStream out, Iterator<? extends T> stream)
      Print an iterator as it gets used - this adds a printing wrapper
    • append

      public static <T> Iterator<T> append(Iterator<? extends T> iter1, Iterator<? extends T> iter2)
      Join two iterators. If there, potentially, going to be many iterators, it is better to create an IteratorConcat explicitly and add each iterator.
    • distinct

      public static <T> Iterator<T> distinct(Iterator<T> iter)
      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

      public static <T> Iterator<T> distinctAdjacent(Iterator<T> iter)
      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

      public static <T> Iterator<T> removeNulls(Iterator<T> iter)
      Remove nulls from an iterator
    • step

      public static int step(Iterator<?> iter, int steps)
      Step forward up to steps places.
      Return number of steps taken.
      API Note:
      The iterator is moved at most steps places with no overshoot. The iterator can be used afterwards.
    • take

      public static <T> List<T> take(Iterator<T> iter, int N)
      Take the first N elements of an iterator - stop early if too few
      See Also:
    • takeWhile

      public static <T> Iterator<T> takeWhile(Iterator<T> iter, Predicate<T> predicate)
      Create an iterator such that it yields elements while a predicate test on the elements is true, end the iteration.
      See Also:
    • takeUntil

      public static <T> Iterator<T> takeUntil(Iterator<T> iter, Predicate<T> predicate)
      Create an iterator such that it yields elements until a predicate test on the elements becomes true, end the iteration.
      See Also:
    • dropWhile

      public static <T> Iterator<T> dropWhile(Iterator<T> iter, Predicate<T> predicate)
      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

      public static <T> Iterator<T> dropUntil(Iterator<T> iter, Predicate<T> predicate)
      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

      public static <X> Iterator<X> limit(Iterator<X> iterator, long 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

      public static <X> Iterator<X> skip(Iterator<X> iterator, long limit)
      Skip over a number of elements of an iterator
    • count

      public static <T> long count(Iterator<T> iterator)
      Count the iterator (this is destructive on the iterator)
    • consume

      public static <T> void consume(Iterator<T> iterator)
      Consume the iterator
    • asString

      public static <T> String asString(Iterator<T> stream, String sep)
      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

      public static <T> void close(Iterator<T> iter)
      Close iterator if marked with Closeable.
    • onCloseIO

      public static <T> IteratorCloseable<T> onCloseIO(Iterator<T> iterator, Closeable ioThing)
      Run an action when an iterator is closed. This assumes the iterator closed with close(java.util.Iterator<T>).

      Convenience function for closing a Closeable after use when passing an iterator out of scope.

    • onClose

      public static <T> IteratorOnClose<T> onClose(Iterator<T> iter, Runnable closeHandler)
      Run an action when an iterator is closed. This assumes the iterator closed with close(java.util.Iterator<T>).
    • log

      public static <T> Iterator<T> log(Iterator<T> stream)
      Print an iterator to stdout, return a copy of the iterator. Printing occurs now. See debug(java.util.Iterator<T>) for an operation to print as the iterator is used.
    • log

      public static <T> Iterator<T> log(PrintStream out, Iterator<T> stream)
      Print an iterator to stdout, return a copy of the iterator. Printing occurs now. See debug(java.util.Iterator<T>) for an operation to print as the iterator is used.
    • debug

      public static <T> Iterator<T> debug(Iterator<T> stream)
      Print an iterator to stdout, return a copy of the iterator. Printing occurs when the iterator is used. See log(java.util.Iterator<T>) for an operation to print now.
    • debug

      public static <T> Iterator<T> debug(PrintStream out, Iterator<T> stream)
      Print an iterator, return a copy of the iterator. Printing occurs as the returned iterator is used.
    • print

      public static <T> void print(Iterator<T> stream)
      Print an iterator (destructive)
    • print

      public static <T> void print(PrintStream out, Iterator<T> stream)
      Print an iterator (destructive)
    • sendToSink

      public static <T> void sendToSink(Iterator<T> iter, Sink<T> sink)
      Send the elements of the iterator to a sink - consumes the iterator
    • iter

      public static <T> Iter<T> iter(Iter<T> iter)
    • iter

      public static <T> Iter<T> iter(Collection<T> collection)
    • iter

      public static <T> Iter<T> iter(Iterator<T> iterator)
    • singletonIter

      public static <T> Iter<T> singletonIter(T item)
    • nullIter

      public static <T> Iter<T> nullIter()
    • materialize

      public 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.
    • concat

      public static <T> Iter<T> concat(Iter<T> iter1, Iter<T> iter2)
      An Iter of 2 Iter's
    • concat

      public static <T> Iter<T> concat(Iterator<T> iter1, Iterator<T> iter2)
      An Iterator of 2 Iterator's. See also IteratorConcat.
    • first

      public static <T> T first(Iterator<T> iter)
      Return the first element of an iterator or null if no such element.
      Parameters:
      iter -
      Returns:
      An item or null.
    • first

      public static <T> T first(Iterator<T> iter, Predicate<T> filter)
      Skip to the first element meeting a condition and return that element.
    • firstIndex

      public static <T> int firstIndex(Iterator<T> iter, Predicate<T> filter)
      Skip to the first element meeting a condition and return that element's index (zero-based).
    • last

      public static <T> T last(Iterator<T> iter)
      Return the last element or null, if no elements. This operation consumes the iterator.
    • last

      public static <T> T last(Iterator<T> iter, Predicate<? super T> filter)
      Return the last element satisfying a predicate. This operation consumes the whole iterator.
    • lastIndex

      public static <T> int lastIndex(List<T> list, Predicate<? super T> filter)
      Find the last occurrence, defined by a predicate, in a list.
    • reverseIterate

      public static <T> Iterator<T> reverseIterate(List<T> list)
      reverse iterator for a list
    • reverseIterate

      public static <T> void reverseIterate(List<T> list, Consumer<? super T> action)
      reverse for each on a list.
    • close

      public void close()
      Specified by:
      close in interface Closeable
    • forEach

      public void forEach(Consumer<T> action)
      Apply the Consumer to each element of the iterator
    • forEachRemaining

      public void forEachRemaining(Consumer<? super T> action)
      Specified by:
      forEachRemaining in interface Iterator<T>
    • toSet

      public Set<T> toSet()
      Consume the Iter and produce a Set
    • toList

      public List<T> toList()
      Consume the Iter and produce a List
    • sendToSink

      public void sendToSink(Sink<T> sink)
    • first

      public T first()
    • last

      public T last()
    • first

      public T first(Predicate<T> filter)
      Skip to the first element meeting a condition and return that element.
    • firstIndex

      public int firstIndex(Predicate<T> filter)
      Skip to the first element meeting a condition and return that element's index (zero-based).
    • filter

      public Iter<T> filter(Predicate<T> filter)
      Filter by predicate
    • allMatch

      public boolean allMatch(Predicate<? super T> predicate)
    • anyMatch

      public boolean anyMatch(Predicate<? super T> predicate)
    • noneMatch

      public boolean noneMatch(Predicate<? super T> predicate)
    • findFirst

      public Optional<T> findFirst(Predicate<? super T> predicate)
    • findAny

      public Optional<T> findAny(Predicate<? super T> predicate)
    • findLast

      public Optional<T> findLast(Predicate<? super T> predicate)
    • removeNulls

      public Iter<T> removeNulls()
      Remove nulls
    • map

      public <R> Iter<R> map(Function<T,R> converter)
      Map each element using given function
    • flatMap

      public <R> Iter<R> flatMap(Function<T,Iterator<R>> converter)
      FlatMap each element using given function of element to iterator of mapped elements.s
    • operate

      public Iter<T> operate(Consumer<T> action)
      Apply an action to everything in the stream, yielding a stream of the original items.
    • foldLeft

      public <R> R foldLeft(R initial, Iter.Folder<T,R> accumulator)
    • foldRight

      public <R> R foldRight(R initial, Iter.Folder<T,R> accumulator)
    • reduce

      public Optional<T> reduce(BinaryOperator<T> accumulator)
      Reduce. This reduce is fold-left (take first element, apply to rest of list)
    • reduce

      public T reduce(T identity, BinaryOperator<T> accumulator)
    • min

      public Optional<T> min(Comparator<T> comparator)
    • max

      public Optional<T> max(Comparator<T> comparator)
    • collect

      public <R> R collect(Supplier<R> supplier, BiConsumer<R,T> accumulator)
      See Stream.collect(Supplier, BiConsumer, BiConsumer), except without the BiConsumer<R, R> combiner
    • collect

      public <R, A> R collect(Collector<? super T,A,R> collector)
    • apply

      public void apply(Consumer<T> action)
      Apply an action to every element of an iterator
    • append

      public Iter<T> append(Iterator<T> iter)
      Join on an Iterator.. If there are going to be many iterators, it is better to create an IteratorConcat and .add each iterator. The overheads are much lower.
    • take

      public Iter<T> take(int N)
      Return an Iter that yields at most the first N items
    • takeWhile

      public Iter<T> takeWhile(Predicate<T> predicate)
      Create an Iter such that it yields elements while a predicate test on the elements is true, end the iteration.
      See Also:
    • takeUntil

      public Iter<T> takeUntil(Predicate<T> predicate)
      Create an Iter such that it yields elements until a predicate test on the elements becomes true, end the iteration.
      See Also:
    • dropWhile

      public Iter<T> dropWhile(Predicate<T> predicate)
      Create an Iter 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

      public Iter<T> dropUntil(Predicate<T> predicate)
      Create an Iter 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

      public Iter<T> limit(long N)
      Limit the number of elements.
    • skip

      public Iter<T> skip(long N)
      Skip over a number of elements.
    • count

      public long count()
      Count the iterator (this is destructive on the iterator)
    • distinct

      public Iter<T> distinct()
      Return an Iter that will see each element of the underlying iterator only once. Note that this need working memory to remember the elements already seen.
    • distinctAdjacent

      public Iter<T> 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()
      Specified by:
      hasNext in interface Iterator<T>
    • next

      public T next()
      Specified by:
      next in interface Iterator<T>
    • remove

      public void remove()
      Specified by:
      remove in interface Iterator<T>