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 ofStreamwhich uses a customForkJoinPoolfor parallel aggregate operations.The following example illustrates an aggregate operation using
ParallelStreamSupportwith 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 asForkJoinTaskin 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,ParallelLongStreamSupportorParallelDoubleStreamSupport.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
Streamand 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 booleanallMatch(Predicate<? super T> predicate)booleananyMatch(Predicate<? super T> predicate)voidclose()<R> Rcollect(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.longcount()Stream<T>distinct()Stream<T>dropWhile(Predicate<? super T> predicate)protected voidexecute(Runnable terminalOperation)protected <R> Rexecute(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)DoubleStreamflatMapToDouble(Function<? super T,? extends DoubleStream> mapper)IntStreamflatMapToInt(Function<? super T,? extends IntStream> mapper)LongStreamflatMapToLong(Function<? super T,? extends LongStream> mapper)voidforEach(Consumer<? super T> action)voidforEachOrdered(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.booleanisParallel()static <T> Stream<T>iterate(T seed, UnaryOperator<T> operator, ForkJoinPool workerPool)Creates a parallel infinite ordered stream produced by iterative application of a functionfto an initial elementseed.Iterator<T>iterator()Stream<T>limit(long maxSize)<R> Stream<R>map(Function<? super T,? extends R> mapper)DoubleStreammapToDouble(ToDoubleFunction<? super T> mapper)IntStreammapToInt(ToIntFunction<? super T> mapper)LongStreammapToLong(ToLongFunction<? super T> mapper)Optional<T>max(Comparator<? super T> comparator)Optional<T>min(Comparator<? super T> comparator)booleannoneMatch(Predicate<? super T> predicate)SonClose(Runnable closeHandler)Sparallel()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)Treduce(T identity, BinaryOperator<T> accumulator)<U> Ureduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)Ssequential()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)Sunordered()-
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- ASpliteratordescribing 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- ASupplierof 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 functionfto 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- TheSupplierof 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:
mapToDoublein interfaceStream<T>
-
flatMapToInt
public IntStream flatMapToInt(Function<? super T,? extends IntStream> mapper)
- Specified by:
flatMapToIntin interfaceStream<T>
-
flatMapToLong
public LongStream flatMapToLong(Function<? super T,? extends LongStream> mapper)
- Specified by:
flatMapToLongin interfaceStream<T>
-
flatMapToDouble
public DoubleStream flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
- Specified by:
flatMapToDoublein interfaceStream<T>
-
sorted
public Stream<T> sorted(Comparator<? super T> comparator)
-
forEachOrdered
public void forEachOrdered(Consumer<? super T> action)
- Specified by:
forEachOrderedin 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:
isParallelin interfaceBaseStream<T,S extends BaseStream<T,S>>
-
iterator
public Iterator<T> iterator()
- Specified by:
iteratorin interfaceBaseStream<T,S extends BaseStream<T,S>>
-
spliterator
public Spliterator<T> spliterator()
- Specified by:
spliteratorin interfaceBaseStream<T,S extends BaseStream<T,S>>
-
sequential
public S sequential()
- Specified by:
sequentialin interfaceBaseStream<T,S extends BaseStream<T,S>>
-
parallel
public S parallel()
- Specified by:
parallelin interfaceBaseStream<T,S extends BaseStream<T,S>>
-
unordered
public S unordered()
- Specified by:
unorderedin interfaceBaseStream<T,S extends BaseStream<T,S>>
-
onClose
public S onClose(Runnable closeHandler)
- Specified by:
onClosein interfaceBaseStream<T,S extends BaseStream<T,S>>
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceBaseStream<T,S extends BaseStream<T,S>>
-
execute
protected void execute(Runnable terminalOperation)
-
execute
protected <R> R execute(Callable<R> terminalOperation)
-
-