public class ParallelIntStreamSupport extends Object implements IntStream
An implementation of IntStream
which uses a custom ForkJoinPool
for parallel aggregate operations.
This is the int
primitive specialization of ParallelStreamSupport
.
The following example illustrates an aggregate operation using ParallelStreamSupport
and
ParallelIntStreamSupport
with a custom ForkJoinPool
, computing the sum of the weights of the red
widgets:
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. isParallel()
returns true
, a
terminal
operation will be executed as ForkJoinTask
in the custom ForkJoinPool
. Otherwise it will be
executed in the calling thread.
IntStream
, which are meaningful for parallel streamsArrays.stream(int[])
StreamSupport.intStream(Spliterator.OfInt, boolean)
StreamSupport.intStream(Supplier, int, boolean)
Internally, this stream wraps an int
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
, ParallelLongStreamSupport
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.
IntStream.Builder
Modifier and Type | Method and Description |
---|---|
boolean |
allMatch(IntPredicate predicate)
Returns whether all elements of this stream match the provided predicate.
|
boolean |
anyMatch(IntPredicate 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 . |
LongStream |
asLongStream()
Returns a
LongStream consisting of the elements of this stream,
converted to long . |
OptionalDouble |
average()
Returns an
OptionalDouble describing the arithmetic mean of elements of
this stream, or an empty optional if this stream is empty. |
Stream<Integer> |
boxed()
Returns a
Stream consisting of the elements of this stream,
each boxed to an Integer . |
void |
close()
Closes this stream, causing all close handlers for this stream pipeline
to be called.
|
<R> R |
collect(Supplier<R> supplier,
ObjIntConsumer<R> accumulator,
BiConsumer<R,R> combiner)
Performs a mutable
reduction operation on the elements of this stream.
|
static IntStream |
concat(IntStream a,
IntStream b,
ForkJoinPool workerPool)
Creates a lazily concatenated parallel
int 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.
|
IntStream |
distinct()
Returns a stream consisting of the distinct elements of this stream.
|
protected <R> R |
execute(Callable<R> terminalOperation) |
protected void |
execute(Runnable terminalOperation) |
IntStream |
filter(IntPredicate predicate)
Returns a stream consisting of the elements of this stream that match
the given predicate.
|
OptionalInt |
findAny()
Returns an
OptionalInt describing some element of the stream, or
an empty OptionalInt if the stream is empty. |
OptionalInt |
findFirst()
Returns an
OptionalInt describing the first element of this
stream, or an empty OptionalInt if the stream is empty. |
IntStream |
flatMap(IntFunction<? extends IntStream> 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(IntConsumer action)
Performs an action for each element of this stream.
|
void |
forEachOrdered(IntConsumer 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 IntStream |
generate(IntSupplier supplier,
ForkJoinPool workerPool)
Creates a parallel infinite sequential unordered
int stream where each element is
generated by the provided IntSupplier . |
boolean |
isParallel()
Returns whether this stream, if a terminal operation were to be executed,
would execute in parallel.
|
static IntStream |
iterate(int seed,
IntUnaryOperator operator,
ForkJoinPool workerPool)
Creates a parallel infinite ordered
int stream produced by iterative application of a
function f to an initial element seed . |
PrimitiveIterator.OfInt |
iterator()
Returns an iterator for the elements of this stream.
|
IntStream |
limit(long maxSize)
Returns a stream consisting of the elements of this stream, truncated
to be no longer than
maxSize in length. |
IntStream |
map(IntUnaryOperator mapper)
Returns a stream consisting of the results of applying the given
function to the elements of this stream.
|
DoubleStream |
mapToDouble(IntToDoubleFunction mapper)
Returns a
DoubleStream consisting of the results of applying the
given function to the elements of this stream. |
LongStream |
mapToLong(IntToLongFunction mapper)
Returns a
LongStream consisting of the results of applying the
given function to the elements of this stream. |
<U> Stream<U> |
mapToObj(IntFunction<? extends U> mapper)
Returns an object-valued
Stream consisting of the results of
applying the given function to the elements of this stream. |
OptionalInt |
max()
Returns an
OptionalInt describing the maximum element of this
stream, or an empty optional if this stream is empty. |
OptionalInt |
min()
Returns an
OptionalInt describing the minimum element of this
stream, or an empty optional if this stream is empty. |
boolean |
noneMatch(IntPredicate 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 IntStream |
parallelStream(int[] array,
ForkJoinPool workerPool)
Creates a parallel
int stream from the given Array. |
static IntStream |
parallelStream(IntStream.Builder builder,
ForkJoinPool workerPool)
Creates a parallel
int stream from the given Builder . |
static IntStream |
parallelStream(Spliterator.OfInt spliterator,
ForkJoinPool workerPool)
Creates a parallel
int stream from the given Spliterator. |
static IntStream |
parallelStream(Supplier<? extends Spliterator.OfInt> supplier,
int characteristics,
ForkJoinPool workerPool)
Creates a parallel
int stream from the given Spliterator supplier. |
IntStream |
peek(IntConsumer 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 IntStream |
range(int startInclusive,
int endExclusive,
ForkJoinPool workerPool)
Creates a parallel ordered
int stream from startInclusive (inclusive) to
endExclusive (exclusive) by an incremental step of 1 . |
static IntStream |
rangeClosed(int startInclusive,
int endInclusive,
ForkJoinPool workerPool)
Creates a parallel ordered
int stream from startInclusive (inclusive) to
endInclusive (inclusive) by an incremental step of 1 . |
OptionalInt |
reduce(IntBinaryOperator op)
Performs a reduction on the
elements of this stream, using an
associative accumulation
function, and returns an
OptionalInt describing the reduced value,
if any. |
int |
reduce(int identity,
IntBinaryOperator 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.
|
IntStream |
skip(long n)
Returns a stream consisting of the remaining elements of this stream
after discarding the first
n elements of the stream. |
IntStream |
sorted()
Returns a stream consisting of the elements of this stream in sorted
order.
|
Spliterator.OfInt |
spliterator()
Returns a spliterator for the elements of this stream.
|
int |
sum()
Returns the sum of elements in this stream.
|
IntSummaryStatistics |
summaryStatistics()
Returns an
IntSummaryStatistics describing various
summary data about the elements of this stream. |
int[] |
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, wait
builder, concat, empty, generate, iterate, of, of, parallel, range, rangeClosed, sequential
close, isParallel, onClose, unordered
public static IntStream parallelStream(int[] array, ForkJoinPool workerPool)
int
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
.int
stream that executes a terminal operation in the given ForkJoinPool
.Arrays.stream(int[])
public static IntStream parallelStream(Spliterator.OfInt spliterator, ForkJoinPool workerPool)
int
stream from the given Spliterator. This operation is similar to
calling StreamSupport.intStream(spliterator, true)
with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool
.spliterator
- A Spliterator.OfInt
describing the stream elements. Must not be null
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not be null
.int
stream that executes a terminal operation in the given ForkJoinPool
.StreamSupport.intStream(Spliterator.OfInt, boolean)
public static IntStream parallelStream(Supplier<? extends Spliterator.OfInt> supplier, int characteristics, ForkJoinPool workerPool)
int
stream from the given Spliterator supplier. This operation is
similar to calling StreamSupport.intStream(supplier, characteristics, true)
with the difference that a
parallel
terminal
operation will be executed in the given ForkJoinPool
.supplier
- A Supplier
of a Spliterator.OfInt
. 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
.int
stream that executes a terminal operation in the given ForkJoinPool
.StreamSupport.intStream(Supplier, int, boolean)
public static IntStream parallelStream(IntStream.Builder builder, ForkJoinPool workerPool)
int
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
.int
stream that executes a terminal operation in the given ForkJoinPool
.IntStream.builder()
public static IntStream iterate(int seed, IntUnaryOperator operator, ForkJoinPool workerPool)
int
stream produced by iterative application of a
function f
to an initial element seed
. This operation is similar to calling
IntStream.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
.int
stream that executes a terminal operation in the given ForkJoinPool
.IntStream.iterate(int, IntUnaryOperator)
public static IntStream generate(IntSupplier supplier, ForkJoinPool workerPool)
int
stream where each element is
generated by the provided IntSupplier
. This operation is similar to calling
IntStream.generate(supplier).parallel()
with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool
.supplier
- The IntSupplier
of generated elements. Must not be null
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not be null
.int
stream that executes a terminal operation in the given ForkJoinPool
.IntStream.generate(IntSupplier)
public static IntStream range(int startInclusive, int endExclusive, ForkJoinPool workerPool)
int
stream from startInclusive
(inclusive) to
endExclusive
(exclusive) by an incremental step of 1
. This operation is similar to calling
IntStream.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
.int
stream that executes a terminal operation in the given ForkJoinPool
for the
range of int
elements.IntStream.range(int, int)
public static IntStream rangeClosed(int startInclusive, int endInclusive, ForkJoinPool workerPool)
int
stream from startInclusive
(inclusive) to
endInclusive
(inclusive) by an incremental step of 1
. This operation is similar to calling
IntStream.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
.int
stream that executes a terminal operation in the given ForkJoinPool
for the
range of int
elements.IntStream.rangeClosed(int, int)
public static IntStream concat(IntStream a, IntStream b, ForkJoinPool workerPool)
int
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
IntStream.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
.IntStream.concat(IntStream, IntStream)
public IntStream filter(IntPredicate predicate)
java.util.stream.IntStream
This is an intermediate operation.
filter
in interface IntStream
predicate
- a non-interfering,
stateless
predicate to apply to each element to determine if it
should be includedpublic IntStream map(IntUnaryOperator mapper)
java.util.stream.IntStream
This is an intermediate operation.
map
in interface IntStream
mapper
- a non-interfering,
stateless
function to apply to each elementpublic <U> Stream<U> mapToObj(IntFunction<? extends U> mapper)
java.util.stream.IntStream
Stream
consisting of the results of
applying the given function to the elements of this stream.
This is an intermediate operation.
mapToObj
in interface IntStream
U
- the element type of the new streammapper
- a non-interfering,
stateless
function to apply to each elementpublic LongStream mapToLong(IntToLongFunction mapper)
java.util.stream.IntStream
LongStream
consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToLong
in interface IntStream
mapper
- a non-interfering,
stateless
function to apply to each elementpublic DoubleStream mapToDouble(IntToDoubleFunction mapper)
java.util.stream.IntStream
DoubleStream
consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToDouble
in interface IntStream
mapper
- a non-interfering,
stateless
function to apply to each elementpublic IntStream flatMap(IntFunction<? extends IntStream> mapper)
java.util.stream.IntStream
closed
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 IntStream
mapper
- a non-interfering,
stateless
function to apply to each element which produces an
IntStream
of new valuesStream.flatMap(Function)
public IntStream distinct()
java.util.stream.IntStream
This is a stateful intermediate operation.
public IntStream sorted()
java.util.stream.IntStream
This is a stateful intermediate operation.
public IntStream peek(IntConsumer action)
java.util.stream.IntStream
This 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 IntStream
action
- a
non-interfering action to perform on the elements as
they are consumed from the streampublic IntStream limit(long maxSize)
java.util.stream.IntStream
maxSize
in length.
public IntStream skip(long n)
java.util.stream.IntStream
n
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.
public void forEach(IntConsumer action)
java.util.stream.IntStream
This 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 IntStream
action
- a
non-interfering action to perform on the elementspublic void forEachOrdered(IntConsumer action)
java.util.stream.IntStream
This is a terminal operation.
forEachOrdered
in interface IntStream
action
- a
non-interfering action to perform on the elementsIntStream.forEach(IntConsumer)
public int[] toArray()
java.util.stream.IntStream
This is a terminal operation.
public int reduce(int identity, IntBinaryOperator op)
java.util.stream.IntStream
int result = identity;
for (int element : this stream)
result = accumulator.applyAsInt(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 IntStream
identity
- the identity value for the accumulating functionop
- an associative,
non-interfering,
stateless
function for combining two valuesIntStream.sum()
,
IntStream.min()
,
IntStream.max()
,
IntStream.average()
public OptionalInt reduce(IntBinaryOperator op)
java.util.stream.IntStream
OptionalInt
describing the reduced value,
if any. This is equivalent to:
boolean foundAny = false;
int result = null;
for (int element : this stream) {
if (!foundAny) {
foundAny = true;
result = element;
}
else
result = accumulator.applyAsInt(result, element);
}
return foundAny ? OptionalInt.of(result) : OptionalInt.empty();
but is not constrained to execute sequentially.
The accumulator
function must be an
associative function.
This is a terminal operation.
reduce
in interface IntStream
op
- an associative,
non-interfering,
stateless
function for combining two valuesIntStream.reduce(int, IntBinaryOperator)
public <R> R collect(Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R,R> combiner)
java.util.stream.IntStream
ArrayList
, 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 (int element : this stream)
accumulator.accept(result, element);
return result;
Like IntStream.reduce(int, IntBinaryOperator)
, collect
operations
can be parallelized without requiring additional synchronization.
This is a terminal operation.
collect
in interface IntStream
R
- 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 int sum()
java.util.stream.IntStream
return reduce(0, Integer::sum);
This is a terminal operation.
public OptionalInt min()
java.util.stream.IntStream
OptionalInt
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(Integer::min);
This is a terminal operation.
public OptionalInt max()
java.util.stream.IntStream
OptionalInt
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(Integer::max);
This is a terminal operation.
public long count()
java.util.stream.IntStream
return mapToLong(e -> 1L).sum();
This is a terminal operation.
public OptionalDouble average()
java.util.stream.IntStream
OptionalDouble
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.
public IntSummaryStatistics summaryStatistics()
java.util.stream.IntStream
IntSummaryStatistics
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 IntStream
IntSummaryStatistics
describing various summary data
about the elements of this streampublic boolean anyMatch(IntPredicate predicate)
java.util.stream.IntStream
false
is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
anyMatch
in interface IntStream
predicate
- a non-interfering,
stateless
predicate to apply to elements of this streamtrue
if any elements of the stream match the provided
predicate, otherwise false
public boolean allMatch(IntPredicate predicate)
java.util.stream.IntStream
true
is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
allMatch
in interface IntStream
predicate
- 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 false
public boolean noneMatch(IntPredicate predicate)
java.util.stream.IntStream
true
is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
noneMatch
in interface IntStream
predicate
- 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 false
public OptionalInt findFirst()
java.util.stream.IntStream
OptionalInt
describing the first element of this
stream, or an empty OptionalInt
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.
public OptionalInt findAny()
java.util.stream.IntStream
OptionalInt
describing some element of the stream, or
an empty OptionalInt
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 IntStream.findFirst()
instead.)
findAny
in interface IntStream
OptionalInt
describing some element of this stream, or
an empty OptionalInt
if the stream is emptyIntStream.findFirst()
public LongStream asLongStream()
java.util.stream.IntStream
LongStream
consisting of the elements of this stream,
converted to long
.
This is an intermediate operation.
asLongStream
in interface IntStream
LongStream
consisting of the elements of this stream,
converted to long
public DoubleStream asDoubleStream()
java.util.stream.IntStream
DoubleStream
consisting of the elements of this stream,
converted to double
.
This is an intermediate operation.
asDoubleStream
in interface IntStream
DoubleStream
consisting of the elements of this stream,
converted to double
public Stream<Integer> boxed()
java.util.stream.IntStream
Stream
consisting of the elements of this stream,
each boxed to an Integer
.
This is an intermediate operation.
public PrimitiveIterator.OfInt iterator()
java.util.stream.BaseStream
This is a terminal operation.
public Spliterator.OfInt spliterator()
java.util.stream.BaseStream
This is a terminal operation.
spliterator
in interface BaseStream<Integer,IntStream>
spliterator
in interface IntStream
public boolean isParallel()
java.util.stream.BaseStream
isParallel
in interface BaseStream<T,S extends BaseStream<T,S>>
true
if this stream would execute in parallel if executedpublic S sequential()
java.util.stream.BaseStream
This is an intermediate operation.
sequential
in interface BaseStream<T,S extends BaseStream<T,S>>
public S parallel()
java.util.stream.BaseStream
This is an intermediate operation.
parallel
in interface BaseStream<T,S extends BaseStream<T,S>>
public S unordered()
java.util.stream.BaseStream
This is an intermediate operation.
unordered
in interface BaseStream<T,S extends BaseStream<T,S>>
public S onClose(Runnable closeHandler)
java.util.stream.BaseStream
BaseStream.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.BaseStream
close
in interface AutoCloseable
close
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.