Package com.infilos.utils.stream
Class ParallelDoubleStream
- java.lang.Object
-
- com.infilos.utils.stream.ParallelDoubleStream
-
- All Implemented Interfaces:
AutoCloseable
,BaseStream<Double,DoubleStream>
,DoubleStream
public class ParallelDoubleStream extends Object implements DoubleStream
An implementation of
DoubleStream
which uses a customForkJoinPool
for parallel aggregate operations. This is thedouble
primitive specialization ofParallelStream
.The following example illustrates an aggregate operation using
ParallelStream
andParallelDoubleStream
with 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 asForkJoinTask
in 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)
-
-
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 boolean
allMatch(DoublePredicate predicate)
boolean
anyMatch(DoublePredicate predicate)
OptionalDouble
average()
Stream<Double>
boxed()
void
close()
<R> R
collect(Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BiConsumer<R,R> combiner)
static DoubleStream
concat(DoubleStream a, DoubleStream b, ForkJoinPool workerPool)
Creates a lazily concatenated paralleldouble
stream whose elements are all the elements of the first stream followed by all the elements of the second stream.long
count()
DoubleStream
distinct()
protected void
execute(Runnable terminalOperation)
protected <R> R
execute(Callable<R> terminalOperation)
DoubleStream
filter(DoublePredicate predicate)
OptionalDouble
findAny()
OptionalDouble
findFirst()
DoubleStream
flatMap(DoubleFunction<? extends DoubleStream> mapper)
void
forEach(DoubleConsumer action)
void
forEachOrdered(DoubleConsumer action)
static DoubleStream
generate(DoubleSupplier supplier, ForkJoinPool workerPool)
Creates a parallel infinite sequential unordereddouble
stream where each element is generated by the providedDoubleSupplier
.boolean
isParallel()
static DoubleStream
iterate(double seed, DoubleUnaryOperator operator, ForkJoinPool workerPool)
Creates a parallel infinite ordereddouble
stream produced by iterative application of a functionf
to an initial elementseed
.PrimitiveIterator.OfDouble
iterator()
DoubleStream
limit(long maxSize)
DoubleStream
map(DoubleUnaryOperator mapper)
IntStream
mapToInt(DoubleToIntFunction mapper)
LongStream
mapToLong(DoubleToLongFunction mapper)
<U> Stream<U>
mapToObj(DoubleFunction<? extends U> mapper)
OptionalDouble
max()
OptionalDouble
min()
boolean
noneMatch(DoublePredicate predicate)
S
onClose(Runnable closeHandler)
S
parallel()
DoubleStream
peek(DoubleConsumer action)
double
reduce(double identity, DoubleBinaryOperator op)
OptionalDouble
reduce(DoubleBinaryOperator op)
S
sequential()
DoubleStream
skip(long n)
DoubleStream
sorted()
Spliterator.OfDouble
spliterator()
static DoubleStream
submit(double[] array, ForkJoinPool workerPool)
Creates a paralleldouble
stream from the given Array.static DoubleStream
submit(Supplier<? extends Spliterator.OfDouble> supplier, int characteristics, ForkJoinPool workerPool)
Creates a paralleldouble
stream from the given Spliterator supplier.static DoubleStream
submit(Spliterator.OfDouble spliterator, ForkJoinPool workerPool)
Creates a paralleldouble
stream from the given Spliterator.static DoubleStream
submit(DoubleStream.Builder builder, ForkJoinPool workerPool)
Creates a paralleldouble
stream from the givenDoubleStream.Builder
.double
sum()
DoubleSummaryStatistics
summaryStatistics()
double[]
toArray()
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, onClose, unordered
-
Methods inherited from interface java.util.stream.DoubleStream
dropWhile, parallel, sequential, takeWhile
-
-
-
-
Method Detail
-
submit
public static DoubleStream submit(double[] array, ForkJoinPool workerPool)
Creates a paralleldouble
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
.- 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
double
stream that executes a terminal operation in the givenForkJoinPool
. - See Also:
Arrays.stream(int[])
-
submit
public static DoubleStream submit(Spliterator.OfDouble spliterator, ForkJoinPool workerPool)
Creates a paralleldouble
stream 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.OfDouble
describing the stream elements. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel
double
stream that executes a terminal operation in the givenForkJoinPool
. - See Also:
StreamSupport.doubleStream(Spliterator.OfDouble, boolean)
-
submit
public static DoubleStream submit(Supplier<? extends Spliterator.OfDouble> supplier, int characteristics, ForkJoinPool workerPool)
Creates a paralleldouble
stream 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
- ASupplier
of 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
double
stream that executes a terminal operation in the givenForkJoinPool
. - See Also:
StreamSupport.doubleStream(Supplier, int, boolean)
-
submit
public static DoubleStream submit(DoubleStream.Builder builder, ForkJoinPool workerPool)
Creates a paralleldouble
stream 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
double
stream 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 ordereddouble
stream produced by iterative application of a functionf
to 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
double
stream 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 unordereddouble
stream 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
- TheDoubleSupplier
of generated elements. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel
double
stream 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 paralleldouble
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 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:
filter
in interfaceDoubleStream
-
map
public DoubleStream map(DoubleUnaryOperator mapper)
- Specified by:
map
in interfaceDoubleStream
-
mapToObj
public <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper)
- Specified by:
mapToObj
in interfaceDoubleStream
-
mapToInt
public IntStream mapToInt(DoubleToIntFunction mapper)
- Specified by:
mapToInt
in interfaceDoubleStream
-
mapToLong
public LongStream mapToLong(DoubleToLongFunction mapper)
- Specified by:
mapToLong
in interfaceDoubleStream
-
flatMap
public DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper)
- Specified by:
flatMap
in interfaceDoubleStream
-
distinct
public DoubleStream distinct()
- Specified by:
distinct
in interfaceDoubleStream
-
sorted
public DoubleStream sorted()
- Specified by:
sorted
in interfaceDoubleStream
-
peek
public DoubleStream peek(DoubleConsumer action)
- Specified by:
peek
in interfaceDoubleStream
-
limit
public DoubleStream limit(long maxSize)
- Specified by:
limit
in interfaceDoubleStream
-
skip
public DoubleStream skip(long n)
- Specified by:
skip
in interfaceDoubleStream
-
forEach
public void forEach(DoubleConsumer action)
- Specified by:
forEach
in interfaceDoubleStream
-
forEachOrdered
public void forEachOrdered(DoubleConsumer action)
- Specified by:
forEachOrdered
in interfaceDoubleStream
-
toArray
public double[] toArray()
- Specified by:
toArray
in interfaceDoubleStream
-
reduce
public double reduce(double identity, DoubleBinaryOperator op)
- Specified by:
reduce
in interfaceDoubleStream
-
reduce
public OptionalDouble reduce(DoubleBinaryOperator op)
- Specified by:
reduce
in interfaceDoubleStream
-
collect
public <R> R collect(Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BiConsumer<R,R> combiner)
- Specified by:
collect
in interfaceDoubleStream
-
sum
public double sum()
- Specified by:
sum
in interfaceDoubleStream
-
min
public OptionalDouble min()
- Specified by:
min
in interfaceDoubleStream
-
max
public OptionalDouble max()
- Specified by:
max
in interfaceDoubleStream
-
count
public long count()
- Specified by:
count
in interfaceDoubleStream
-
average
public OptionalDouble average()
- Specified by:
average
in interfaceDoubleStream
-
summaryStatistics
public DoubleSummaryStatistics summaryStatistics()
- Specified by:
summaryStatistics
in interfaceDoubleStream
-
anyMatch
public boolean anyMatch(DoublePredicate predicate)
- Specified by:
anyMatch
in interfaceDoubleStream
-
allMatch
public boolean allMatch(DoublePredicate predicate)
- Specified by:
allMatch
in interfaceDoubleStream
-
noneMatch
public boolean noneMatch(DoublePredicate predicate)
- Specified by:
noneMatch
in interfaceDoubleStream
-
findFirst
public OptionalDouble findFirst()
- Specified by:
findFirst
in interfaceDoubleStream
-
findAny
public OptionalDouble findAny()
- Specified by:
findAny
in interfaceDoubleStream
-
boxed
public Stream<Double> boxed()
- Specified by:
boxed
in interfaceDoubleStream
-
iterator
public PrimitiveIterator.OfDouble iterator()
- Specified by:
iterator
in interfaceBaseStream<Double,DoubleStream>
- Specified by:
iterator
in interfaceDoubleStream
-
spliterator
public Spliterator.OfDouble spliterator()
- Specified by:
spliterator
in interfaceBaseStream<Double,DoubleStream>
- Specified by:
spliterator
in interfaceDoubleStream
-
isParallel
public boolean isParallel()
- Specified by:
isParallel
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)
-
-