Class ParallelStreamSupport<T>
- java.lang.Object
-
- com.github.ferstl.streams.ParallelStreamSupport<T>
-
- Type Parameters:
T
- The type of the stream elements.
- All Implemented Interfaces:
AutoCloseable
,BaseStream<T,Stream<T>>
,Stream<T>
public class ParallelStreamSupport<T> extends Object implements Stream<T>
An implementation ofStream
which uses a customForkJoinPool
for parallel aggregate operations.The following example illustrates an aggregate operation using
ParallelStreamSupport
with a customForkJoinPool
:ForkJoinPool pool = new ForkJoinPool(); int sum = ParallelStreamSupport.parallelStream(widgets, pool) .filter(w -> w.getColor() == RED) .mapToInt(w -> w.getWeight()) .sum();
In case this stream is configured for parallel execution, i.e.
This implementation offers various factory methods which are based on:BaseStream.isParallel()
returnstrue
, a terminal operation will be executed asForkJoinTask
in the customForkJoinPool
. Otherwise it will be executed in the calling thread.- The static factory methods of
Stream
, which are meaningful for parallel streams Collection.parallelStream()
Arrays.stream(Object[])
StreamSupport.stream(Spliterator, boolean)
StreamSupport.stream(Supplier, int, boolean)
- API Note:
Internally, this stream wraps a stream which is initially created in one of the static factory methods. Whenever a non-terminal operation is called the underlying stream will be replaced with the result of calling the same method on that stream. The return value of these operations is always this stream or, in case of operations that return a different type of stream, one of
ParallelIntStreamSupport
,ParallelLongStreamSupport
orParallelDoubleStreamSupport
.Although each factory method returns a parallel stream, calling
BaseStream.sequential()
is still possible and leads to sequential execution of a terminal operation within the calling thread.- Implementation Note:
See the class documentation for
Stream
and the package documentation for java.util.stream for additional specification.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface java.util.stream.Stream
Stream.Builder<T extends Object>
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
allMatch(Predicate<? super T> predicate)
boolean
anyMatch(Predicate<? super T> predicate)
void
close()
<R> R
collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
<R,A>
Rcollect(Collector<? super T,A,R> collector)
static <T> Stream<T>
concat(Stream<? extends T> a, Stream<? extends T> b, ForkJoinPool workerPool)
Creates a lazily concatenated parallel stream whose elements are all the elements of the first stream followed by all the elements of the second stream.long
count()
Stream<T>
distinct()
Stream<T>
dropWhile(Predicate<? super T> predicate)
protected void
execute(Runnable terminalOperation)
protected <R> R
execute(Callable<R> terminalOperation)
Stream<T>
filter(Predicate<? super T> predicate)
Optional<T>
findAny()
Optional<T>
findFirst()
<R> Stream<R>
flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
DoubleStream
flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
IntStream
flatMapToInt(Function<? super T,? extends IntStream> mapper)
LongStream
flatMapToLong(Function<? super T,? extends LongStream> mapper)
void
forEach(Consumer<? super T> action)
void
forEachOrdered(Consumer<? super T> action)
static <T> Stream<T>
generate(Supplier<T> supplier, ForkJoinPool workerPool)
Creates a parallel infinite sequential unordered stream where each element is generated by the providedSupplier
.boolean
isParallel()
static <T> Stream<T>
iterate(T seed, UnaryOperator<T> operator, ForkJoinPool workerPool)
Creates a parallel infinite ordered stream produced by iterative application of a functionf
to an initial elementseed
.Iterator<T>
iterator()
Stream<T>
limit(long maxSize)
<R> Stream<R>
map(Function<? super T,? extends R> mapper)
DoubleStream
mapToDouble(ToDoubleFunction<? super T> mapper)
IntStream
mapToInt(ToIntFunction<? super T> mapper)
LongStream
mapToLong(ToLongFunction<? super T> mapper)
Optional<T>
max(Comparator<? super T> comparator)
Optional<T>
min(Comparator<? super T> comparator)
boolean
noneMatch(Predicate<? super T> predicate)
S
onClose(Runnable closeHandler)
S
parallel()
static <T> Stream<T>
parallelStream(Collection<T> collection, ForkJoinPool workerPool)
Creates a parallel stream from the given Collection.static <T> Stream<T>
parallelStream(Supplier<? extends Spliterator<T>> supplier, int characteristics, ForkJoinPool workerPool)
Creates a parallel stream from the given Spliterator supplier.static <T> Stream<T>
parallelStream(Spliterator<T> spliterator, ForkJoinPool workerPool)
Creates a parallel stream from the given Spliterator.static <T> Stream<T>
parallelStream(Stream.Builder<T> builder, ForkJoinPool workerPool)
Creates a parallel stream from the givenStream.Builder
.static <T> Stream<T>
parallelStream(T[] array, ForkJoinPool workerPool)
Creates a parallel stream from the given Array.Stream<T>
peek(Consumer<? super T> action)
Optional<T>
reduce(BinaryOperator<T> accumulator)
T
reduce(T identity, BinaryOperator<T> accumulator)
<U> U
reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
S
sequential()
Stream<T>
skip(long n)
Stream<T>
sorted()
Stream<T>
sorted(Comparator<? super T> comparator)
Spliterator<T>
spliterator()
Stream<T>
takeWhile(Predicate<? super T> predicate)
Object[]
toArray()
<A> A[]
toArray(IntFunction<A[]> generator)
S
unordered()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.stream.BaseStream
close, isParallel, iterator, onClose, parallel, sequential, spliterator, unordered
-
-
-
-
Method Detail
-
parallelStream
public static <T> Stream<T> parallelStream(Collection<T> collection, ForkJoinPool workerPool)
Creates a parallel stream from the given Collection. This operation is similar toCollection.parallelStream()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Type Parameters:
T
- The type of stream elements.- Parameters:
collection
- Collection to create the parallel stream from. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel stream that executes a terminal operation in the given
ForkJoinPool
. - See Also:
Collection.parallelStream()
-
parallelStream
public static <T> Stream<T> parallelStream(T[] array, ForkJoinPool workerPool)
Creates a parallel stream from the given Array. This operation is similar to callingArrays.stream(array).parallel()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Type Parameters:
T
- The type of stream elements.- Parameters:
array
- Array to create the parallel stream from. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel stream that executes a terminal operation in the given
ForkJoinPool
. - See Also:
Arrays.stream(Object[])
-
parallelStream
public static <T> Stream<T> parallelStream(Spliterator<T> spliterator, ForkJoinPool workerPool)
Creates a parallel stream from the given Spliterator. This operation is similar to callingStreamSupport.stream(spliterator, true)
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Type Parameters:
T
- The type of stream elements.- Parameters:
spliterator
- ASpliterator
describing the stream elements. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel stream that executes a terminal operation in the given
ForkJoinPool
. - See Also:
StreamSupport.stream(Spliterator, boolean)
-
parallelStream
public static <T> Stream<T> parallelStream(Supplier<? extends Spliterator<T>> supplier, int characteristics, ForkJoinPool workerPool)
Creates a parallel stream from the given Spliterator supplier. This operation is similar to callingStreamSupport.stream(supplier, characteristics, true)
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Type Parameters:
T
- The type of stream elements.- Parameters:
supplier
- ASupplier
of aSpliterator
. Must not benull
.characteristics
- Spliterator characteristics of the suppliedSpliterator
. The characteristics must be equal tosupplier.get().characteristics()
, otherwise undefined behavior may occur when terminal operation commences.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel stream that executes a terminal operation in the given
ForkJoinPool
. - See Also:
StreamSupport.stream(Supplier, int, boolean)
-
parallelStream
public static <T> Stream<T> parallelStream(Stream.Builder<T> builder, ForkJoinPool workerPool)
Creates a parallel stream from the givenStream.Builder
. This operation is similar to callingbuilder.build().parallel()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Type Parameters:
T
- The type of stream elements.- Parameters:
builder
- The builder to create the stream from. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel stream that executes a terminal operation in the given
ForkJoinPool
. - See Also:
Stream.builder()
-
iterate
public static <T> Stream<T> iterate(T seed, UnaryOperator<T> operator, ForkJoinPool workerPool)
Creates a parallel infinite ordered stream produced by iterative application of a functionf
to an initial elementseed
. This operation is similar to callingStream.iterate(seed, operator).parallel()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Type Parameters:
T
- The type of stream elements.- Parameters:
seed
- The initial element.operator
- A function to be applied to to the previous element to produce a new element. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel stream that executes a terminal operation in the given
ForkJoinPool
. - See Also:
Stream.iterate(Object, UnaryOperator)
-
generate
public static <T> Stream<T> generate(Supplier<T> supplier, ForkJoinPool workerPool)
Creates a parallel infinite sequential unordered stream where each element is generated by the providedSupplier
. This operation is similar to callingStream.generate(supplier).parallel()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Type Parameters:
T
- The type of stream elements.- Parameters:
supplier
- TheSupplier
of generated elements. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel stream that executes a terminal operation in the given
ForkJoinPool
. - See Also:
Stream.generate(Supplier)
-
concat
public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b, ForkJoinPool workerPool)
Creates a lazily concatenated parallel stream whose elements are all the elements of the first stream followed by all the elements of the second stream. This operation is similar to callingStream.concat(a, b).parallel()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Type Parameters:
T
- The type of stream elements.- Parameters:
a
- The first stream. Must not benull
.b
- The second stream. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel stream that executes a terminal operation in the given
ForkJoinPool
. - See Also:
Stream.concat(Stream, Stream)
-
mapToInt
public IntStream mapToInt(ToIntFunction<? super T> mapper)
-
mapToLong
public LongStream mapToLong(ToLongFunction<? super T> mapper)
-
mapToDouble
public DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)
- Specified by:
mapToDouble
in interfaceStream<T>
-
flatMapToInt
public IntStream flatMapToInt(Function<? super T,? extends IntStream> mapper)
- Specified by:
flatMapToInt
in interfaceStream<T>
-
flatMapToLong
public LongStream flatMapToLong(Function<? super T,? extends LongStream> mapper)
- Specified by:
flatMapToLong
in interfaceStream<T>
-
flatMapToDouble
public DoubleStream flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
- Specified by:
flatMapToDouble
in interfaceStream<T>
-
sorted
public Stream<T> sorted(Comparator<? super T> comparator)
-
forEachOrdered
public void forEachOrdered(Consumer<? super T> action)
- Specified by:
forEachOrdered
in interfaceStream<T>
-
toArray
public <A> A[] toArray(IntFunction<A[]> generator)
-
reduce
public T reduce(T identity, BinaryOperator<T> accumulator)
-
reduce
public Optional<T> reduce(BinaryOperator<T> accumulator)
-
reduce
public <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
-
collect
public <R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
-
min
public Optional<T> min(Comparator<? super T> comparator)
-
max
public Optional<T> max(Comparator<? super T> comparator)
-
isParallel
public boolean isParallel()
- Specified by:
isParallel
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
iterator
public Iterator<T> iterator()
- Specified by:
iterator
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
spliterator
public Spliterator<T> spliterator()
- Specified by:
spliterator
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
sequential
public S sequential()
- Specified by:
sequential
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
parallel
public S parallel()
- Specified by:
parallel
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
unordered
public S unordered()
- Specified by:
unordered
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
onClose
public S onClose(Runnable closeHandler)
- Specified by:
onClose
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
execute
protected void execute(Runnable terminalOperation)
-
execute
protected <R> R execute(Callable<R> terminalOperation)
-
-