Class Streams
java.lang.Object
dk.cloudcreate.essentials.shared.collections.Streams
Utility class for working with streams.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <R,T, U> Stream<R> zipOrderedAndEqualSizedStreams(Stream<T> streamT, Stream<U> streamU, BiFunction<T, U, R> zipFunction) Combines two streams into a single stream by applying a zip function to pairs of elements from the two streams.
-
Constructor Details
-
Streams
public Streams()
-
-
Method Details
-
zipOrderedAndEqualSizedStreams
public static <R,T, Stream<R> zipOrderedAndEqualSizedStreamsU> (Stream<T> streamT, Stream<U> streamU, BiFunction<T, U, R> zipFunction) Combines two streams into a single stream by applying a zip function to pairs of elements from the two streams. The streams must have equal sizes. The resulting stream will have the same order as the input streams.The zip operation combines elements of the two streams pairwise: the first element from
streamTwith the first element fromstreamU, the second element with the second, and so on. The providedzipFunctiondetermines how the elements are combined into the resulting stream.The resulting stream will automatically close both input streams when it is closed.
Example:
var tStream = Stream.of("A", "B", "C", "D", "E"); var uStream = Stream.of("1", "2", "3", "4", "5"); List<String> result = Streams.zipOrderedAndEqualSizedStreams(tStream, uStream, (t, u) -> t + ":" + u) .collect(Collectors.toList()); assertThat(result).isEqualTo(List.of("A:1", "B:2", "C:3", "D:4", "E:5"));- Type Parameters:
R- The type of elements in the resulting stream.T- The type of elements in the first input stream.U- The type of elements in the second input stream.- Parameters:
streamT- the first input stream; must be non-null.streamU- the second input stream; must be non-null.zipFunction- a function to combine elements fromstreamTandstreamU; must be non-null.- Returns:
- a new stream consisting of elements resulting from applying the
zipFunctionto pairs of elements fromstreamTandstreamU. - Throws:
IllegalArgumentException- ifstreamT,streamU, orzipFunctionis null or ifstreamTorstreamUdo not have equal sizes.
-