java.lang.Object
dk.cloudcreate.essentials.shared.collections.Streams

public final class Streams extends Object
Utility class for working with streams.
  • Constructor Details

    • Streams

      public Streams()
  • Method Details

    • zipOrderedAndEqualSizedStreams

      public static <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. 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 streamT with the first element from streamU, the second element with the second, and so on. The provided zipFunction determines 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 from streamT and streamU; must be non-null.
      Returns:
      a new stream consisting of elements resulting from applying the zipFunction to pairs of elements from streamT and streamU.
      Throws:
      IllegalArgumentException - if streamT, streamU, or zipFunction is null or if streamT or streamU do not have equal sizes.