Class IteratorUtil


  • public final class IteratorUtil
    extends Object
    Operations for dealing with Iterators.
    Author:
    Clément Fournier
    Since:
    6.11.0
    • Method Detail

      • flatMapWithSelf

        public static <R> Iterator<R> flatMapWithSelf​(Iterator<? extends R> iter,
                                                      Function<? super R,​? extends @Nullable Iterator<? extends R>> f)
        Like flatMap, but yields each element of the input iterator before yielding the results of the mapper function. Null elements of the input iterator are both yielded by the returned iterator and passed to the stepper. If the stepper returns null, that result is ignored.
      • filterNotNull

        public static <T> Iterator<@NonNull T> filterNotNull​(Iterator<? extends T> it)
      • mapNotNull

        public static <T,​R> Iterator<@NonNull R> mapNotNull​(Iterator<? extends T> it,
                                                                  Function<? super T,​? extends R> mapper)
      • map

        public static <T,​R> Iterator<R> map​(Iterator<? extends T> iter,
                                                  Function<? super T,​? extends R> mapper)
      • mapIterator

        public static <T,​R> Iterable<R> mapIterator​(Iterable<? extends T> iter,
                                                          Function<? super Iterator<? extends T>,​? extends Iterator<R>> mapper)
        Apply a transform on the iterator of an iterable.
      • distinct

        public static <T> Iterator<T> distinct​(Iterator<? extends T> iter)
      • toList

        public static <T> List<T> toList​(Iterator<? extends T> it)
      • toNonNullList

        public static <T> List<@NonNull T> toNonNullList​(Iterator<? extends @Nullable T> it)
      • dropLast

        public static <T> Iterator<@NonNull T> dropLast​(Iterator<? extends @Nullable T> it,
                                                        int n)
        Remove the last n elements of the iterator. This uses n elements as a lookahead.
      • coerceWildcard

        public static <T> Iterator<T> coerceWildcard​(Iterator<? extends T> it)
        Coerce an iterator with a wildcard. This is safe because the Iterator interface is covariant (not ListIterator though).
      • count

        public static int count​(Iterator<?> it)
        Counts the items in this iterator, exhausting it.
      • last

        public static <T> @Nullable T last​(Iterator<? extends T> iterator)
      • getNth

        public static <T> @Nullable T getNth​(Iterator<? extends T> iterator,
                                             int n)
        Returns the nth element of this iterator, or null if the iterator is shorter.
        Throws:
        IllegalArgumentException - If n is negative
      • advance

        public static void advance​(Iterator<?> iterator,
                                   int n)
        Advance n times.
      • take

        public static <T> Iterator<T> take​(Iterator<? extends T> iterator,
                                           int n)
        Limit the number of elements yielded by this iterator to the given number.
      • drop

        public static <T> Iterator<T> drop​(Iterator<? extends T> source,
                                           int n)
        Produce an iterator whose first element is the nth element of the given source.
      • generate

        public static <T> Iterator<@NonNull T> generate​(@Nullable T seed,
                                                        Function<? super @NonNull T,​? extends @Nullable T> stepper)
        Returns an iterator that applies a stepping function to the previous value yielded. Iteration stops on the first null value returned by the stepper.
        Type Parameters:
        T - Type of values
        Parameters:
        seed - First value. If null then the iterator is empty
        stepper - Step function
      • anyMatch

        public static <T> boolean anyMatch​(Iterator<? extends T> iterator,
                                           Predicate<? super T> pred)
        Returns whether some element match the predicate. If empty then false is returned.
      • allMatch

        public static <T> boolean allMatch​(Iterator<? extends T> iterator,
                                           Predicate<? super T> pred)
        Returns whether all elements match the predicate. If empty then true is returned.
      • noneMatch

        public static <T> boolean noneMatch​(Iterator<? extends T> iterator,
                                            Predicate<? super T> pred)
        Returns whether no elements match the predicate. If empty then true is returned.
      • singletonIterator

        public static <T> Iterator<T> singletonIterator​(T value)
      • asReversed

        public static <T> Iterable<T> asReversed​(List<T> lst)
      • toStream

        public static <T> Stream<T> toStream​(Iterator<? extends T> iter)
      • toStream

        public static <T> Stream<T> toStream​(Iterable<T> iter)