java.lang.Object
com.pivovarit.collectors.ParallelCollectors
An umbrella class exposing static factory methods for instantiating parallel
Collector
s-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
A 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) -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
R> Collector <T, ?, CompletableFuture<Stream<R>>> A convenienceCollector
used for executing parallel computations using Virtual Threads and returning them asCompletableFuture
containing aStream
of these elements.static <T,
R> Collector <T, ?, CompletableFuture<Stream<R>>> A convenienceCollector
used for executing parallel computations on a customExecutor
and returning them asCompletableFuture
containing aStream
of these elements.static <T,
R, RR> Collector <T, ?, CompletableFuture<RR>> A convenienceCollector
used for executing parallel computations using Virtual Threads and returning them as aCompletableFuture
containing a result of the application of the user-providedCollector
.static <T,
R, RR> Collector <T, ?, CompletableFuture<RR>> A convenienceCollector
used for executing parallel computations on a customExecutor
and returning them as aCompletableFuture
containing 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 convenienceCollector
for collecting aStream<CompletableFuture<T>>
into aCompletableFuture<List<T>>
static <T,
R> Collector <CompletableFuture<T>, ?, CompletableFuture<R>> A convenienceCollector
for collecting aStream<CompletableFuture<T>>
into aCompletableFuture<R>
using a providedCollector<T, ?, R>
-
Constructor Details
-
ParallelCollectors
private ParallelCollectors()
-
-
Method Details
-
parallel
public static <T,R, Collector<T,RR> ?, parallelCompletableFuture<RR>> (Function<T, R> mapper, Collector<R, ?, RR> collector) A convenienceCollector
used for executing parallel computations using Virtual Threads and returning them as aCompletableFuture
containing 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 bymapper
RR
- the reduction resultcollector
- Parameters:
mapper
- a transformation to be performed in parallelcollector
- theCollector
describing the reduction- Returns:
- a
Collector
which collects all processed elements into a user-provided mutableCollection
in 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 convenienceCollector
used for executing parallel computations on a customExecutor
and returning them as aCompletableFuture
containing 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 bymapper
RR
- the reduction resultcollector
- Parameters:
mapper
- a transformation to be performed in parallelcollector
- theCollector
describing the reductionexecutor
- theExecutor
to use for asynchronous executionparallelism
- the max parallelism level- Returns:
- a
Collector
which collects all processed elements into a user-provided mutableCollection
in parallel - Since:
- 2.0.0
-
parallel
A convenienceCollector
used for executing parallel computations using Virtual Threads and returning them asCompletableFuture
containing aStream
of 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
Collector
which collects all processed elements into aStream
in parallel - Since:
- 3.0.0
-
parallel
public static <T,R> Collector<T,?, parallelCompletableFuture<Stream<R>>> (Function<T, R> mapper, Executor executor, int parallelism) A convenienceCollector
used for executing parallel computations on a customExecutor
and returning them asCompletableFuture
containing aStream
of 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
- theExecutor
to use for asynchronous executionparallelism
- the max parallelism level- Returns:
- a
Collector
which collects all processed elements into aStream
in parallel - Since:
- 2.0.0
-
parallelToStream
A convenienceCollector
used for executing parallel computations using Virtual Threads and returning aStream
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 elementsR
- the result returned bymapper
- Parameters:
mapper
- a transformation to be performed in parallel- Returns:
- a
Collector
which collects all processed elements into aStream
in parallel - Since:
- 3.0.0
-
parallelToStream
public static <T,R> Collector<T,?, parallelToStreamStream<R>> (Function<T, R> mapper, Executor executor, int parallelism) A convenienceCollector
used for executing parallel computations on a customExecutor
and returning aStream
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 elementsR
- the result returned bymapper
- Parameters:
mapper
- a transformation to be performed in parallelexecutor
- theExecutor
to use for asynchronous executionparallelism
- the max parallelism level- Returns:
- a
Collector
which collects all processed elements into aStream
in parallel - Since:
- 2.0.0
-
parallelToOrderedStream
A convenienceCollector
used for executing parallel computations using Virtual Threads and returning aStream
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 elementsR
- the result returned bymapper
- Parameters:
mapper
- a transformation to be performed in parallel- Returns:
- a
Collector
which collects all processed elements into aStream
in parallel - Since:
- 3.0.0
-
parallelToOrderedStream
public static <T,R> Collector<T,?, parallelToOrderedStreamStream<R>> (Function<T, R> mapper, Executor executor, int parallelism) A convenienceCollector
used for executing parallel computations on a customExecutor
and returning aStream
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 elementsR
- the result returned bymapper
- Parameters:
mapper
- a transformation to be performed in parallelexecutor
- theExecutor
to use for asynchronous executionparallelism
- the max parallelism level- Returns:
- a
Collector
which collects all processed elements into aStream
in parallel - Since:
- 2.0.0
-
toFuture
public static <T,R> Collector<CompletableFuture<T>,?, toFutureCompletableFuture<R>> (Collector<T, ?, R> collector) A convenienceCollector
for 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
- theCollector
describing the reduction- Returns:
- a
Collector
which collects all futures and combines them into a single future using the provided downstreamCollector
- Since:
- 2.3.0
-
toFuture
A convenienceCollector
for collecting aStream<CompletableFuture<T>>
into aCompletableFuture<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
-