Class Iterators


  • public final class Iterators
    extends Object
    The methods in this class should only read the input Collections/Arrays, not modify any of them.
    Since:
    0.9
    Author:
    Haiyang Li
    • Method Detail

      • contains

        public static boolean contains​(Iterator<?> iter,
                                       Object objToFind)
        Parameters:
        iter -
        objToFind -
        Returns:
        true, if successful
      • containsAny

        public static boolean containsAny​(Iterator<?> iter,
                                          Set<?> objsToFind)
        Parameters:
        iter -
        objToFind -
        Returns:
        true, if successful
      • containsAll

        public static boolean containsAll​(Iterator<?> iter,
                                          Collection<?> objsToFind)
        Parameters:
        iter -
        objToFind -
        Returns:
        true, if successful
      • indexOf

        public static long indexOf​(Iterator<?> iter,
                                   Object objToFind)
        Parameters:
        iter -
        objToFind -
        Returns:
      • occurrencesOf

        public static long occurrencesOf​(Iterator<?> iter,
                                         Object objToFind)
        Parameters:
        iter -
        objToFind -
        Returns:
      • count

        public static long count​(Iterator<?> iter)
        Parameters:
        iter -
        Returns:
      • count

        public static <T> long count​(Iterator<T> iter,
                                     Predicate<? super T> filter)
        Type Parameters:
        T -
        Parameters:
        iter -
        filter -
        Returns:
      • toList

        public static <T> List<T> toList​(Iterator<? extends T> iter)
        Type Parameters:
        T -
        Parameters:
        iter -
        Returns:
      • toSet

        public static <T> Set<T> toSet​(Iterator<? extends T> iter)
        Type Parameters:
        T -
        Parameters:
        iter -
        Returns:
      • toCollection

        public static <T,​C extends Collection<T>> C toCollection​(Iterator<? extends T> iter,
                                                                       Supplier<? extends C> collectionFactory)
        Type Parameters:
        T -
        C -
        Parameters:
        iter -
        collectionFactory -
        Returns:
      • toMap

        public static <T,​K,​E extends ExceptionMap<K,​T> toMap​(Iterator<? extends T> iter,
                                                                                 Throwables.Function<? super T,​K,​E> keyMapper)
                                                                          throws E extends Exception
        Type Parameters:
        T -
        K - the key type
        E -
        Parameters:
        iter -
        keyMapper -
        Returns:
        Throws:
        E - the e
        E extends Exception
      • toMap

        public static <T,​K,​V,​E extends Exception,​E2 extends ExceptionMap<K,​V> toMap​(Iterator<? extends T> iter,
                                                                                                                    Throwables.Function<? super T,​K,​E> keyMapper,
                                                                                                                    Throwables.Function<? super T,​? extends V,​E2> valueExtractor)
                                                                                                             throws E extends Exception,
                                                                                                                    E2 extends Exception
        Type Parameters:
        T -
        K - the key type
        V - the value type
        E -
        E2 -
        Parameters:
        iter -
        keyMapper -
        valueExtractor -
        Returns:
        Throws:
        E - the e
        E2 - the e2
        E extends Exception
      • toMap

        public static <T,​K,​V,​M extends Map<K,​V>,​E extends Exception,​E2 extends Exception> M toMap​(Iterator<? extends T> iter,
                                                                                                                                      Throwables.Function<? super T,​K,​E> keyMapper,
                                                                                                                                      Throwables.Function<? super T,​? extends V,​E2> valueExtractor,
                                                                                                                                      Supplier<? extends M> mapSupplier)
                                                                                                                               throws E extends Exception,
                                                                                                                                      E2 extends Exception
        Type Parameters:
        T -
        K - the key type
        V - the value type
        M -
        E -
        E2 -
        Parameters:
        iter -
        keyMapper -
        valueExtractor -
        mapSupplier -
        Returns:
        Throws:
        E - the e
        E2 - the e2
        E extends Exception
      • forEach

        public static <A,​B,​E extends Exception> void forEach​(Iterator<A> a,
                                                                         Iterator<B> b,
                                                                         A valueForNoneA,
                                                                         B valueForNoneB,
                                                                         Throwables.BiConsumer<? super A,​? super B,​E> action)
                                                                  throws E extends Exception
        Type Parameters:
        A -
        B -
        E -
        Parameters:
        a -
        b -
        valueForNoneA -
        valueForNoneB -
        action -
        Throws:
        E - the e
        E extends Exception
      • forEach

        public static <A,​B,​C,​E extends Exception> void forEach​(Iterator<A> a,
                                                                                 Iterator<B> b,
                                                                                 Iterator<C> c,
                                                                                 A valueForNoneA,
                                                                                 B valueForNoneB,
                                                                                 C valueForNoneC,
                                                                                 Throwables.TriConsumer<? super A,​? super B,​? super C,​E> action)
                                                                          throws E extends Exception
        Type Parameters:
        A -
        B -
        C -
        E -
        Parameters:
        a -
        b -
        c -
        valueForNoneA -
        valueForNoneB -
        valueForNoneC -
        action -
        Throws:
        E - the e
        E extends Exception
      • forEachPair

        public static <T,​E extends Exception> void forEachPair​(Iterator<T> iter,
                                                                     Throwables.BiConsumer<? super T,​? super T,​E> action)
                                                              throws E extends Exception
        For each pair.
        Type Parameters:
        T -
        E -
        Parameters:
        iter -
        action -
        Throws:
        E - the e
        E extends Exception
      • forEachPair

        public static <T,​E extends Exception> void forEachPair​(Iterator<T> iter,
                                                                     Throwables.BiConsumer<? super T,​? super T,​E> action,
                                                                     int increment)
                                                              throws E extends Exception
        For each pair.
        Type Parameters:
        T -
        E -
        Parameters:
        iter -
        action -
        increment -
        Throws:
        E - the e
        E extends Exception
      • forEachTriple

        public static <T,​E extends Exception> void forEachTriple​(Iterator<T> iter,
                                                                       Throwables.TriConsumer<? super T,​? super T,​? super T,​E> action)
                                                                throws E extends Exception
        For each triple.
        Type Parameters:
        T -
        E -
        Parameters:
        iter -
        action -
        Throws:
        E - the e
        E extends Exception
      • forEachTriple

        public static <T,​E extends Exception> void forEachTriple​(Iterator<T> iter,
                                                                       Throwables.TriConsumer<? super T,​? super T,​? super T,​E> action,
                                                                       int increment)
                                                                throws E extends Exception
        For each triple.
        Type Parameters:
        T -
        E -
        Parameters:
        iter -
        action -
        increment -
        Throws:
        E - the e
        E extends Exception
      • repeat

        public static <T> ObjIterator<T> repeat​(T e,
                                                int n)
        Type Parameters:
        T -
        Parameters:
        e -
        n -
        Returns:
      • repeatEach

        public static <T> ObjIterator<T> repeatEach​(Collection<T> c,
                                                    int n)
        Type Parameters:
        T -
        Parameters:
        c -
        n -
        Returns:
      • repeatAll

        public static <T> ObjIterator<T> repeatAll​(Collection<T> c,
                                                   int n)
        Type Parameters:
        T -
        Parameters:
        c -
        n -
        Returns:
      • repeatEachToSize

        public static <T> ObjIterator<T> repeatEachToSize​(Collection<T> c,
                                                          int size)
        Repeat each to size.
        Type Parameters:
        T -
        Parameters:
        c -
        size -
        Returns:
      • repeatAllToSize

        public static <T> ObjIterator<T> repeatAllToSize​(Collection<T> c,
                                                         int size)
        Repeat all to size.
        Type Parameters:
        T -
        Parameters:
        c -
        size -
        Returns:
      • concat

        @SafeVarargs
        public static <T> ObjIterator<T> concat​(T[]... a)
        Type Parameters:
        T -
        Parameters:
        a -
        Returns:
      • concat

        public static <T> ObjIterator<T> concat​(Collection<? extends Iterator<? extends T>> c)
        Type Parameters:
        T -
        Parameters:
        c -
        Returns:
      • concat

        @SafeVarargs
        public static <A,​B,​C> TriIterator<A,​B,​C> concat​(TriIterator<A,​B,​C>... a)
        Type Parameters:
        A -
        B -
        C -
        Parameters:
        a -
        Returns:
      • zip

        public static <A,​B,​R> ObjIterator<R> zip​(Collection<A> a,
                                                             Collection<B> b,
                                                             BiFunction<? super A,​? super B,​R> zipFunction)
        Type Parameters:
        A -
        B -
        R -
        Parameters:
        a -
        b -
        zipFunction -
        Returns:
      • zip

        public static <A,​B,​R> ObjIterator<R> zip​(Iterator<A> a,
                                                             Iterator<B> b,
                                                             BiFunction<? super A,​? super B,​R> zipFunction)
        Type Parameters:
        A -
        B -
        R -
        Parameters:
        a -
        b -
        zipFunction -
        Returns:
      • zip

        public static <A,​B,​C,​R> ObjIterator<R> zip​(Collection<A> a,
                                                                     Collection<B> b,
                                                                     Collection<C> c,
                                                                     TriFunction<? super A,​? super B,​? super C,​R> zipFunction)
        Type Parameters:
        A -
        B -
        C -
        R -
        Parameters:
        a -
        b -
        c -
        zipFunction -
        Returns:
      • zip

        public static <A,​B,​C,​R> ObjIterator<R> zip​(Iterator<A> a,
                                                                     Iterator<B> b,
                                                                     Iterator<C> c,
                                                                     TriFunction<? super A,​? super B,​? super C,​R> zipFunction)
        Type Parameters:
        A -
        B -
        C -
        R -
        Parameters:
        a -
        b -
        c -
        zipFunction -
        Returns:
      • zip

        public static <A,​B,​R> ObjIterator<R> zip​(Collection<A> a,
                                                             Collection<B> b,
                                                             A valueForNoneA,
                                                             B valueForNoneB,
                                                             BiFunction<? super A,​? super B,​R> zipFunction)
        Type Parameters:
        A -
        B -
        R -
        Parameters:
        a -
        b -
        valueForNoneA -
        valueForNoneB -
        zipFunction -
        Returns:
      • zip

        public static <A,​B,​R> ObjIterator<R> zip​(Iterator<A> a,
                                                             Iterator<B> b,
                                                             A valueForNoneA,
                                                             B valueForNoneB,
                                                             BiFunction<? super A,​? super B,​R> zipFunction)
        Type Parameters:
        A -
        B -
        R -
        Parameters:
        a -
        b -
        valueForNoneA -
        valueForNoneB -
        zipFunction -
        Returns:
      • zip

        public static <A,​B,​C,​R> ObjIterator<R> zip​(Collection<A> a,
                                                                     Collection<B> b,
                                                                     Collection<C> c,
                                                                     A valueForNoneA,
                                                                     B valueForNoneB,
                                                                     C valueForNoneC,
                                                                     TriFunction<? super A,​? super B,​? super C,​R> zipFunction)
        Type Parameters:
        A -
        B -
        C -
        R -
        Parameters:
        a -
        b -
        c -
        valueForNoneA -
        valueForNoneB -
        valueForNoneC -
        zipFunction -
        Returns:
      • zip

        public static <A,​B,​C,​R> ObjIterator<R> zip​(Iterator<A> a,
                                                                     Iterator<B> b,
                                                                     Iterator<C> c,
                                                                     A valueForNoneA,
                                                                     B valueForNoneB,
                                                                     C valueForNoneC,
                                                                     TriFunction<? super A,​? super B,​? super C,​R> zipFunction)
        Type Parameters:
        A -
        B -
        C -
        R -
        Parameters:
        a -
        b -
        c -
        valueForNoneA -
        valueForNoneB -
        valueForNoneC -
        zipFunction -
        Returns:
      • unzip

        public static <T,​L,​R> BiIterator<L,​R> unzip​(Iterator<? extends T> iter,
                                                                      BiConsumer<? super T,​Pair<L,​R>> unzip)
        Type Parameters:
        T -
        L -
        R -
        Parameters:
        iter -
        unzip - the second parameter is an output parameter.
        Returns:
      • unzipp

        public static <T,​L,​M,​R> TriIterator<L,​M,​R> unzipp​(Iterator<? extends T> iter,
                                                                                        BiConsumer<? super T,​Triple<L,​M,​R>> unzip)
        Type Parameters:
        T -
        L -
        M -
        R -
        Parameters:
        iter -
        unzip - the second parameter is an output parameter.
        Returns:
      • split

        public static <T> ObjIterator<List<T>> split​(Iterator<? extends T> iter,
                                                     int chunkSize)
        Type Parameters:
        T -
        Parameters:
        iter -
        chunkSize - the desired size of each sub sequence (the last may be smaller).
        Returns:
      • get

        public static <T> u.Nullable<T> get​(Iterator<T> iter,
                                            int index)
        Type Parameters:
        T -
        Parameters:
        iter -
        index -
        Returns:
      • first

        public static <T> u.Nullable<T> first​(Iterator<T> iter)
        Type Parameters:
        T -
        Parameters:
        iter -
        Returns:
      • firstNonNull

        public static <T> u.Optional<T> firstNonNull​(Iterator<T> iter)
        First non null.
        Type Parameters:
        T -
        Parameters:
        iter -
        Returns:
      • last

        public static <T> u.Nullable<T> last​(Iterator<T> iter)
        Type Parameters:
        T -
        Parameters:
        iter -
        Returns:
      • lastNonNull

        public static <T> u.Optional<T> lastNonNull​(Iterator<T> iter)
        Last non null.
        Type Parameters:
        T -
        Parameters:
        iter -
        Returns:
      • advance

        public static long advance​(Iterator<?> iterator,
                                   long numberToAdvance)
        Note: copied from Google Guava under Apache license v2
        Calls next() on iterator, either numberToAdvance times or until hasNext() returns false, whichever comes first.
        Parameters:
        iterator -
        numberToAdvance -
        Returns:
      • skip

        public static <T> ObjIterator<T> skip​(Iterator<T> iter,
                                              long n)
        Calls next() on iterator, either n times or until hasNext() returns false, whichever comes first. This is a lazy evaluation operation. The skip action is only triggered when Iterator.hasNext() or Iterator.next() is called.
        Type Parameters:
        T -
        Parameters:
        iter -
        n -
        Returns:
      • limit

        public static <T> ObjIterator<T> limit​(Iterator<T> iter,
                                               long count)
        Returns a new Iterator.
        Type Parameters:
        T -
        Parameters:
        iter -
        count -
        Returns:
      • skipAndLimit

        public static <T> ObjIterator<T> skipAndLimit​(Iterator<T> iter,
                                                      long offset,
                                                      long count)
        Calls next() on iterator, either offset times or until hasNext() returns false, whichever comes first. This is a lazy evaluation operation. The skip action is only triggered when Iterator.hasNext() or Iterator.next() is called.
        Type Parameters:
        T -
        Parameters:
        iter -
        offset -
        count -
        Returns:
      • slice

        public static <T> ObjIterator<T> slice​(Iterator<T> iter,
                                               long fromIndex,
                                               long toIndex)
        Type Parameters:
        T -
        Parameters:
        iter -
        fromIndex -
        toIndex -
        Returns:
      • skipNull

        public static <T> ObjIterator<T> skipNull​(Iterator<T> iter)
        Returns a new ObjIterator with null elements removed.
        Type Parameters:
        T -
        Parameters:
        iter -
        Returns:
      • getOnlyElement

        public static <T> u.Nullable<T> getOnlyElement​(Iterator<? extends T> iter)
                                                throws DuplicatedResultException
        Gets the only element.
        Type Parameters:
        T -
        Parameters:
        iter -
        Returns:
        throws DuplicatedResultException if there are more than one elements in the specified iter.
        Throws:
        DuplicatedResultException - the duplicated result exception
      • filter

        public static <T> ObjIterator<T> filter​(Iterator<T> iter,
                                                Predicate<? super T> filter)
        Type Parameters:
        T -
        Parameters:
        iter -
        filter -
        Returns:
      • map

        public static <T,​U> ObjIterator<U> map​(Iterator<T> iter,
                                                     Function<? super T,​U> mapper)
        Type Parameters:
        T -
        U -
        Parameters:
        iter -
        mapper -
        Returns:
      • flatMap

        public static <T,​U> ObjIterator<U> flatMap​(Iterator<T> iter,
                                                         Function<? super T,​? extends Collection<? extends U>> mapper)
        Type Parameters:
        T -
        U -
        Parameters:
        iter -
        mapper -
        Returns:
      • generate

        public static <T,​U> ObjIterator<T> generate​(U init,
                                                          Predicate<? super U> hasNext,
                                                          Function<? super U,​T> supplier)
        Type Parameters:
        T -
        U -
        Parameters:
        init -
        hasNext -
        supplier -
        Returns:
      • generate

        public static <T,​U> ObjIterator<T> generate​(U init,
                                                          BiPredicate<? super U,​T> hasNext,
                                                          BiFunction<? super U,​T,​T> supplier)
        Type Parameters:
        T -
        U -
        Parameters:
        init -
        hasNext -
        supplier -
        Returns: