Class Zipping
- java.lang.Object
-
- org.openmuc.jeebus.spine.utils.algorithms.Zipping
-
public class Zipping extends java.lang.ObjectUtility 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 ofzipMap(Stream, Stream, BiFunction)where the second stream just counts up from 0.
-
-
-
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
streamAproduces valuesfoo1, foo2, foo3in order, andstreamBproduces valuesbar1, bar2, bar3in order, thenzipMap(func, streamA, streamBwill produce the following values:func.apply(foo1, bar1),func.apply(foo2, bar2),func.apply(foo3, bar3),
The output stream terminates when streamA or streamB terminates, whichever ends first.
zipMap(func, streamA, streamB)is equivalent tozip(streamA, streamB).map(pair -> pair.consume(func)).- Type Parameters:
A- type of elements instreamAB- type of elements instreamBC- type of results- Parameters:
streamA- Any stream of objectsstreamB- Any other stream of objectsfunc- 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
streamAproduces valuesfoo1, foo2, foo3in order, andstreamBproduces valuesbar1, bar2, bar3in order, thenzip(streamA, streamBwill produce the following values:Pair.of(foo1, bar1),Pair.of(foo2, bar2),Pair.of(foo3, bar3),
The output stream terminates when streamA or streamB terminates, whichever ends first.
zipMap(func, streamA, streamB)is equivalent tozip(streamA, streamB).map(pair -> pair.consume(func)).- Type Parameters:
A- type of elements instreamAB- type of elements instreamB- Parameters:
streamA- Any stream of objectsstreamB- 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 ofzipMap(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 insourceC- 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.
-
-