Class Zipping


  • public class Zipping
    extends java.lang.Object
    Utility class for algorithms that zip streams/collections together, i.e. produce a new stream/collection consisting of tuples of elements taken from the input streams/collections in lockstep.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <A> java.util.stream.Stream<java.lang.Integer> indicesWhere​(java.util.stream.Stream<A> source, java.util.function.BiPredicate<java.lang.Integer,​? super A> predicate)
      Find indices in the source stream where the given predicate holds.
      static <A,​B>
      java.util.stream.Stream<Pair<A,​B>>
      zip​(java.util.stream.Stream<A> streamA, java.util.stream.Stream<B> streamB)
      Combine elements from two streams in lockstep to produce a new stream.
      static <A,​B,​C>
      java.util.stream.Stream<C>
      zipMap​(java.util.stream.Stream<A> streamA, java.util.stream.Stream<B> streamB, java.util.function.BiFunction<A,​B,​C> func)
      Combine elements from two streams in lockstep to produce a new stream.
      static <A,​C>
      java.util.stream.Stream<C>
      zipWithCount​(java.util.stream.Stream<A> source, java.util.function.BiFunction<A,​java.lang.Integer,​C> func)
      A specialization of zipMap(Stream, Stream, BiFunction) where the second stream just counts up from 0.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • zipMap

        public static <A,​B,​C> java.util.stream.Stream<C> zipMap​(java.util.stream.Stream<A> streamA,
                                                                            java.util.stream.Stream<B> streamB,
                                                                            java.util.function.BiFunction<A,​B,​C> func)
        Combine elements from two streams in lockstep to produce a new stream. This is a terminal operation.

        If streamA produces values foo1, foo2, foo3 in order, and streamB produces values bar1, bar2, bar3 in order, then zipMap(func, streamA, streamB will produce the following values:

        • func.apply(foo1, bar1),
        • func.apply(foo2, bar2),
        • func.apply(foo3, bar3),
        in order.

        The output stream terminates when streamA or streamB terminates, whichever ends first.

        zipMap(func, streamA, streamB) is equivalent to zip(streamA, streamB).map(pair -> pair.consume(func)).

        Type Parameters:
        A - type of elements in streamA
        B - type of elements in streamB
        C - type of results
        Parameters:
        streamA - Any stream of objects
        streamB - Any other stream of objects
        func - The function with which elements will be combined
        Returns:
        A combined stream
      • zip

        public static <A,​B> java.util.stream.Stream<Pair<A,​B>> zip​(java.util.stream.Stream<A> streamA,
                                                                               java.util.stream.Stream<B> streamB)
        Combine elements from two streams in lockstep to produce a new stream. This is a terminal operation.

        If streamA produces values foo1, foo2, foo3 in order, and streamB produces values bar1, bar2, bar3 in order, then zip(streamA, streamB will produce the following values:

        • Pair.of(foo1, bar1),
        • Pair.of(foo2, bar2),
        • Pair.of(foo3, bar3),
        in order.

        The output stream terminates when streamA or streamB terminates, whichever ends first.

        zipMap(func, streamA, streamB) is equivalent to zip(streamA, streamB).map(pair -> pair.consume(func)).

        Type Parameters:
        A - type of elements in streamA
        B - type of elements in streamB
        Parameters:
        streamA - Any stream of objects
        streamB - Any other stream of objects
        Returns:
        A combined stream
      • zipWithCount

        public static <A,​C> java.util.stream.Stream<C> zipWithCount​(java.util.stream.Stream<A> source,
                                                                          java.util.function.BiFunction<A,​java.lang.Integer,​C> func)
        A specialization of zipMap(Stream, Stream, BiFunction) where the second stream just counts up from 0. Useful for accessing indices when iterating over a collection.
        Type Parameters:
        A - type of elements in source
        C - type of results
        Parameters:
        source - The source stream.
        func - The function to combine indices and elements
        Returns:
        A stream of function results
      • indicesWhere

        public static <A> java.util.stream.Stream<java.lang.Integer> indicesWhere​(java.util.stream.Stream<A> source,
                                                                                  java.util.function.BiPredicate<java.lang.Integer,​? super A> predicate)
        Find indices in the source stream where the given predicate holds.
        Type Parameters:
        A - type of stream elements
        Parameters:
        source - The source stream.
        predicate - The predicate to check. Also has access to the index.
        Returns:
        A stream of indices where the predicate held.