Class ParallelDoubleStreamSupport
- java.lang.Object
-
- com.github.ferstl.streams.ParallelDoubleStreamSupport
-
- All Implemented Interfaces:
AutoCloseable,BaseStream<Double,DoubleStream>,DoubleStream
public class ParallelDoubleStreamSupport extends Object implements DoubleStream
An implementation of
DoubleStreamwhich uses a customForkJoinPoolfor parallel aggregate operations. This is thedoubleprimitive specialization ofParallelStreamSupport.The following example illustrates an aggregate operation using
ParallelStreamSupportandParallelDoubleStreamSupportwith a customForkJoinPool, computing the sum of the weights of the red widgets:ForkJoinPool pool = new ForkJoinPool(); double sum = ParallelStreamSupport.parallelStream(widgets, pool) .filter(w -> w.getColor() == RED) .mapToDouble(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
DoubleStream, which are meaningful for parallel streams Arrays.stream(double[])StreamSupport.doubleStream(Spliterator.OfDouble, boolean)StreamSupport.doubleStream(Supplier, int, boolean)
- API Note:
Internally, this stream wraps a
doublestream 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 ofParallelStreamSupport,ParallelIntStreamSupportorParallelLongStreamSupport.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.DoubleStream
DoubleStream.Builder
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanallMatch(DoublePredicate predicate)booleananyMatch(DoublePredicate predicate)OptionalDoubleaverage()Stream<Double>boxed()voidclose()<R> Rcollect(Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BiConsumer<R,R> combiner)static DoubleStreamconcat(DoubleStream a, DoubleStream b, ForkJoinPool workerPool)Creates a lazily concatenated paralleldoublestream whose elements are all the elements of the first stream followed by all the elements of the second stream.longcount()DoubleStreamdistinct()DoubleStreamdropWhile(DoublePredicate predicate)protected voidexecute(Runnable terminalOperation)protected <R> Rexecute(Callable<R> terminalOperation)DoubleStreamfilter(DoublePredicate predicate)OptionalDoublefindAny()OptionalDoublefindFirst()DoubleStreamflatMap(DoubleFunction<? extends DoubleStream> mapper)voidforEach(DoubleConsumer action)voidforEachOrdered(DoubleConsumer action)static DoubleStreamgenerate(DoubleSupplier supplier, ForkJoinPool workerPool)Creates a parallel infinite sequential unordereddoublestream where each element is generated by the providedDoubleSupplier.booleanisParallel()static DoubleStreamiterate(double seed, DoubleUnaryOperator operator, ForkJoinPool workerPool)Creates a parallel infinite ordereddoublestream produced by iterative application of a functionfto an initial elementseed.PrimitiveIterator.OfDoubleiterator()DoubleStreamlimit(long maxSize)DoubleStreammap(DoubleUnaryOperator mapper)IntStreammapToInt(DoubleToIntFunction mapper)LongStreammapToLong(DoubleToLongFunction mapper)<U> Stream<U>mapToObj(DoubleFunction<? extends U> mapper)OptionalDoublemax()OptionalDoublemin()booleannoneMatch(DoublePredicate predicate)SonClose(Runnable closeHandler)Sparallel()static DoubleStreamparallelStream(double[] array, ForkJoinPool workerPool)Creates a paralleldoublestream from the given Array.static DoubleStreamparallelStream(Supplier<? extends Spliterator.OfDouble> supplier, int characteristics, ForkJoinPool workerPool)Creates a paralleldoublestream from the given Spliterator supplier.static DoubleStreamparallelStream(Spliterator.OfDouble spliterator, ForkJoinPool workerPool)Creates a paralleldoublestream from the given Spliterator.static DoubleStreamparallelStream(DoubleStream.Builder builder, ForkJoinPool workerPool)Creates a paralleldoublestream from the givenDoubleStream.Builder.DoubleStreampeek(DoubleConsumer action)doublereduce(double identity, DoubleBinaryOperator op)OptionalDoublereduce(DoubleBinaryOperator op)Ssequential()DoubleStreamskip(long n)DoubleStreamsorted()Spliterator.OfDoublespliterator()doublesum()DoubleSummaryStatisticssummaryStatistics()DoubleStreamtakeWhile(DoublePredicate predicate)double[]toArray()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, onClose, unordered
-
Methods inherited from interface java.util.stream.DoubleStream
parallel, sequential
-
-
-
-
Method Detail
-
parallelStream
public static DoubleStream parallelStream(double[] array, ForkJoinPool workerPool)
Creates a paralleldoublestream 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.- 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
doublestream that executes a terminal operation in the givenForkJoinPool. - See Also:
Arrays.stream(int[])
-
parallelStream
public static DoubleStream parallelStream(Spliterator.OfDouble spliterator, ForkJoinPool workerPool)
Creates a paralleldoublestream from the given Spliterator. This operation is similar to callingStreamSupport.doubleStream(spliterator, true)with the difference that a parallel terminal operation will be executed in the givenForkJoinPool.- Parameters:
spliterator- ASpliterator.OfDoubledescribing the stream elements. Must not benull.workerPool- Thread pool for parallel execution of a terminal operation. Must not benull.- Returns:
- A parallel
doublestream that executes a terminal operation in the givenForkJoinPool. - See Also:
StreamSupport.doubleStream(Spliterator.OfDouble, boolean)
-
parallelStream
public static DoubleStream parallelStream(Supplier<? extends Spliterator.OfDouble> supplier, int characteristics, ForkJoinPool workerPool)
Creates a paralleldoublestream from the given Spliterator supplier. This operation is similar to callingStreamSupport.doubleStream(supplier, characteristics, true)with the difference that a parallel terminal operation will be executed in the givenForkJoinPool.- Parameters:
supplier- ASupplierof aSpliterator.OfDouble. 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
doublestream that executes a terminal operation in the givenForkJoinPool. - See Also:
StreamSupport.doubleStream(Supplier, int, boolean)
-
parallelStream
public static DoubleStream parallelStream(DoubleStream.Builder builder, ForkJoinPool workerPool)
Creates a paralleldoublestream from the givenDoubleStream.Builder. This operation is similar to callingbuilder.build().parallel()with the difference that a parallel terminal operation will be executed in the givenForkJoinPool.- 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
doublestream that executes a terminal operation in the givenForkJoinPool. - See Also:
DoubleStream.builder()
-
iterate
public static DoubleStream iterate(double seed, DoubleUnaryOperator operator, ForkJoinPool workerPool)
Creates a parallel infinite ordereddoublestream produced by iterative application of a functionfto an initial elementseed. This operation is similar to callingDoubleStream.iterate(seed, operator).parallel()with the difference that a parallel terminal operation will be executed in the givenForkJoinPool.- 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
doublestream that executes a terminal operation in the givenForkJoinPool. - See Also:
DoubleStream.iterate(double, DoubleUnaryOperator)
-
generate
public static DoubleStream generate(DoubleSupplier supplier, ForkJoinPool workerPool)
Creates a parallel infinite sequential unordereddoublestream where each element is generated by the providedDoubleSupplier. This operation is similar to callingDoubleStream.generate(supplier).parallel()with the difference that a parallel terminal operation will be executed in the givenForkJoinPool.- Parameters:
supplier- TheDoubleSupplierof generated elements. Must not benull.workerPool- Thread pool for parallel execution of a terminal operation. Must not benull.- Returns:
- A parallel
doublestream that executes a terminal operation in the givenForkJoinPool. - See Also:
DoubleStream.generate(DoubleSupplier)
-
concat
public static DoubleStream concat(DoubleStream a, DoubleStream b, ForkJoinPool workerPool)
Creates a lazily concatenated paralleldoublestream whose elements are all the elements of the first stream followed by all the elements of the second stream. This operation is similar to callingDoubleStream.concat(a, b).parallel()with the difference that a parallel terminal operation will be executed in the givenForkJoinPool.- 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:
DoubleStream.concat(DoubleStream, DoubleStream)
-
filter
public DoubleStream filter(DoublePredicate predicate)
- Specified by:
filterin interfaceDoubleStream
-
map
public DoubleStream map(DoubleUnaryOperator mapper)
- Specified by:
mapin interfaceDoubleStream
-
mapToObj
public <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper)
- Specified by:
mapToObjin interfaceDoubleStream
-
mapToInt
public IntStream mapToInt(DoubleToIntFunction mapper)
- Specified by:
mapToIntin interfaceDoubleStream
-
mapToLong
public LongStream mapToLong(DoubleToLongFunction mapper)
- Specified by:
mapToLongin interfaceDoubleStream
-
flatMap
public DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper)
- Specified by:
flatMapin interfaceDoubleStream
-
distinct
public DoubleStream distinct()
- Specified by:
distinctin interfaceDoubleStream
-
sorted
public DoubleStream sorted()
- Specified by:
sortedin interfaceDoubleStream
-
peek
public DoubleStream peek(DoubleConsumer action)
- Specified by:
peekin interfaceDoubleStream
-
limit
public DoubleStream limit(long maxSize)
- Specified by:
limitin interfaceDoubleStream
-
skip
public DoubleStream skip(long n)
- Specified by:
skipin interfaceDoubleStream
-
takeWhile
public DoubleStream takeWhile(DoublePredicate predicate)
- Specified by:
takeWhilein interfaceDoubleStream
-
dropWhile
public DoubleStream dropWhile(DoublePredicate predicate)
- Specified by:
dropWhilein interfaceDoubleStream
-
forEach
public void forEach(DoubleConsumer action)
- Specified by:
forEachin interfaceDoubleStream
-
forEachOrdered
public void forEachOrdered(DoubleConsumer action)
- Specified by:
forEachOrderedin interfaceDoubleStream
-
toArray
public double[] toArray()
- Specified by:
toArrayin interfaceDoubleStream
-
reduce
public double reduce(double identity, DoubleBinaryOperator op)- Specified by:
reducein interfaceDoubleStream
-
reduce
public OptionalDouble reduce(DoubleBinaryOperator op)
- Specified by:
reducein interfaceDoubleStream
-
collect
public <R> R collect(Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BiConsumer<R,R> combiner)
- Specified by:
collectin interfaceDoubleStream
-
sum
public double sum()
- Specified by:
sumin interfaceDoubleStream
-
min
public OptionalDouble min()
- Specified by:
minin interfaceDoubleStream
-
max
public OptionalDouble max()
- Specified by:
maxin interfaceDoubleStream
-
count
public long count()
- Specified by:
countin interfaceDoubleStream
-
average
public OptionalDouble average()
- Specified by:
averagein interfaceDoubleStream
-
summaryStatistics
public DoubleSummaryStatistics summaryStatistics()
- Specified by:
summaryStatisticsin interfaceDoubleStream
-
anyMatch
public boolean anyMatch(DoublePredicate predicate)
- Specified by:
anyMatchin interfaceDoubleStream
-
allMatch
public boolean allMatch(DoublePredicate predicate)
- Specified by:
allMatchin interfaceDoubleStream
-
noneMatch
public boolean noneMatch(DoublePredicate predicate)
- Specified by:
noneMatchin interfaceDoubleStream
-
findFirst
public OptionalDouble findFirst()
- Specified by:
findFirstin interfaceDoubleStream
-
findAny
public OptionalDouble findAny()
- Specified by:
findAnyin interfaceDoubleStream
-
boxed
public Stream<Double> boxed()
- Specified by:
boxedin interfaceDoubleStream
-
iterator
public PrimitiveIterator.OfDouble iterator()
- Specified by:
iteratorin interfaceBaseStream<Double,DoubleStream>- Specified by:
iteratorin interfaceDoubleStream
-
spliterator
public Spliterator.OfDouble spliterator()
- Specified by:
spliteratorin interfaceBaseStream<Double,DoubleStream>- Specified by:
spliteratorin interfaceDoubleStream
-
isParallel
public boolean isParallel()
- Specified by:
isParallelin 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)
-
-