public class ParallelDoubleStreamSupport extends Object implements DoubleStream
An implementation of DoubleStream which uses a custom ForkJoinPool for parallel aggregate
operations. This is the double primitive specialization of ParallelStreamSupport.
The following example illustrates an aggregate operation using ParallelStreamSupport and
ParallelDoubleStreamSupport with a custom ForkJoinPool, 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. isParallel() returns true, a
terminal
operation will be executed as ForkJoinTask in the custom ForkJoinPool. Otherwise it will be
executed in the calling thread.
DoubleStream, which are meaningful for parallel streamsArrays.stream(double[])StreamSupport.doubleStream(Spliterator.OfDouble, boolean)StreamSupport.doubleStream(Supplier, int, boolean)
Internally, this stream wraps a double 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 ParallelStreamSupport,
ParallelIntStreamSupport or ParallelLongStreamSupport.
Although each factory method returns a parallel stream, calling sequential() is still possible and leads to
sequential execution of a terminal operation within the calling thread.
See the class documentation for Stream and the package documentation for
java.util.stream for
additional specification.
DoubleStream.Builder| Modifier and Type | Method and Description |
|---|---|
boolean |
allMatch(DoublePredicate predicate)
Returns whether all elements of this stream match the provided predicate.
|
boolean |
anyMatch(DoublePredicate predicate)
Returns whether any elements of this stream match the provided
predicate.
|
OptionalDouble |
average()
Returns an
OptionalDouble describing the arithmetic
mean of elements of this stream, or an empty optional if this
stream is empty. |
Stream<Double> |
boxed()
Returns a
Stream consisting of the elements of this stream,
boxed to Double. |
void |
close()
Closes this stream, causing all close handlers for this stream pipeline
to be called.
|
<R> R |
collect(Supplier<R> supplier,
ObjDoubleConsumer<R> accumulator,
BiConsumer<R,R> combiner)
Performs a mutable
reduction operation on the elements of this stream.
|
static DoubleStream |
concat(DoubleStream a,
DoubleStream b,
ForkJoinPool workerPool)
Creates a lazily concatenated parallel
double stream whose elements are all the elements
of the first stream followed by all the elements of the second stream. |
long |
count()
Returns the count of elements in this stream.
|
DoubleStream |
distinct()
Returns a stream consisting of the distinct elements of this stream.
|
protected <R> R |
execute(Callable<R> terminalOperation) |
protected void |
execute(Runnable terminalOperation) |
DoubleStream |
filter(DoublePredicate predicate)
Returns a stream consisting of the elements of this stream that match
the given predicate.
|
OptionalDouble |
findAny()
Returns an
OptionalDouble describing some element of the stream,
or an empty OptionalDouble if the stream is empty. |
OptionalDouble |
findFirst()
Returns an
OptionalDouble describing the first element of this
stream, or an empty OptionalDouble if the stream is empty. |
DoubleStream |
flatMap(DoubleFunction<? extends DoubleStream> mapper)
Returns a stream consisting of the results of replacing each element of
this stream with the contents of a mapped stream produced by applying
the provided mapping function to each element.
|
void |
forEach(DoubleConsumer action)
Performs an action for each element of this stream.
|
void |
forEachOrdered(DoubleConsumer action)
Performs an action for each element of this stream, guaranteeing that
each element is processed in encounter order for streams that have a
defined encounter order.
|
static DoubleStream |
generate(DoubleSupplier supplier,
ForkJoinPool workerPool)
Creates a parallel infinite sequential unordered
double stream where each element is
generated by the provided DoubleSupplier. |
boolean |
isParallel()
Returns whether this stream, if a terminal operation were to be executed,
would execute in parallel.
|
static DoubleStream |
iterate(double seed,
DoubleUnaryOperator operator,
ForkJoinPool workerPool)
Creates a parallel infinite ordered
double stream produced by iterative application of a
function f to an initial element seed. |
PrimitiveIterator.OfDouble |
iterator()
Returns an iterator for the elements of this stream.
|
DoubleStream |
limit(long maxSize)
Returns a stream consisting of the elements of this stream, truncated
to be no longer than
maxSize in length. |
DoubleStream |
map(DoubleUnaryOperator mapper)
Returns a stream consisting of the results of applying the given
function to the elements of this stream.
|
IntStream |
mapToInt(DoubleToIntFunction mapper)
Returns an
IntStream consisting of the results of applying the
given function to the elements of this stream. |
LongStream |
mapToLong(DoubleToLongFunction mapper)
Returns a
LongStream consisting of the results of applying the
given function to the elements of this stream. |
<U> Stream<U> |
mapToObj(DoubleFunction<? extends U> mapper)
Returns an object-valued
Stream consisting of the results of
applying the given function to the elements of this stream. |
OptionalDouble |
max()
Returns an
OptionalDouble describing the maximum element of this
stream, or an empty OptionalDouble if this stream is empty. |
OptionalDouble |
min()
Returns an
OptionalDouble describing the minimum element of this
stream, or an empty OptionalDouble if this stream is empty. |
boolean |
noneMatch(DoublePredicate predicate)
Returns whether no elements of this stream match the provided predicate.
|
S |
onClose(Runnable closeHandler)
Returns an equivalent stream with an additional close handler.
|
S |
parallel()
Returns an equivalent stream that is parallel.
|
static DoubleStream |
parallelStream(double[] array,
ForkJoinPool workerPool)
Creates a parallel
double stream from the given Array. |
static DoubleStream |
parallelStream(DoubleStream.Builder builder,
ForkJoinPool workerPool)
Creates a parallel
double stream from the given Builder. |
static DoubleStream |
parallelStream(Spliterator.OfDouble spliterator,
ForkJoinPool workerPool)
Creates a parallel
double stream from the given Spliterator. |
static DoubleStream |
parallelStream(Supplier<? extends Spliterator.OfDouble> supplier,
int characteristics,
ForkJoinPool workerPool)
Creates a parallel
double stream from the given Spliterator supplier. |
DoubleStream |
peek(DoubleConsumer action)
Returns a stream consisting of the elements of this stream, additionally
performing the provided action on each element as elements are consumed
from the resulting stream.
|
OptionalDouble |
reduce(DoubleBinaryOperator op)
Performs a reduction on the
elements of this stream, using an
associative accumulation
function, and returns an
OptionalDouble describing the reduced
value, if any. |
double |
reduce(double identity,
DoubleBinaryOperator op)
Performs a reduction on the
elements of this stream, using the provided identity value and an
associative
accumulation function, and returns the reduced value.
|
S |
sequential()
Returns an equivalent stream that is sequential.
|
DoubleStream |
skip(long n)
Returns a stream consisting of the remaining elements of this stream
after discarding the first
n elements of the stream. |
DoubleStream |
sorted()
Returns a stream consisting of the elements of this stream in sorted
order.
|
Spliterator.OfDouble |
spliterator()
Returns a spliterator for the elements of this stream.
|
double |
sum()
Returns the sum of elements in this stream.
|
DoubleSummaryStatistics |
summaryStatistics()
Returns a
DoubleSummaryStatistics describing various summary data
about the elements of this stream. |
double[] |
toArray()
Returns an array containing the elements of this stream.
|
S |
unordered()
Returns an equivalent stream that is
unordered.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitbuilder, concat, empty, generate, iterate, of, of, parallel, sequentialclose, isParallel, onClose, unorderedpublic static DoubleStream parallelStream(double[] array, ForkJoinPool workerPool)
double stream from the given Array. This operation is similar to
calling Arrays.stream(array).parallel() with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool.array - Array to create the parallel stream from. Must not be null.workerPool - Thread pool for parallel execution of a terminal operation. Must not be null.double stream that executes a terminal operation in the given ForkJoinPool.Arrays.stream(int[])public static DoubleStream parallelStream(Spliterator.OfDouble spliterator, ForkJoinPool workerPool)
double stream from the given Spliterator. This operation is similar to
calling StreamSupport.doubleStream(spliterator, true) with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool.spliterator - A Spliterator.OfDouble describing the stream elements. Must not be null.workerPool - Thread pool for parallel execution of a terminal operation. Must not be null.double stream that executes a terminal operation in the given ForkJoinPool.StreamSupport.doubleStream(Spliterator.OfDouble, boolean)public static DoubleStream parallelStream(Supplier<? extends Spliterator.OfDouble> supplier, int characteristics, ForkJoinPool workerPool)
double stream from the given Spliterator supplier. This operation is
similar to calling StreamSupport.doubleStream(supplier, characteristics, true) with the difference that a
parallel
terminal
operation will be executed in the given ForkJoinPool.supplier - A Supplier of a Spliterator.OfDouble. Must not be null.characteristics - Spliterator characteristics of the supplied Spliterator. The characteristics must
be equal to supplier.get().characteristics(), otherwise undefined behavior may occur when terminal
operation commences.workerPool - Thread pool for parallel execution of a terminal operation. Must not be null.double stream that executes a terminal operation in the given ForkJoinPool.StreamSupport.doubleStream(Supplier, int, boolean)public static DoubleStream parallelStream(DoubleStream.Builder builder, ForkJoinPool workerPool)
double stream from the given Builder. This operation is
similar to calling builder.build().parallel() with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool.builder - The builder to create the stream from. Must not be null.workerPool - Thread pool for parallel execution of a terminal operation. Must not be null.double stream that executes a terminal operation in the given ForkJoinPool.DoubleStream.builder()public static DoubleStream iterate(double seed, DoubleUnaryOperator operator, ForkJoinPool workerPool)
double stream produced by iterative application of a
function f to an initial element seed. This operation is similar to calling
DoubleStream.iterate(seed, operator).parallel() with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool.seed - The initial element.operator - A function to be applied to to the previous element to produce a new element. Must not be null.workerPool - Thread pool for parallel execution of a terminal operation. Must not be null.double stream that executes a terminal operation in the given ForkJoinPool.DoubleStream.iterate(double, DoubleUnaryOperator)public static DoubleStream generate(DoubleSupplier supplier, ForkJoinPool workerPool)
double stream where each element is
generated by the provided DoubleSupplier. This operation is similar to calling
DoubleStream.generate(supplier).parallel() with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool.supplier - The DoubleSupplier of generated elements. Must not be null.workerPool - Thread pool for parallel execution of a terminal operation. Must not be null.double stream that executes a terminal operation in the given ForkJoinPool.DoubleStream.generate(DoubleSupplier)public static DoubleStream concat(DoubleStream a, DoubleStream b, ForkJoinPool workerPool)
double 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 calling
DoubleStream.concat(a, b).parallel() with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool.a - The first stream. Must not be null.b - The second stream. Must not be null.workerPool - Thread pool for parallel execution of a terminal operation. Must not be null.ForkJoinPool.DoubleStream.concat(DoubleStream, DoubleStream)public DoubleStream filter(DoublePredicate predicate)
java.util.stream.DoubleStreamThis is an intermediate operation.
filter in interface DoubleStreampredicate - a non-interfering,
stateless
predicate to apply to each element to determine if it
should be includedpublic DoubleStream map(DoubleUnaryOperator mapper)
java.util.stream.DoubleStreamThis is an intermediate operation.
map in interface DoubleStreammapper - a non-interfering,
stateless
function to apply to each elementpublic <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper)
java.util.stream.DoubleStreamStream consisting of the results of
applying the given function to the elements of this stream.
This is an intermediate operation.
mapToObj in interface DoubleStreamU - the element type of the new streammapper - a non-interfering,
stateless
function to apply to each elementpublic IntStream mapToInt(DoubleToIntFunction mapper)
java.util.stream.DoubleStreamIntStream consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToInt in interface DoubleStreammapper - a non-interfering,
stateless
function to apply to each elementpublic LongStream mapToLong(DoubleToLongFunction mapper)
java.util.stream.DoubleStreamLongStream consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToLong in interface DoubleStreammapper - a non-interfering,
stateless
function to apply to each elementpublic DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper)
java.util.stream.DoubleStreamclosed after its contents
have been placed into this stream. (If a mapped stream is null
an empty stream is used, instead.)
This is an intermediate operation.
flatMap in interface DoubleStreammapper - a non-interfering,
stateless
function to apply to each element which produces a
DoubleStream of new valuesStream.flatMap(Function)public DoubleStream distinct()
java.util.stream.DoubleStreamDouble.compare(double, double).
This is a stateful intermediate operation.
distinct in interface DoubleStreampublic DoubleStream sorted()
java.util.stream.DoubleStreamDouble.compare(double, double).
This is a stateful intermediate operation.
sorted in interface DoubleStreampublic DoubleStream peek(DoubleConsumer action)
java.util.stream.DoubleStreamThis is an intermediate operation.
For parallel stream pipelines, the action may be called at whatever time and in whatever thread the element is made available by the upstream operation. If the action modifies shared state, it is responsible for providing the required synchronization.
peek in interface DoubleStreamaction - a
non-interfering action to perform on the elements as
they are consumed from the streampublic DoubleStream limit(long maxSize)
java.util.stream.DoubleStreammaxSize in length.
limit in interface DoubleStreammaxSize - the number of elements the stream should be limited topublic DoubleStream skip(long n)
java.util.stream.DoubleStreamn elements of the stream.
If this stream contains fewer than n elements then an
empty stream will be returned.
This is a stateful intermediate operation.
skip in interface DoubleStreamn - the number of leading elements to skippublic void forEach(DoubleConsumer action)
java.util.stream.DoubleStreamThis is a terminal operation.
For parallel stream pipelines, this operation does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism. For any given element, the action may be performed at whatever time and in whatever thread the library chooses. If the action accesses shared state, it is responsible for providing the required synchronization.
forEach in interface DoubleStreamaction - a
non-interfering action to perform on the elementspublic void forEachOrdered(DoubleConsumer action)
java.util.stream.DoubleStreamThis is a terminal operation.
forEachOrdered in interface DoubleStreamaction - a
non-interfering action to perform on the elementsDoubleStream.forEach(DoubleConsumer)public double[] toArray()
java.util.stream.DoubleStreamThis is a terminal operation.
toArray in interface DoubleStreampublic double reduce(double identity,
DoubleBinaryOperator op)
java.util.stream.DoubleStream
double result = identity;
for (double element : this stream)
result = accumulator.applyAsDouble(result, element)
return result;
but is not constrained to execute sequentially.
The identity value must be an identity for the accumulator
function. This means that for all x,
accumulator.apply(identity, x) is equal to x.
The accumulator function must be an
associative function.
This is a terminal operation.
reduce in interface DoubleStreamidentity - the identity value for the accumulating functionop - an associative,
non-interfering,
stateless
function for combining two valuesDoubleStream.sum(),
DoubleStream.min(),
DoubleStream.max(),
DoubleStream.average()public OptionalDouble reduce(DoubleBinaryOperator op)
java.util.stream.DoubleStreamOptionalDouble describing the reduced
value, if any. This is equivalent to:
boolean foundAny = false;
double result = null;
for (double element : this stream) {
if (!foundAny) {
foundAny = true;
result = element;
}
else
result = accumulator.applyAsDouble(result, element);
}
return foundAny ? OptionalDouble.of(result) : OptionalDouble.empty();
but is not constrained to execute sequentially.
The accumulator function must be an
associative function.
This is a terminal operation.
reduce in interface DoubleStreamop - an associative,
non-interfering,
stateless
function for combining two valuesDoubleStream.reduce(double, DoubleBinaryOperator)public <R> R collect(Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BiConsumer<R,R> combiner)
java.util.stream.DoubleStreamArrayList, and elements are incorporated by updating
the state of the result rather than by replacing the result. This
produces a result equivalent to:
R result = supplier.get();
for (double element : this stream)
accumulator.accept(result, element);
return result;
Like DoubleStream.reduce(double, DoubleBinaryOperator), collect
operations can be parallelized without requiring additional
synchronization.
This is a terminal operation.
collect in interface DoubleStreamR - type of the resultsupplier - a function that creates a new result container. For a
parallel execution, this function may be called
multiple times and must return a fresh value each time.accumulator - an associative,
non-interfering,
stateless
function for incorporating an additional element into a resultcombiner - an associative,
non-interfering,
stateless
function for combining two values, which must be
compatible with the accumulator functionStream.collect(Supplier, BiConsumer, BiConsumer)public double sum()
java.util.stream.DoubleStream
return reduce(0, Double::sum);
However, since floating-point summation is not exact, the above
code is not necessarily equivalent to the summation computation
done by this method.
If any stream element is a NaN or the sum is at any point a NaN
then the sum will be NaN.
The value of a floating-point sum is a function both
of the input values as well as the order of addition
operations. The order of addition operations of this method is
intentionally not defined to allow for implementation
flexibility to improve the speed and accuracy of the computed
result.
In particular, this method may be implemented using compensated
summation or other technique to reduce the error bound in the
numerical sum compared to a simple summation of double
values.
This is a terminal operation.
sum in interface DoubleStreampublic OptionalDouble min()
java.util.stream.DoubleStreamOptionalDouble describing the minimum element of this
stream, or an empty OptionalDouble if this stream is empty. The minimum
element will be Double.NaN if any stream element was NaN. Unlike
the numerical comparison operators, this method considers negative zero
to be strictly smaller than positive zero. This is a special case of a
reduction and is
equivalent to:
return reduce(Double::min);
This is a terminal operation.
min in interface DoubleStreamOptionalDouble containing the minimum element of this
stream, or an empty optional if the stream is emptypublic OptionalDouble max()
java.util.stream.DoubleStreamOptionalDouble describing the maximum element of this
stream, or an empty OptionalDouble if this stream is empty. The maximum
element will be Double.NaN if any stream element was NaN. Unlike
the numerical comparison operators, this method considers negative zero
to be strictly smaller than positive zero. This is a
special case of a
reduction and is
equivalent to:
return reduce(Double::max);
This is a terminal operation.
max in interface DoubleStreamOptionalDouble containing the maximum element of this
stream, or an empty optional if the stream is emptypublic long count()
java.util.stream.DoubleStream
return mapToLong(e -> 1L).sum();
This is a terminal operation.
count in interface DoubleStreampublic OptionalDouble average()
java.util.stream.DoubleStreamOptionalDouble describing the arithmetic
mean of elements of this stream, or an empty optional if this
stream is empty.
If any recorded value is a NaN or the sum is at any point a NaN
then the average will be NaN.
The average returned can vary depending upon the order in
which values are recorded.
This method may be implemented using compensated summation or
other technique to reduce the error bound in the numerical sum used to compute the average.
The average is a special case of a reduction.
This is a terminal operation.
average in interface DoubleStreamOptionalDouble containing the average element of this
stream, or an empty optional if the stream is emptypublic DoubleSummaryStatistics summaryStatistics()
java.util.stream.DoubleStreamDoubleSummaryStatistics describing various summary data
about the elements of this stream. This is a special
case of a reduction.
This is a terminal operation.
summaryStatistics in interface DoubleStreamDoubleSummaryStatistics describing various summary data
about the elements of this streampublic boolean anyMatch(DoublePredicate predicate)
java.util.stream.DoubleStreamfalse is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
anyMatch in interface DoubleStreampredicate - a non-interfering,
stateless
predicate to apply to elements of this streamtrue if any elements of the stream match the provided
predicate, otherwise falsepublic boolean allMatch(DoublePredicate predicate)
java.util.stream.DoubleStreamtrue is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
allMatch in interface DoubleStreampredicate - a non-interfering,
stateless
predicate to apply to elements of this streamtrue if either all elements of the stream match the
provided predicate or the stream is empty, otherwise falsepublic boolean noneMatch(DoublePredicate predicate)
java.util.stream.DoubleStreamtrue is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
noneMatch in interface DoubleStreampredicate - a non-interfering,
stateless
predicate to apply to elements of this streamtrue if either no elements of the stream match the
provided predicate or the stream is empty, otherwise falsepublic OptionalDouble findFirst()
java.util.stream.DoubleStreamOptionalDouble describing the first element of this
stream, or an empty OptionalDouble if the stream is empty. If
the stream has no encounter order, then any element may be returned.
This is a short-circuiting terminal operation.
findFirst in interface DoubleStreamOptionalDouble describing the first element of this
stream, or an empty OptionalDouble if the stream is emptypublic OptionalDouble findAny()
java.util.stream.DoubleStreamOptionalDouble describing some element of the stream,
or an empty OptionalDouble if the stream is empty.
This is a short-circuiting terminal operation.
The behavior of this operation is explicitly nondeterministic; it is
free to select any element in the stream. This is to allow for maximal
performance in parallel operations; the cost is that multiple invocations
on the same source may not return the same result. (If a stable result
is desired, use DoubleStream.findFirst() instead.)
findAny in interface DoubleStreamOptionalDouble describing some element of this stream,
or an empty OptionalDouble if the stream is emptyDoubleStream.findFirst()public Stream<Double> boxed()
java.util.stream.DoubleStreamStream consisting of the elements of this stream,
boxed to Double.
This is an intermediate operation.
boxed in interface DoubleStreamStream consistent of the elements of this stream,
each boxed to a Doublepublic PrimitiveIterator.OfDouble iterator()
java.util.stream.BaseStreamThis is a terminal operation.
iterator in interface BaseStream<Double,DoubleStream>iterator in interface DoubleStreampublic Spliterator.OfDouble spliterator()
java.util.stream.BaseStreamThis is a terminal operation.
spliterator in interface BaseStream<Double,DoubleStream>spliterator in interface DoubleStreampublic boolean isParallel()
java.util.stream.BaseStreamisParallel in interface BaseStream<T,S extends BaseStream<T,S>>true if this stream would execute in parallel if executedpublic S sequential()
java.util.stream.BaseStreamThis is an intermediate operation.
sequential in interface BaseStream<T,S extends BaseStream<T,S>>public S parallel()
java.util.stream.BaseStreamThis is an intermediate operation.
parallel in interface BaseStream<T,S extends BaseStream<T,S>>public S unordered()
java.util.stream.BaseStreamThis is an intermediate operation.
unordered in interface BaseStream<T,S extends BaseStream<T,S>>public S onClose(Runnable closeHandler)
java.util.stream.BaseStreamBaseStream.close() method
is called on the stream, and are executed in the order they were
added. All close handlers are run, even if earlier close handlers throw
exceptions. If any close handler throws an exception, the first
exception thrown will be relayed to the caller of close(), with
any remaining exceptions added to that exception as suppressed exceptions
(unless one of the remaining exceptions is the same exception as the
first exception, since an exception cannot suppress itself.) May
return itself.
This is an intermediate operation.
onClose in interface BaseStream<T,S extends BaseStream<T,S>>closeHandler - A task to execute when the stream is closedpublic void close()
java.util.stream.BaseStreamclose in interface AutoCloseableclose in interface BaseStream<T,S extends BaseStream<T,S>>AutoCloseable.close()protected void execute(Runnable terminalOperation)
protected <R> R execute(Callable<R> terminalOperation)
Copyright © 2019. All rights reserved.