public class ParallelLongStreamSupport extends Object implements LongStream
An implementation of LongStream which uses a custom ForkJoinPool for parallel aggregate operations.
This is the long primitive specialization of ParallelStreamSupport.
The following example illustrates an aggregate operation using ParallelStreamSupport and
ParallelLongStreamSupport with a custom ForkJoinPool, computing the sum of the weights of the red
widgets:
ForkJoinPool pool = new ForkJoinPool();
long sum = ParallelStreamSupport.parallelStream(widgets, pool)
.filter(w -> w.getColor() == RED)
.mapToLong(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.
LongStream, which are meaningful for parallel streamsArrays.stream(long[])StreamSupport.longStream(Spliterator.OfLong, boolean)StreamSupport.longStream(Supplier, int, boolean)
Internally, this stream wraps a long 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
ParallelDoubleStreamSupport.
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.
LongStream.Builder| Modifier and Type | Method and Description |
|---|---|
boolean |
allMatch(LongPredicate predicate)
Returns whether all elements of this stream match the provided predicate.
|
boolean |
anyMatch(LongPredicate predicate)
Returns whether any elements of this stream match the provided
predicate.
|
DoubleStream |
asDoubleStream()
Returns a
DoubleStream consisting of the elements of this stream,
converted to double. |
OptionalDouble |
average()
Returns an
OptionalDouble describing the arithmetic mean of elements of
this stream, or an empty optional if this stream is empty. |
Stream<Long> |
boxed()
Returns a
Stream consisting of the elements of this stream,
each boxed to a Long. |
void |
close()
Closes this stream, causing all close handlers for this stream pipeline
to be called.
|
<R> R |
collect(Supplier<R> supplier,
ObjLongConsumer<R> accumulator,
BiConsumer<R,R> combiner)
Performs a mutable
reduction operation on the elements of this stream.
|
static LongStream |
concat(LongStream a,
LongStream b,
ForkJoinPool workerPool)
Creates a lazily concatenated parallel
long 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.
|
LongStream |
distinct()
Returns a stream consisting of the distinct elements of this stream.
|
protected <R> R |
execute(Callable<R> terminalOperation) |
protected void |
execute(Runnable terminalOperation) |
LongStream |
filter(LongPredicate predicate)
Returns a stream consisting of the elements of this stream that match
the given predicate.
|
OptionalLong |
findAny()
Returns an
OptionalLong describing some element of the stream, or
an empty OptionalLong if the stream is empty. |
OptionalLong |
findFirst()
Returns an
OptionalLong describing the first element of this
stream, or an empty OptionalLong if the stream is empty. |
LongStream |
flatMap(LongFunction<? extends LongStream> 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(LongConsumer action)
Performs an action for each element of this stream.
|
void |
forEachOrdered(LongConsumer 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 LongStream |
generate(LongSupplier supplier,
ForkJoinPool workerPool)
Creates a parallel infinite sequential unordered
long stream where each element is
generated by the provided LongSupplier. |
boolean |
isParallel()
Returns whether this stream, if a terminal operation were to be executed,
would execute in parallel.
|
static LongStream |
iterate(long seed,
LongUnaryOperator operator,
ForkJoinPool workerPool)
Creates a parallel infinite ordered
long stream produced by iterative application of a
function f to an initial element seed. |
PrimitiveIterator.OfLong |
iterator()
Returns an iterator for the elements of this stream.
|
LongStream |
limit(long maxSize)
Returns a stream consisting of the elements of this stream, truncated
to be no longer than
maxSize in length. |
LongStream |
map(LongUnaryOperator mapper)
Returns a stream consisting of the results of applying the given
function to the elements of this stream.
|
DoubleStream |
mapToDouble(LongToDoubleFunction mapper)
Returns a
DoubleStream consisting of the results of applying the
given function to the elements of this stream. |
IntStream |
mapToInt(LongToIntFunction mapper)
Returns an
IntStream consisting of the results of applying the
given function to the elements of this stream. |
<U> Stream<U> |
mapToObj(LongFunction<? extends U> mapper)
Returns an object-valued
Stream consisting of the results of
applying the given function to the elements of this stream. |
OptionalLong |
max()
Returns an
OptionalLong describing the maximum element of this
stream, or an empty optional if this stream is empty. |
OptionalLong |
min()
Returns an
OptionalLong describing the minimum element of this
stream, or an empty optional if this stream is empty. |
boolean |
noneMatch(LongPredicate 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 LongStream |
parallelStream(long[] array,
ForkJoinPool workerPool)
Creates a parallel
long stream from the given Array. |
static LongStream |
parallelStream(LongStream.Builder builder,
ForkJoinPool workerPool)
Creates a parallel
long stream from the given Builder. |
static LongStream |
parallelStream(Spliterator.OfLong spliterator,
ForkJoinPool workerPool)
Creates a parallel
long stream from the given Spliterator. |
static LongStream |
parallelStream(Supplier<? extends Spliterator.OfLong> supplier,
int characteristics,
ForkJoinPool workerPool)
Creates a parallel
long stream from the given Spliterator supplier. |
LongStream |
peek(LongConsumer 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.
|
static LongStream |
range(long startInclusive,
long endExclusive,
ForkJoinPool workerPool)
Creates a parallel ordered
long stream from startInclusive (inclusive) to
endExclusive (exclusive) by an incremental step of 1. |
static LongStream |
rangeClosed(long startInclusive,
long endInclusive,
ForkJoinPool workerPool)
Creates a parallel ordered
long stream from startInclusive (inclusive) to
endInclusive (inclusive) by an incremental step of 1. |
OptionalLong |
reduce(LongBinaryOperator op)
Performs a reduction on the
elements of this stream, using an
associative accumulation
function, and returns an
OptionalLong describing the reduced value,
if any. |
long |
reduce(long identity,
LongBinaryOperator 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.
|
LongStream |
skip(long n)
Returns a stream consisting of the remaining elements of this stream
after discarding the first
n elements of the stream. |
LongStream |
sorted()
Returns a stream consisting of the elements of this stream in sorted
order.
|
Spliterator.OfLong |
spliterator()
Returns a spliterator for the elements of this stream.
|
long |
sum()
Returns the sum of elements in this stream.
|
LongSummaryStatistics |
summaryStatistics()
Returns a
LongSummaryStatistics describing various summary data
about the elements of this stream. |
long[] |
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, range, rangeClosed, sequentialclose, isParallel, onClose, unorderedpublic static LongStream parallelStream(long[] array, ForkJoinPool workerPool)
long 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.long stream that executes a terminal operation in the given ForkJoinPool.Arrays.stream(long[])public static LongStream parallelStream(Spliterator.OfLong spliterator, ForkJoinPool workerPool)
long stream from the given Spliterator. This operation is similar to
calling StreamSupport.longStream(spliterator, true) with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool.spliterator - A Spliterator.OfLong describing the stream elements. Must not be null.workerPool - Thread pool for parallel execution of a terminal operation. Must not be null.long stream that executes a terminal operation in the given ForkJoinPool.StreamSupport.longStream(Spliterator.OfLong, boolean)public static LongStream parallelStream(Supplier<? extends Spliterator.OfLong> supplier, int characteristics, ForkJoinPool workerPool)
long stream from the given Spliterator supplier. This operation is
similar to calling StreamSupport.longStream(supplier, characteristics, true) with the difference that a
parallel
terminal
operation will be executed in the given ForkJoinPool.supplier - A Supplier of a Spliterator.OfLong. 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.long stream that executes a terminal operation in the given ForkJoinPool.StreamSupport.longStream(Supplier, int, boolean)public static LongStream parallelStream(LongStream.Builder builder, ForkJoinPool workerPool)
long 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.long stream that executes a terminal operation in the given ForkJoinPool.
builder()public static LongStream iterate(long seed, LongUnaryOperator operator, ForkJoinPool workerPool)
long stream produced by iterative application of a
function f to an initial element seed. This operation is similar to calling
LongStream.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.long stream that executes a terminal operation in the given ForkJoinPool.LongStream.iterate(long, LongUnaryOperator)public static LongStream generate(LongSupplier supplier, ForkJoinPool workerPool)
long stream where each element is
generated by the provided LongSupplier. This operation is similar to calling
LongStream.generate(supplier).parallel() with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool.supplier - The LongSupplier of generated elements. Must not be null.workerPool - Thread pool for parallel execution of a terminal operation. Must not be null.long stream that executes a terminal operation in the given ForkJoinPool.LongStream.generate(LongSupplier)public static LongStream range(long startInclusive, long endExclusive, ForkJoinPool workerPool)
long stream from startInclusive (inclusive) to
endExclusive (exclusive) by an incremental step of 1. This operation is similar to calling
LongStream.range(startInclusive, endExclusive).parallel() with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool.startInclusive - the (inclusive) initial valueendExclusive - the exclusive upper boundworkerPool - Thread pool for parallel execution of a terminal operation. Must not be null.long stream that executes a terminal operation in the given ForkJoinPool for
the range of long elements.LongStream.range(long, long)public static LongStream rangeClosed(long startInclusive, long endInclusive, ForkJoinPool workerPool)
long stream from startInclusive (inclusive) to
endInclusive (inclusive) by an incremental step of 1. This operation is similar to calling
LongStream.rangeClosed(startInclusive, endInclusive).parallel() with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool.startInclusive - the (inclusive) initial valueendInclusive - the inclusive upper boundworkerPool - Thread pool for parallel execution of a terminal operation. Must not be null.long stream that executes a terminal operation in the given ForkJoinPool for
the range of long elements.LongStream.rangeClosed(long, long)public static LongStream concat(LongStream a, LongStream b, ForkJoinPool workerPool)
long 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
LongStream.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.LongStream.concat(LongStream, LongStream)public LongStream filter(LongPredicate predicate)
java.util.stream.LongStreamThis is an intermediate operation.
filter in interface LongStreampredicate - a non-interfering,
stateless
predicate to apply to each element to determine if it
should be includedpublic LongStream map(LongUnaryOperator mapper)
java.util.stream.LongStreamThis is an intermediate operation.
map in interface LongStreammapper - a non-interfering,
stateless
function to apply to each elementpublic <U> Stream<U> mapToObj(LongFunction<? extends U> mapper)
java.util.stream.LongStreamStream consisting of the results of
applying the given function to the elements of this stream.
This is an intermediate operation.
mapToObj in interface LongStreamU - the element type of the new streammapper - a non-interfering,
stateless
function to apply to each elementpublic IntStream mapToInt(LongToIntFunction mapper)
java.util.stream.LongStreamIntStream consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToInt in interface LongStreammapper - a non-interfering,
stateless
function to apply to each elementpublic DoubleStream mapToDouble(LongToDoubleFunction mapper)
java.util.stream.LongStreamDoubleStream consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToDouble in interface LongStreammapper - a non-interfering,
stateless
function to apply to each elementpublic LongStream flatMap(LongFunction<? extends LongStream> mapper)
java.util.stream.LongStreamclosed 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 LongStreammapper - a non-interfering,
stateless
function to apply to each element which produces a
LongStream of new valuesStream.flatMap(Function)public LongStream distinct()
java.util.stream.LongStreamThis is a stateful intermediate operation.
distinct in interface LongStreampublic LongStream sorted()
java.util.stream.LongStreamThis is a stateful intermediate operation.
sorted in interface LongStreampublic LongStream peek(LongConsumer action)
java.util.stream.LongStreamThis 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 LongStreamaction - a
non-interfering action to perform on the elements as
they are consumed from the streampublic LongStream limit(long maxSize)
java.util.stream.LongStreammaxSize in length.
limit in interface LongStreammaxSize - the number of elements the stream should be limited topublic LongStream skip(long n)
java.util.stream.LongStreamn 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 LongStreamn - the number of leading elements to skippublic void forEach(LongConsumer action)
java.util.stream.LongStreamThis 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 LongStreamaction - a
non-interfering action to perform on the elementspublic void forEachOrdered(LongConsumer action)
java.util.stream.LongStreamThis is a terminal operation.
forEachOrdered in interface LongStreamaction - a
non-interfering action to perform on the elementsLongStream.forEach(LongConsumer)public long[] toArray()
java.util.stream.LongStreamThis is a terminal operation.
toArray in interface LongStreampublic long reduce(long identity,
LongBinaryOperator op)
java.util.stream.LongStream
long result = identity;
for (long element : this stream)
result = accumulator.applyAsLong(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 LongStreamidentity - the identity value for the accumulating functionop - an associative,
non-interfering,
stateless
function for combining two valuesLongStream.sum(),
LongStream.min(),
LongStream.max(),
LongStream.average()public OptionalLong reduce(LongBinaryOperator op)
java.util.stream.LongStreamOptionalLong describing the reduced value,
if any. This is equivalent to:
boolean foundAny = false;
long result = null;
for (long element : this stream) {
if (!foundAny) {
foundAny = true;
result = element;
}
else
result = accumulator.applyAsLong(result, element);
}
return foundAny ? OptionalLong.of(result) : OptionalLong.empty();
but is not constrained to execute sequentially.
The accumulator function must be an
associative function.
This is a terminal operation.
reduce in interface LongStreamop - an associative,
non-interfering,
stateless
function for combining two valuesLongStream.reduce(long, LongBinaryOperator)public <R> R collect(Supplier<R> supplier, ObjLongConsumer<R> accumulator, BiConsumer<R,R> combiner)
java.util.stream.LongStreamArrayList, 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 (long element : this stream)
accumulator.accept(result, element);
return result;
Like LongStream.reduce(long, LongBinaryOperator), collect operations
can be parallelized without requiring additional synchronization.
This is a terminal operation.
collect in interface LongStreamR - 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 long sum()
java.util.stream.LongStream
return reduce(0, Long::sum);
This is a terminal operation.
sum in interface LongStreampublic OptionalLong min()
java.util.stream.LongStreamOptionalLong describing the minimum element of this
stream, or an empty optional if this stream is empty. This is a special
case of a reduction
and is equivalent to:
return reduce(Long::min);
This is a terminal operation.
min in interface LongStreamOptionalLong containing the minimum element of this
stream, or an empty OptionalLong if the stream is emptypublic OptionalLong max()
java.util.stream.LongStreamOptionalLong describing the maximum element of this
stream, or an empty optional if this stream is empty. This is a special
case of a reduction
and is equivalent to:
return reduce(Long::max);
This is a terminal operation.
max in interface LongStreamOptionalLong containing the maximum element of this
stream, or an empty OptionalLong if the stream is emptypublic long count()
java.util.stream.LongStream
return map(e -> 1L).sum();
This is a terminal operation.
count in interface LongStreampublic OptionalDouble average()
java.util.stream.LongStreamOptionalDouble describing the arithmetic mean of elements of
this stream, or an empty optional if this stream is empty. This is a
special case of a
reduction.
This is a terminal operation.
average in interface LongStreamOptionalDouble containing the average element of this
stream, or an empty optional if the stream is emptypublic LongSummaryStatistics summaryStatistics()
java.util.stream.LongStreamLongSummaryStatistics 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 LongStreamLongSummaryStatistics describing various summary data
about the elements of this streampublic boolean anyMatch(LongPredicate predicate)
java.util.stream.LongStreamfalse is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
anyMatch in interface LongStreampredicate - 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(LongPredicate predicate)
java.util.stream.LongStreamtrue is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
allMatch in interface LongStreampredicate - 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(LongPredicate predicate)
java.util.stream.LongStreamtrue is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
noneMatch in interface LongStreampredicate - 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 OptionalLong findFirst()
java.util.stream.LongStreamOptionalLong describing the first element of this
stream, or an empty OptionalLong 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 LongStreamOptionalLong describing the first element of this
stream, or an empty OptionalLong if the stream is emptypublic OptionalLong findAny()
java.util.stream.LongStreamOptionalLong describing some element of the stream, or
an empty OptionalLong 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 LongStream.findFirst() instead.)
findAny in interface LongStreamOptionalLong describing some element of this stream,
or an empty OptionalLong if the stream is emptyLongStream.findFirst()public DoubleStream asDoubleStream()
java.util.stream.LongStreamDoubleStream consisting of the elements of this stream,
converted to double.
This is an intermediate operation.
asDoubleStream in interface LongStreamDoubleStream consisting of the elements of this stream,
converted to doublepublic Stream<Long> boxed()
java.util.stream.LongStreamStream consisting of the elements of this stream,
each boxed to a Long.
This is an intermediate operation.
boxed in interface LongStreamStream consistent of the elements of this stream,
each boxed to Longpublic PrimitiveIterator.OfLong iterator()
java.util.stream.BaseStreamThis is a terminal operation.
iterator in interface BaseStream<Long,LongStream>iterator in interface LongStreampublic Spliterator.OfLong spliterator()
java.util.stream.BaseStreamThis is a terminal operation.
spliterator in interface BaseStream<Long,LongStream>spliterator in interface LongStreampublic 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.