Class ParallelCollectors

java.lang.Object
com.pivovarit.collectors.ParallelCollectors

public final class ParallelCollectors extends Object
An umbrella class exposing static factory methods for instantiating parallel Collectors
  • Constructor Details

    • ParallelCollectors

      private ParallelCollectors()
  • Method Details

    • parallel

      public static <T, R, RR> Collector<T,?,CompletableFuture<RR>> parallel(Function<T,R> mapper, Collector<R,?,RR> collector)
      A convenience Collector used for executing parallel computations using Virtual Threads and returning them as a CompletableFuture containing a result of the application of the user-provided Collector.
      Example:
      
       CompletableFuture<List<String>> result = Stream.of(1, 2, 3)
         .collect(parallel(i -> foo(i), toList()));
       
      Type Parameters:
      T - the type of the collected elements
      R - the result returned by mapper
      RR - the reduction result collector
      Parameters:
      mapper - a transformation to be performed in parallel
      collector - the Collector describing the reduction
      Returns:
      a Collector which collects all processed elements into a user-provided mutable Collection in parallel
      Since:
      3.0.0
    • parallel

      public static <T, R, RR> Collector<T,?,CompletableFuture<RR>> parallel(Function<T,R> mapper, Collector<R,?,RR> collector, int parallelism)
      A convenience Collector used for executing parallel computations using Virtual Threads and returning them as a CompletableFuture containing a result of the application of the user-provided Collector.
      Example:
      
       CompletableFuture<List<String>> result = Stream.of(1, 2, 3)
         .collect(parallel(i -> foo(i), toList(), executor, 2));
       
      Type Parameters:
      T - the type of the collected elements
      R - the result returned by mapper
      RR - the reduction result collector
      Parameters:
      mapper - a transformation to be performed in parallel
      collector - the Collector describing the reduction
      parallelism - the max parallelism level
      Returns:
      a Collector which collects all processed elements into a user-provided mutable Collection in parallel
      Since:
      3.2.0
    • parallel

      public static <T, R, RR> Collector<T,?,CompletableFuture<RR>> parallel(Function<T,R> mapper, Collector<R,?,RR> collector, Executor executor, int parallelism)
      A convenience Collector used for executing parallel computations on a custom Executor and returning them as a CompletableFuture containing a result of the application of the user-provided Collector.
      Example:
      
       CompletableFuture<List<String>> result = Stream.of(1, 2, 3)
         .collect(parallel(i -> foo(i), toList(), executor, 2));
       
      Type Parameters:
      T - the type of the collected elements
      R - the result returned by mapper
      RR - the reduction result collector
      Parameters:
      mapper - a transformation to be performed in parallel
      collector - the Collector describing the reduction
      executor - the Executor to use for asynchronous execution
      parallelism - the max parallelism level
      Returns:
      a Collector which collects all processed elements into a user-provided mutable Collection in parallel
      Since:
      2.0.0
    • parallel

      public static <T, R> Collector<T,?,CompletableFuture<Stream<R>>> parallel(Function<T,R> mapper)
      A convenience Collector used for executing parallel computations using Virtual Threads and returning them as CompletableFuture containing a Stream of these elements.

      The collector maintains the order of processed Stream. Instances should not be reused.
      Example:
      
       CompletableFuture<Stream<String>> result = Stream.of(1, 2, 3)
         .collect(parallel(i -> foo()));
       
      Type Parameters:
      T - the type of the collected elements
      R - the result returned by mapper
      Parameters:
      mapper - a transformation to be performed in parallel
      Returns:
      a Collector which collects all processed elements into a Stream in parallel
      Since:
      3.0.0
    • parallel

      public static <T, R> Collector<T,?,CompletableFuture<Stream<R>>> parallel(Function<T,R> mapper, int parallelism)
      A convenience Collector used for executing parallel computations using Virtual Threads and returning them as CompletableFuture containing a Stream of these elements.

      The collector maintains the order of processed Stream. Instances should not be reused.
      Example:
      
       CompletableFuture<Stream<String>> result = Stream.of(1, 2, 3)
         .collect(parallel(i -> foo()));
       
      Type Parameters:
      T - the type of the collected elements
      R - the result returned by mapper
      Parameters:
      mapper - a transformation to be performed in parallel
      Returns:
      a Collector which collects all processed elements into a Stream in parallel
      Since:
      3.2.0
    • parallel

      public static <T, R> Collector<T,?,CompletableFuture<Stream<R>>> parallel(Function<T,R> mapper, Executor executor, int parallelism)
      A convenience Collector used for executing parallel computations on a custom Executor and returning them as CompletableFuture containing a Stream of these elements.

      The collector maintains the order of processed Stream. Instances should not be reused.
      Example:
      
       CompletableFuture<Stream<String>> result = Stream.of(1, 2, 3)
         .collect(parallel(i -> foo(), executor, 2));
       
      Type Parameters:
      T - the type of the collected elements
      R - the result returned by mapper
      Parameters:
      mapper - a transformation to be performed in parallel
      executor - the Executor to use for asynchronous execution
      parallelism - the max parallelism level
      Returns:
      a Collector which collects all processed elements into a Stream in parallel
      Since:
      2.0.0
    • parallelToStream

      public static <T, R> Collector<T,?,Stream<R>> parallelToStream(Function<T,R> mapper)
      A convenience Collector used for executing parallel computations using Virtual Threads and returning a Stream instance returning results as they arrive.

      For the parallelism of 1, the stream is executed by the calling thread.
      Example:

      
       Stream.of(1, 2, 3)
         .collect(parallelToStream(i -> foo()))
         .forEach(System.out::println);
       
      Type Parameters:
      T - the type of the collected elements
      R - the result returned by mapper
      Parameters:
      mapper - a transformation to be performed in parallel
      Returns:
      a Collector which collects all processed elements into a Stream in parallel
      Since:
      3.0.0
    • parallelToStream

      public static <T, R> Collector<T,?,Stream<R>> parallelToStream(Function<T,R> mapper, int parallelism)
      A convenience Collector used for executing parallel computations using Virtual Threads and returning a Stream instance returning results as they arrive.

      For the parallelism of 1, the stream is executed by the calling thread.
      Example:

      
       Stream.of(1, 2, 3)
         .collect(parallelToStream(i -> foo(), executor, 2))
         .forEach(System.out::println);
       
      Type Parameters:
      T - the type of the collected elements
      R - the result returned by mapper
      Parameters:
      mapper - a transformation to be performed in parallel
      parallelism - the max parallelism level
      Returns:
      a Collector which collects all processed elements into a Stream in parallel
      Since:
      3.2.0
    • parallelToStream

      public static <T, R> Collector<T,?,Stream<R>> parallelToStream(Function<T,R> mapper, Executor executor, int parallelism)
      A convenience Collector used for executing parallel computations on a custom Executor and returning a Stream instance returning results as they arrive.

      For the parallelism of 1, the stream is executed by the calling thread.
      Example:

      
       Stream.of(1, 2, 3)
         .collect(parallelToStream(i -> foo(), executor, 2))
         .forEach(System.out::println);
       
      Type Parameters:
      T - the type of the collected elements
      R - the result returned by mapper
      Parameters:
      mapper - a transformation to be performed in parallel
      executor - the Executor to use for asynchronous execution
      parallelism - the max parallelism level
      Returns:
      a Collector which collects all processed elements into a Stream in parallel
      Since:
      2.0.0
    • parallelToOrderedStream

      public static <T, R> Collector<T,?,Stream<R>> parallelToOrderedStream(Function<T,R> mapper)
      A convenience Collector used for executing parallel computations using Virtual Threads and returning a Stream instance returning results as they arrive while maintaining the initial order.

      For the parallelism of 1, the stream is executed by the calling thread.
      Example:

      
       Stream.of(1, 2, 3)
         .collect(parallelToOrderedStream(i -> foo()))
         .forEach(System.out::println);
       
      Type Parameters:
      T - the type of the collected elements
      R - the result returned by mapper
      Parameters:
      mapper - a transformation to be performed in parallel
      Returns:
      a Collector which collects all processed elements into a Stream in parallel
      Since:
      3.0.0
    • parallelToOrderedStream

      public static <T, R> Collector<T,?,Stream<R>> parallelToOrderedStream(Function<T,R> mapper, int parallelism)
      A convenience Collector used for executing parallel computations using Virtual Threads and returning a Stream instance returning results as they arrive while maintaining the initial order.

      For the parallelism of 1, the stream is executed by the calling thread.
      Example:

      
       Stream.of(1, 2, 3)
         .collect(parallelToOrderedStream(i -> foo(), executor, 2))
         .forEach(System.out::println);
       
      Type Parameters:
      T - the type of the collected elements
      R - the result returned by mapper
      Parameters:
      mapper - a transformation to be performed in parallel
      parallelism - the max parallelism level
      Returns:
      a Collector which collects all processed elements into a Stream in parallel
      Since:
      3.2.0
    • parallelToOrderedStream

      public static <T, R> Collector<T,?,Stream<R>> parallelToOrderedStream(Function<T,R> mapper, Executor executor, int parallelism)
      A convenience Collector used for executing parallel computations on a custom Executor and returning a Stream instance returning results as they arrive while maintaining the initial order.

      For the parallelism of 1, the stream is executed by the calling thread.
      Example:

      
       Stream.of(1, 2, 3)
         .collect(parallelToOrderedStream(i -> foo(), executor, 2))
         .forEach(System.out::println);
       
      Type Parameters:
      T - the type of the collected elements
      R - the result returned by mapper
      Parameters:
      mapper - a transformation to be performed in parallel
      executor - the Executor to use for asynchronous execution
      parallelism - the max parallelism level
      Returns:
      a Collector which collects all processed elements into a Stream in parallel
      Since:
      2.0.0
    • toFuture

      public static <T, R> Collector<CompletableFuture<T>,?,CompletableFuture<R>> toFuture(Collector<T,?,R> collector)
      A convenience Collector for collecting a Stream<CompletableFuture<T>> into a CompletableFuture<R> using a provided Collector<T, ?, R>
      Type Parameters:
      T - the type of the collected elements
      R - the result of the transformation
      Parameters:
      collector - the Collector describing the reduction
      Returns:
      a Collector which collects all futures and combines them into a single future using the provided downstream Collector
      Since:
      2.3.0
    • toFuture

      public static <T> Collector<CompletableFuture<T>,?,CompletableFuture<List<T>>> toFuture()
      A convenience Collector for collecting a Stream<CompletableFuture<T>> into a CompletableFuture<List<T>>
      Type Parameters:
      T - the type of the collected elements
      Returns:
      a Collector which collects all futures and combines them into a single future returning a list of results
      Since:
      2.3.0