Class Iter<T>

  • Type Parameters:
    T - the type of element over which an instance of Iter iterates,
    All Implemented Interfaces:
    java.util.Iterator<T>, IteratorCloseable<T>, Closeable

    public class Iter<T>
    extends java.lang.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.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Iter.Folder<X,​Y>  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean allMatch​(java.util.function.Predicate<? super T> predicate)  
      static <T> boolean allMatch​(java.util.Iterator<T> iter, java.util.function.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)
      boolean anyMatch​(java.util.function.Predicate<? super T> predicate)  
      static <T> boolean anyMatch​(java.util.Iterator<T> iter, java.util.function.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)
      static <T> java.util.Iterator<T> append​(java.util.Iterator<? extends T> iter1, java.util.Iterator<? extends T> iter2)
      Join two iterators.
      Iter<T> append​(java.util.Iterator<T> iter)
      Join on an Iterator..
      void apply​(java.util.function.Consumer<T> action)
      Apply an action to every element of an iterator
      static <T> void apply​(java.util.Iterator<? extends T> stream, java.util.function.Consumer<T> action)
      Act on elements of an iterator.
      static <T> java.util.stream.Stream<T> asStream​(java.util.Iterator<T> iterator)  
      static <T> java.util.stream.Stream<T> asStream​(java.util.Iterator<T> iterator, boolean parallel)  
      static <T> java.lang.String asString​(java.util.Iterator<T> stream, java.lang.CharSequence sep, java.lang.CharSequence prefix, java.lang.CharSequence suffix)
      Create a string from an iterator, using the separator, prefix and suffix.
      static <T> java.lang.String asString​(java.util.Iterator<T> stream, java.lang.String sep)
      Create a string from an iterator, using the separator.
      void close()  
      static <T> void close​(java.util.Iterator<T> iter)
      Close iterator if marked with Closeable.
      <R> R collect​(java.util.function.Supplier<R> supplier, java.util.function.BiConsumer<R,​T> accumulator)
      See Stream.collect(Supplier, BiConsumer, BiConsumer), except without the BiConsumer<R, R> combiner
      static <T,​R>
      R
      collect​(java.util.Iterator<T> iter, java.util.function.Supplier<R> supplier, java.util.function.BiConsumer<R,​? super T> accumulator)
      See Stream.collect(Supplier, BiConsumer, BiConsumer), except without the BiConsumer<R, R> combiner
      static <T,​R,​A>
      R
      collect​(java.util.Iterator<T> iter, java.util.stream.Collector<? super T,​A,​R> collector)
      See Stream.collect(Collector)
      <R,​A>
      R
      collect​(java.util.stream.Collector<? super T,​A,​R> collector)
      See Stream.collect(Collector)
      static <T> Iter<T> concat​(java.util.Iterator<T> iter1, java.util.Iterator<T> iter2)
      An Iterator of 2 Iterator's.
      static <T> Iter<T> concat​(Iter<T> iter1, Iter<T> iter2)
      An Iter of 2 Iter's
      static <T> void consume​(java.util.Iterator<T> iterator)
      Consume the iterator
      long count()
      Count the iterator (this is destructive on the iterator)
      static <T> long count​(java.util.Iterator<T> iterator)
      Count the iterator (this is destructive on the iterator)
      static <T> java.util.Iterator<T> debug​(java.io.PrintStream out, java.util.Iterator<T> stream)
      Print an iterator, return a copy of the iterator.
      static <T> java.util.Iterator<T> debug​(java.util.Iterator<T> stream)
      Print an iterator to stdout, return a copy of the iterator.
      Iter<T> distinct()
      Return an Iter that will see each element of the underlying iterator only once.
      static <T> java.util.Iterator<T> distinct​(java.util.Iterator<T> iter)
      Return an iterator that will see each element of the underlying iterator only once.
      Iter<T> distinctAdjacent()
      Remove adjacent duplicates.
      static <T> java.util.Iterator<T> distinctAdjacent​(java.util.Iterator<T> iter)
      Remove adjacent duplicates.
      Iter<T> dropUntil​(java.util.function.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.
      static <T> java.util.Iterator<T> dropUntil​(java.util.Iterator<T> iter, java.util.function.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.
      Iter<T> dropWhile​(java.util.function.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.
      static <T> java.util.Iterator<T> dropWhile​(java.util.Iterator<T> iter, java.util.function.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.
      static <T> Iter<T> empty()  
      Iter<T> filter​(java.util.function.Predicate<T> filter)
      Filter by predicate
      static <T> java.util.Iterator<T> filter​(java.util.Iterator<? extends T> stream, java.util.function.Predicate<T> filter)  
      java.util.Optional<T> findAny​(java.util.function.Predicate<? super T> predicate)  
      static <T> java.util.Optional<T> findAny​(java.util.Iterator<T> iter, java.util.function.Predicate<? super T> predicate)
      Return an Optional with an element of an iterator that matches the predicate.
      java.util.Optional<T> findFirst​(java.util.function.Predicate<? super T> predicate)  
      static <T> java.util.Optional<T> findFirst​(java.util.Iterator<T> iter, java.util.function.Predicate<? super T> predicate)
      Return an Optional with the first element of an iterator that matches the predicate.
      java.util.Optional<T> findLast​(java.util.function.Predicate<? super T> predicate)  
      static <T> java.util.Optional<T> findLast​(java.util.Iterator<T> iter, java.util.function.Predicate<? super T> predicate)
      Return an Optional with the last element of an iterator that matches the predicate.
      T first()  
      T first​(java.util.function.Predicate<T> filter)
      Skip to the first element meeting a condition and return that element.
      static <T> T first​(java.util.Iterator<T> iter)
      Return the first element of an iterator or null if no such element.
      static <T> T first​(java.util.Iterator<T> iter, java.util.function.Predicate<T> filter)
      Skip to the first element meeting a condition and return that element.
      int firstIndex​(java.util.function.Predicate<T> filter)
      Skip to the first element meeting a condition and return that element's index (zero-based).
      static <T> int firstIndex​(java.util.Iterator<T> iter, java.util.function.Predicate<T> filter)
      Skip to the first element meeting a condition and return that element's index (zero-based).
      <R> Iter<R> flatMap​(java.util.function.Function<T,​java.util.Iterator<R>> converter)
      FlatMap each element using given function of element to iterator of mapped elements.s
      static <T,​R>
      java.util.Iterator<R>
      flatMap​(java.util.Iterator<T> iter, java.util.function.Function<T,​java.util.Iterator<R>> mapper)
      Apply a function to every element of an iterator, to produce possibly multiple mapping each time.
      static <T,​R>
      R
      foldLeft​(java.util.Iterator<? extends T> stream, R value, Iter.Folder<T,​R> function)  
      <R> R foldLeft​(R initial, Iter.Folder<T,​R> accumulator)  
      static <T,​R>
      R
      foldRight​(java.util.Iterator<? extends T> stream, R value, Iter.Folder<T,​R> function)  
      <R> R foldRight​(R initial, Iter.Folder<T,​R> accumulator)  
      void forEach​(java.util.function.Consumer<T> action)
      Apply the Consumer to each element of the iterator
      static <T> void forEach​(java.util.Iterator<T> iter, java.util.function.Consumer<T> action)
      Shorter form of "forEachRemaining"
      boolean hasNext()  
      static <T> Iter<T> iter​(java.util.Collection<T> collection)  
      static <T> Iter<T> iter​(java.util.Iterator<T> iterator)  
      static <T> Iter<T> iter​(Iter<T> iter)  
      static <T> java.util.Iterator<T> iterator​(java.util.Iterator<? extends T> iterator)
      Create another iterator without risk of concurrent modification exceptions.
      T last()  
      static <T> T last​(java.util.Iterator<T> iter)
      Return the last element or null, if no elements.
      static <T> T last​(java.util.Iterator<T> iter, java.util.function.Predicate<? super T> filter)
      Return the last element satisfying a predicate.
      static <T> int lastIndex​(java.util.List<T> list, java.util.function.Predicate<? super T> filter)
      Find the last occurrence, defined by a predicate, in a list.
      Iter<T> limit​(long N)
      Limit the number of elements.
      static <X> java.util.Iterator<X> limit​(java.util.Iterator<X> iterator, long limit)
      Return an iterator that is limited to the given number of elements.
      static <T> java.util.Iterator<T> log​(java.io.PrintStream out, java.util.Iterator<T> stream)
      Print an iterator to stdout, return a copy of the iterator.
      static <T> java.util.Iterator<T> log​(java.util.Iterator<T> stream)
      Print an iterator to stdout, return a copy of the iterator.
      <R> Iter<R> map​(java.util.function.Function<T,​R> converter)
      Map each element using given function
      static <T,​R>
      java.util.Iterator<R>
      map​(java.util.Iterator<? extends T> stream, java.util.function.Function<T,​R> converter)
      Apply a function to every element of an iterator, transforming it from a T to an R.
      static <T> java.util.Iterator<T> materialize​(java.util.Iterator<T> iter)
      Materialize an iterator, that is, force it to run now - useful in debugging or when iteration may modify underlying datastructures.
      java.util.Optional<T> max​(java.util.Comparator<T> comparator)  
      static <T> java.util.Optional<T> max​(java.util.Iterator<T> iter, java.util.Comparator<T> comparator)  
      java.util.Optional<T> min​(java.util.Comparator<T> comparator)  
      static <T> java.util.Optional<T> min​(java.util.Iterator<T> iter, java.util.Comparator<T> comparator)  
      T next()  
      boolean noneMatch​(java.util.function.Predicate<? super T> predicate)  
      static <T> boolean noneMatch​(java.util.Iterator<T> iter, java.util.function.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)
      static <T> java.util.Iterator<T> noRemove​(java.util.Iterator<T> iter)
      Return an iterator that does not permit remove.
      static <T> java.util.Iterator<T> notFilter​(java.util.Iterator<? extends T> stream, java.util.function.Predicate<T> filter)  
      static <T> Iter<T> nullIter()  
      static <T> java.util.Iterator<T> nullIterator()  
      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> onClose​(java.util.Iterator<T> iter, java.lang.Runnable closeHandler)
      Run an action when an iterator is closed.
      static <T> IteratorCloseable<T> onCloseIO​(java.util.Iterator<T> iterator, java.io.Closeable ioThing)
      Run an action when an iterator is closed.
      Iter<T> operate​(java.util.function.Consumer<T> action)
      Apply an action to everything in the stream, yielding a stream of the original items.
      static <T> java.util.Iterator<T> operate​(java.util.Iterator<? extends T> stream, java.util.function.Consumer<T> action)
      Apply an action to everything in stream, yielding a stream of the same items.
      static <T> void print​(java.io.PrintStream out, java.util.Iterator<T> stream)
      Print an iterator (destructive)
      static <T> void print​(java.util.Iterator<T> stream)
      Print an iterator (destructive)
      static <T> java.util.Iterator<T> printWrapper​(java.io.PrintStream out, java.util.Iterator<? extends T> stream)
      Print an iterator as it gets used - this adds a printing wrapper
      static <T> java.util.Iterator<T> printWrapper​(java.util.Iterator<? extends T> stream)
      Print an iterator as it gets used - this adds a printing wrapper
      java.util.Optional<T> reduce​(java.util.function.BinaryOperator<T> accumulator)
      Reduce.
      static <T> java.util.Optional<T> reduce​(java.util.Iterator<T> iter, java.util.function.BinaryOperator<T> accumulator)  
      static <T> T reduce​(java.util.Iterator<T> iter, T identity, java.util.function.BinaryOperator<T> accumulator)  
      T reduce​(T identity, java.util.function.BinaryOperator<T> accumulator)  
      void remove()  
      Iter<T> removeNulls()
      Remove nulls
      static <T> java.util.Iterator<T> removeNulls​(java.util.Iterator<T> iter)
      Remove nulls from an iterator
      static <T> void sendToSink​(java.util.Iterator<T> iter, Sink<T> sink)
      Send the elements of the iterator to a sink - consumes the iterator
      void sendToSink​(Sink<T> sink)  
      static <T> java.util.Iterator<T> singleton​(T item)  
      static <T> Iter<T> singletonIter​(T item)  
      Iter<T> skip​(long N)
      Skip over a number of elements.
      static <X> java.util.Iterator<X> skip​(java.util.Iterator<X> iterator, long limit)
      Skip over a number of elements of an iterator
      static int step​(java.util.Iterator<?> iter, int steps)
      Step forward up to steps places.
      Iter<T> take​(int N)
      Return an Iter that yields at most the first N items
      static <T> java.util.List<T> take​(java.util.Iterator<T> iter, int N)
      Take the first N elements of an iterator - stop early if too few
      Iter<T> takeUntil​(java.util.function.Predicate<T> predicate)
      Create an Iter such that it yields elements until a predicate test on the elements becomes true, end the iteration.
      static <T> java.util.Iterator<T> takeUntil​(java.util.Iterator<T> iter, java.util.function.Predicate<T> predicate)
      Create an iterator such that it yields elements until a predicate test on the elements becomes true, end the iteration.
      Iter<T> takeWhile​(java.util.function.Predicate<T> predicate)
      Create an Iter such that it yields elements while a predicate test on the elements is true, end the iteration.
      static <T> java.util.Iterator<T> takeWhile​(java.util.Iterator<T> iter, java.util.function.Predicate<T> predicate)
      Create an iterator such that it yields elements while a predicate test on the elements is true, end the iteration.
      java.util.List<T> toList()
      Consume the Iter and produce a List
      static <T> java.util.List<T> toList​(java.util.Iterator<? extends T> stream)
      Collect an iterator into a list.
      java.util.Set<T> toSet()
      Consume the Iter and produce a Set
      static <T> java.util.Set<T> toSet​(java.util.Iterator<? extends T> stream)
      Collect an iterator into a set.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Iterator

        forEachRemaining
    • Method Detail

      • forEach

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

        public static <T> java.util.stream.Stream<T> asStream​(java.util.Iterator<T> iterator)
      • asStream

        public static <T> java.util.stream.Stream<T> asStream​(java.util.Iterator<T> iterator,
                                                              boolean parallel)
      • singleton

        public static <T> java.util.Iterator<T> singleton​(T item)
      • nullIterator

        public static <T> java.util.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> java.util.Iterator<T> noRemove​(java.util.Iterator<T> iter)
        Return an iterator that does not permit remove. This makes an "UnmodifiableIterator".
      • toSet

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

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

        public static <T> java.util.Iterator<T> iterator​(java.util.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​(java.util.Iterator<? extends T> stream,
                                             R value,
                                             Iter.Folder<T,​R> function)
      • foldRight

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

        public static <T> java.util.Optional<T> reduce​(java.util.Iterator<T> iter,
                                                       java.util.function.BinaryOperator<T> accumulator)
      • reduce

        public static <T> T reduce​(java.util.Iterator<T> iter,
                                   T identity,
                                   java.util.function.BinaryOperator<T> accumulator)
      • min

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

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

        public static <T,​R> R collect​(java.util.Iterator<T> iter,
                                            java.util.function.Supplier<R> supplier,
                                            java.util.function.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​(java.util.Iterator<T> iter,
                                                    java.util.stream.Collector<? super T,​A,​R> collector)
        See Stream.collect(Collector)
      • apply

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

        public static <T> java.util.Iterator<T> filter​(java.util.Iterator<? extends T> stream,
                                                       java.util.function.Predicate<T> filter)
      • notFilter

        public static <T> java.util.Iterator<T> notFilter​(java.util.Iterator<? extends T> stream,
                                                          java.util.function.Predicate<T> filter)
      • allMatch

        public static <T> boolean allMatch​(java.util.Iterator<T> iter,
                                           java.util.function.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​(java.util.Iterator<T> iter,
                                           java.util.function.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​(java.util.Iterator<T> iter,
                                            java.util.function.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> java.util.Optional<T> findFirst​(java.util.Iterator<T> iter,
                                                          java.util.function.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> java.util.Optional<T> findLast​(java.util.Iterator<T> iter,
                                                         java.util.function.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> java.util.Optional<T> findAny​(java.util.Iterator<T> iter,
                                                        java.util.function.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> java.util.Iterator<R> map​(java.util.Iterator<? extends T> stream,
                                                            java.util.function.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> java.util.Iterator<R> flatMap​(java.util.Iterator<T> iter,
                                                                java.util.function.Function<T,​java.util.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> java.util.Iterator<T> operate​(java.util.Iterator<? extends T> stream,
                                                        java.util.function.Consumer<T> action)
        Apply an action to everything in stream, yielding a stream of the same items.
      • printWrapper

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

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

        public static <T> java.util.Iterator<T> append​(java.util.Iterator<? extends T> iter1,
                                                       java.util.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> java.util.Iterator<T> distinct​(java.util.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> java.util.Iterator<T> distinctAdjacent​(java.util.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> java.util.Iterator<T> removeNulls​(java.util.Iterator<T> iter)
        Remove nulls from an iterator
      • step

        public static int step​(java.util.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> java.util.List<T> take​(java.util.Iterator<T> iter,
                                                 int N)
        Take the first N elements of an iterator - stop early if too few
        See Also:
        limit(Iterator, long)
      • takeWhile

        public static <T> java.util.Iterator<T> takeWhile​(java.util.Iterator<T> iter,
                                                          java.util.function.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:
        filter(Iterator, Predicate)
      • takeUntil

        public static <T> java.util.Iterator<T> takeUntil​(java.util.Iterator<T> iter,
                                                          java.util.function.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:
        filter(Iterator, Predicate)
      • dropWhile

        public static <T> java.util.Iterator<T> dropWhile​(java.util.Iterator<T> iter,
                                                          java.util.function.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> java.util.Iterator<T> dropUntil​(java.util.Iterator<T> iter,
                                                          java.util.function.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> java.util.Iterator<X> limit​(java.util.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:
        take(Iterator, int)
      • skip

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

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

        public static <T> void consume​(java.util.Iterator<T> iterator)
        Consume the iterator
      • asString

        public static <T> java.lang.String asString​(java.util.Iterator<T> stream,
                                                    java.lang.String sep)
        Create a string from an iterator, using the separator. Note: this consumes the iterator.
      • asString

        public static <T> java.lang.String asString​(java.util.Iterator<T> stream,
                                                    java.lang.CharSequence sep,
                                                    java.lang.CharSequence prefix,
                                                    java.lang.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​(java.util.Iterator<T> iter)
        Close iterator if marked with Closeable.
      • onCloseIO

        public static <T> IteratorCloseable<T> onCloseIO​(java.util.Iterator<T> iterator,
                                                         java.io.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​(java.util.Iterator<T> iter,
                                                     java.lang.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> java.util.Iterator<T> log​(java.util.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> java.util.Iterator<T> log​(java.io.PrintStream out,
                                                    java.util.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> java.util.Iterator<T> debug​(java.util.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> java.util.Iterator<T> debug​(java.io.PrintStream out,
                                                      java.util.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​(java.util.Iterator<T> stream)
        Print an iterator (destructive)
      • print

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

        public static <T> void sendToSink​(java.util.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​(java.util.Collection<T> collection)
      • iter

        public static <T> Iter<T> iter​(java.util.Iterator<T> iterator)
      • singletonIter

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

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

        public static <T> java.util.Iterator<T> materialize​(java.util.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​(java.util.Iterator<T> iter1,
                                         java.util.Iterator<T> iter2)
        An Iterator of 2 Iterator's. See also IteratorConcat.
      • first

        public static <T> T first​(java.util.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​(java.util.Iterator<T> iter,
                                  java.util.function.Predicate<T> filter)
        Skip to the first element meeting a condition and return that element.
      • firstIndex

        public static <T> int firstIndex​(java.util.Iterator<T> iter,
                                         java.util.function.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​(java.util.Iterator<T> iter)
        Return the last element or null, if no elements. This operation consumes the iterator.
      • last

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

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

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

        public void forEach​(java.util.function.Consumer<T> action)
        Apply the Consumer to each element of the iterator
      • toSet

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

        public java.util.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​(java.util.function.Predicate<T> filter)
        Skip to the first element meeting a condition and return that element.
      • firstIndex

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

        public Iter<T> filter​(java.util.function.Predicate<T> filter)
        Filter by predicate
      • allMatch

        public boolean allMatch​(java.util.function.Predicate<? super T> predicate)
      • anyMatch

        public boolean anyMatch​(java.util.function.Predicate<? super T> predicate)
      • noneMatch

        public boolean noneMatch​(java.util.function.Predicate<? super T> predicate)
      • findFirst

        public java.util.Optional<T> findFirst​(java.util.function.Predicate<? super T> predicate)
      • findAny

        public java.util.Optional<T> findAny​(java.util.function.Predicate<? super T> predicate)
      • findLast

        public java.util.Optional<T> findLast​(java.util.function.Predicate<? super T> predicate)
      • removeNulls

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

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

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

        public Iter<T> operate​(java.util.function.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 java.util.Optional<T> reduce​(java.util.function.BinaryOperator<T> accumulator)
        Reduce. This reduce is fold-left (take first element, apply to rest of list)
      • reduce

        public T reduce​(T identity,
                        java.util.function.BinaryOperator<T> accumulator)
      • min

        public java.util.Optional<T> min​(java.util.Comparator<T> comparator)
      • max

        public java.util.Optional<T> max​(java.util.Comparator<T> comparator)
      • collect

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

        public <R,​A> R collect​(java.util.stream.Collector<? super T,​A,​R> collector)
        See Stream.collect(Collector)
      • apply

        public void apply​(java.util.function.Consumer<T> action)
        Apply an action to every element of an iterator
      • append

        public Iter<T> append​(java.util.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​(java.util.function.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:
        filter(Predicate)
      • takeUntil

        public Iter<T> takeUntil​(java.util.function.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:
        filter(Predicate)
      • dropWhile

        public Iter<T> dropWhile​(java.util.function.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​(java.util.function.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 java.util.Iterator<T>
      • next

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

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