java.lang.Object
com.pivovarit.collectors.ParallelCollectors
An umbrella class exposing static factory methods for instantiating parallel
Collectors- Author:
- Grzegorz Piwowarek
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA subset of collectors which perform operations in batches and not separately (one object in a thread pool's worker queue represents a batch of operations to be performed by a single thread) -
Method Summary
Modifier and TypeMethodDescriptionstatic <T,R> Collector <T, ?, CompletableFuture<Stream<R>>> A convenienceCollectorused for executing parallel computations using Virtual Threads and returning them asCompletableFuturecontaining aStreamof these elements.static <T,R> Collector <T, ?, CompletableFuture<Stream<R>>> A convenienceCollectorused for executing parallel computations on a customExecutorand returning them asCompletableFuturecontaining aStreamof these elements.static <T,R, RR> Collector <T, ?, CompletableFuture<RR>> A convenienceCollectorused for executing parallel computations using Virtual Threads and returning them as aCompletableFuturecontaining a result of the application of the user-providedCollector.static <T,R, RR> Collector <T, ?, CompletableFuture<RR>> A convenienceCollectorused for executing parallel computations on a customExecutorand returning them as aCompletableFuturecontaining a result of the application of the user-providedCollector.parallelToOrderedStream(Function<T, R> mapper) parallelToOrderedStream(Function<T, R> mapper, Executor executor, int parallelism) parallelToStream(Function<T, R> mapper) parallelToStream(Function<T, R> mapper, Executor executor, int parallelism) static <T> Collector<CompletableFuture<T>, ?, CompletableFuture<List<T>>> toFuture()A convenienceCollectorfor collecting aStream<CompletableFuture<T>>into aCompletableFuture<List<T>>static <T,R> Collector <CompletableFuture<T>, ?, CompletableFuture<R>> A convenienceCollectorfor collecting aStream<CompletableFuture<T>>into aCompletableFuture<R>using a providedCollector<T, ?, R>
-
Method Details
-
parallel
public static <T,R, Collector<T,RR> ?, parallelCompletableFuture<RR>> (Function<T, R> mapper, Collector<R, ?, RR> collector) A convenienceCollectorused for executing parallel computations using Virtual Threads and returning them as aCompletableFuturecontaining a result of the application of the user-providedCollector.
Example:CompletableFuture<List<String>> result = Stream.of(1, 2, 3) .collect(parallel(i -> foo(i), toList()));- Type Parameters:
T- the type of the collected elementsR- the result returned bymapperRR- the reduction resultcollector- Parameters:
mapper- a transformation to be performed in parallelcollector- theCollectordescribing the reduction- Returns:
- a
Collectorwhich collects all processed elements into a user-provided mutableCollectionin parallel - Since:
- 3.0.0
-
parallel
public static <T,R, Collector<T,RR> ?, parallelCompletableFuture<RR>> (Function<T, R> mapper, Collector<R, ?, RR> collector, Executor executor, int parallelism) A convenienceCollectorused for executing parallel computations on a customExecutorand returning them as aCompletableFuturecontaining a result of the application of the user-providedCollector.
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 elementsR- the result returned bymapperRR- the reduction resultcollector- Parameters:
mapper- a transformation to be performed in parallelcollector- theCollectordescribing the reductionexecutor- theExecutorto use for asynchronous executionparallelism- the max parallelism level- Returns:
- a
Collectorwhich collects all processed elements into a user-provided mutableCollectionin parallel - Since:
- 2.0.0
-
parallel
A convenienceCollectorused for executing parallel computations using Virtual Threads and returning them asCompletableFuturecontaining aStreamof these elements.
The collector maintains the order of processedStream. 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 elementsR- the result returned bymapper- Parameters:
mapper- a transformation to be performed in parallel- Returns:
- a
Collectorwhich collects all processed elements into aStreamin parallel - Since:
- 3.0.0
-
parallel
public static <T,R> Collector<T,?, parallelCompletableFuture<Stream<R>>> (Function<T, R> mapper, Executor executor, int parallelism) A convenienceCollectorused for executing parallel computations on a customExecutorand returning them asCompletableFuturecontaining aStreamof these elements.
The collector maintains the order of processedStream. 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 elementsR- the result returned bymapper- Parameters:
mapper- a transformation to be performed in parallelexecutor- theExecutorto use for asynchronous executionparallelism- the max parallelism level- Returns:
- a
Collectorwhich collects all processed elements into aStreamin parallel - Since:
- 2.0.0
-
parallelToStream
A convenienceCollectorused for executing parallel computations using Virtual Threads and returning aStreaminstance 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 elementsR- the result returned bymapper- Parameters:
mapper- a transformation to be performed in parallel- Returns:
- a
Collectorwhich collects all processed elements into aStreamin parallel - Since:
- 3.0.0
-
parallelToStream
public static <T,R> Collector<T,?, parallelToStreamStream<R>> (Function<T, R> mapper, Executor executor, int parallelism) A convenienceCollectorused for executing parallel computations on a customExecutorand returning aStreaminstance 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 elementsR- the result returned bymapper- Parameters:
mapper- a transformation to be performed in parallelexecutor- theExecutorto use for asynchronous executionparallelism- the max parallelism level- Returns:
- a
Collectorwhich collects all processed elements into aStreamin parallel - Since:
- 2.0.0
-
parallelToOrderedStream
A convenienceCollectorused for executing parallel computations using Virtual Threads and returning aStreaminstance 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 elementsR- the result returned bymapper- Parameters:
mapper- a transformation to be performed in parallel- Returns:
- a
Collectorwhich collects all processed elements into aStreamin parallel - Since:
- 3.0.0
-
parallelToOrderedStream
public static <T,R> Collector<T,?, parallelToOrderedStreamStream<R>> (Function<T, R> mapper, Executor executor, int parallelism) A convenienceCollectorused for executing parallel computations on a customExecutorand returning aStreaminstance 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 elementsR- the result returned bymapper- Parameters:
mapper- a transformation to be performed in parallelexecutor- theExecutorto use for asynchronous executionparallelism- the max parallelism level- Returns:
- a
Collectorwhich collects all processed elements into aStreamin parallel - Since:
- 2.0.0
-
toFuture
public static <T,R> Collector<CompletableFuture<T>,?, toFutureCompletableFuture<R>> (Collector<T, ?, R> collector) A convenienceCollectorfor collecting aStream<CompletableFuture<T>>into aCompletableFuture<R>using a providedCollector<T, ?, R>- Type Parameters:
T- the type of the collected elementsR- the result of the transformation- Parameters:
collector- theCollectordescribing the reduction- Returns:
- a
Collectorwhich collects all futures and combines them into a single future using the provided downstreamCollector - Since:
- 2.3.0
-
toFuture
A convenienceCollectorfor collecting aStream<CompletableFuture<T>>into aCompletableFuture<List<T>>- Type Parameters:
T- the type of the collected elements- Returns:
- a
Collectorwhich collects all futures and combines them into a single future returning a list of results - Since:
- 2.3.0
-