Package com.infilos.utils.stream
Class ParallelIntStream
- java.lang.Object
-
- com.infilos.utils.stream.ParallelIntStream
-
- All Implemented Interfaces:
AutoCloseable
,BaseStream<Integer,IntStream>
,IntStream
public class ParallelIntStream extends Object implements IntStream
An implementation of
IntStream
which uses a customForkJoinPool
for parallel aggregate operations. This is theint
primitive specialization ofParallelStream
.The following example illustrates an aggregate operation using
ParallelStream
andParallelIntStream
with a customForkJoinPool
, 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.
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
IntStream
, which are meaningful for parallel streams Arrays.stream(int[])
StreamSupport.intStream(Spliterator.OfInt, boolean)
StreamSupport.intStream(Supplier, int, boolean)
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface java.util.stream.IntStream
IntStream.Builder
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
allMatch(IntPredicate predicate)
boolean
anyMatch(IntPredicate predicate)
DoubleStream
asDoubleStream()
LongStream
asLongStream()
OptionalDouble
average()
Stream<Integer>
boxed()
void
close()
<R> R
collect(Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R,R> combiner)
static IntStream
concat(IntStream a, IntStream b, ForkJoinPool workerPool)
Creates a lazily concatenated parallelint
stream whose elements are all the elements of the first stream followed by all the elements of the second stream.long
count()
IntStream
distinct()
protected void
execute(Runnable terminalOperation)
protected <R> R
execute(Callable<R> terminalOperation)
IntStream
filter(IntPredicate predicate)
OptionalInt
findAny()
OptionalInt
findFirst()
IntStream
flatMap(IntFunction<? extends IntStream> mapper)
void
forEach(IntConsumer action)
void
forEachOrdered(IntConsumer action)
static IntStream
generate(IntSupplier supplier, ForkJoinPool workerPool)
Creates a parallel infinite sequential unorderedint
stream where each element is generated by the providedIntSupplier
.boolean
isParallel()
static IntStream
iterate(int seed, IntUnaryOperator operator, ForkJoinPool workerPool)
Creates a parallel infinite orderedint
stream produced by iterative application of a functionf
to an initial elementseed
.PrimitiveIterator.OfInt
iterator()
IntStream
limit(long maxSize)
IntStream
map(IntUnaryOperator mapper)
DoubleStream
mapToDouble(IntToDoubleFunction mapper)
LongStream
mapToLong(IntToLongFunction mapper)
<U> Stream<U>
mapToObj(IntFunction<? extends U> mapper)
OptionalInt
max()
OptionalInt
min()
boolean
noneMatch(IntPredicate predicate)
S
onClose(Runnable closeHandler)
S
parallel()
IntStream
peek(IntConsumer action)
static IntStream
range(int startInclusive, int endExclusive, ForkJoinPool workerPool)
Creates a parallel orderedint
stream fromstartInclusive
(inclusive) toendExclusive
(exclusive) by an incremental step of1
.static IntStream
rangeClosed(int startInclusive, int endInclusive, ForkJoinPool workerPool)
Creates a parallel orderedint
stream fromstartInclusive
(inclusive) toendInclusive
(inclusive) by an incremental step of1
.int
reduce(int identity, IntBinaryOperator op)
OptionalInt
reduce(IntBinaryOperator op)
S
sequential()
IntStream
skip(long n)
IntStream
sorted()
Spliterator.OfInt
spliterator()
static IntStream
submit(int[] array, ForkJoinPool workerPool)
Creates a parallelint
stream from the given Array.static IntStream
submit(Supplier<? extends Spliterator.OfInt> supplier, int characteristics, ForkJoinPool workerPool)
Creates a parallelint
stream from the given Spliterator supplier.static IntStream
submit(Spliterator.OfInt spliterator, ForkJoinPool workerPool)
Creates a parallelint
stream from the given Spliterator.static IntStream
submit(IntStream.Builder builder, ForkJoinPool workerPool)
Creates a parallelint
stream from the givenIntStream.Builder
.int
sum()
IntSummaryStatistics
summaryStatistics()
int[]
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.IntStream
dropWhile, parallel, sequential, takeWhile
-
-
-
-
Method Detail
-
submit
public static IntStream submit(int[] array, ForkJoinPool workerPool)
Creates a parallelint
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
int
stream that executes a terminal operation in the givenForkJoinPool
. - See Also:
Arrays.stream(int[])
-
submit
public static IntStream submit(Spliterator.OfInt spliterator, ForkJoinPool workerPool)
Creates a parallelint
stream from the given Spliterator. This operation is similar to callingStreamSupport.intStream(spliterator, true)
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Parameters:
spliterator
- ASpliterator.OfInt
describing the stream elements. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel
int
stream that executes a terminal operation in the givenForkJoinPool
. - See Also:
StreamSupport.intStream(Spliterator.OfInt, boolean)
-
submit
public static IntStream submit(Supplier<? extends Spliterator.OfInt> supplier, int characteristics, ForkJoinPool workerPool)
Creates a parallelint
stream from the given Spliterator supplier. This operation is similar to callingStreamSupport.intStream(supplier, characteristics, true)
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Parameters:
supplier
- ASupplier
of aSpliterator.OfInt
. 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
int
stream that executes a terminal operation in the givenForkJoinPool
. - See Also:
StreamSupport.intStream(Supplier, int, boolean)
-
submit
public static IntStream submit(IntStream.Builder builder, ForkJoinPool workerPool)
Creates a parallelint
stream from the givenIntStream.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
int
stream that executes a terminal operation in the givenForkJoinPool
. - See Also:
IntStream.builder()
-
iterate
public static IntStream iterate(int seed, IntUnaryOperator operator, ForkJoinPool workerPool)
Creates a parallel infinite orderedint
stream produced by iterative application of a functionf
to an initial elementseed
. This operation is similar to callingIntStream.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
int
stream that executes a terminal operation in the givenForkJoinPool
. - See Also:
IntStream.iterate(int, IntUnaryOperator)
-
generate
public static IntStream generate(IntSupplier supplier, ForkJoinPool workerPool)
Creates a parallel infinite sequential unorderedint
stream where each element is generated by the providedIntSupplier
. This operation is similar to callingIntStream.generate(supplier).parallel()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Parameters:
supplier
- TheIntSupplier
of generated elements. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel
int
stream that executes a terminal operation in the givenForkJoinPool
. - See Also:
IntStream.generate(IntSupplier)
-
range
public static IntStream range(int startInclusive, int endExclusive, ForkJoinPool workerPool)
Creates a parallel orderedint
stream fromstartInclusive
(inclusive) toendExclusive
(exclusive) by an incremental step of1
. This operation is similar to callingIntStream.range(startInclusive, endExclusive).parallel()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Parameters:
startInclusive
- the (inclusive) initial valueendExclusive
- the exclusive upper boundworkerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel
int
stream that executes a terminal operation in the givenForkJoinPool
for the range ofint
elements. - See Also:
IntStream.range(int, int)
-
rangeClosed
public static IntStream rangeClosed(int startInclusive, int endInclusive, ForkJoinPool workerPool)
Creates a parallel orderedint
stream fromstartInclusive
(inclusive) toendInclusive
(inclusive) by an incremental step of1
. This operation is similar to callingIntStream.rangeClosed(startInclusive, endInclusive).parallel()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Parameters:
startInclusive
- the (inclusive) initial valueendInclusive
- the inclusive upper boundworkerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel
int
stream that executes a terminal operation in the givenForkJoinPool
for the range ofint
elements. - See Also:
IntStream.rangeClosed(int, int)
-
concat
public static IntStream concat(IntStream a, IntStream b, ForkJoinPool workerPool)
Creates a lazily concatenated parallelint
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 callingIntStream.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:
IntStream.concat(IntStream, IntStream)
-
filter
public IntStream filter(IntPredicate predicate)
-
map
public IntStream map(IntUnaryOperator mapper)
-
mapToObj
public <U> Stream<U> mapToObj(IntFunction<? extends U> mapper)
-
mapToLong
public LongStream mapToLong(IntToLongFunction mapper)
-
mapToDouble
public DoubleStream mapToDouble(IntToDoubleFunction mapper)
- Specified by:
mapToDouble
in interfaceIntStream
-
flatMap
public IntStream flatMap(IntFunction<? extends IntStream> mapper)
-
peek
public IntStream peek(IntConsumer action)
-
forEach
public void forEach(IntConsumer action)
-
forEachOrdered
public void forEachOrdered(IntConsumer action)
- Specified by:
forEachOrdered
in interfaceIntStream
-
reduce
public int reduce(int identity, IntBinaryOperator op)
-
reduce
public OptionalInt reduce(IntBinaryOperator op)
-
collect
public <R> R collect(Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R,R> combiner)
-
min
public OptionalInt min()
-
max
public OptionalInt max()
-
average
public OptionalDouble average()
-
summaryStatistics
public IntSummaryStatistics summaryStatistics()
- Specified by:
summaryStatistics
in interfaceIntStream
-
anyMatch
public boolean anyMatch(IntPredicate predicate)
-
allMatch
public boolean allMatch(IntPredicate predicate)
-
noneMatch
public boolean noneMatch(IntPredicate predicate)
-
findFirst
public OptionalInt findFirst()
-
findAny
public OptionalInt findAny()
-
asLongStream
public LongStream asLongStream()
- Specified by:
asLongStream
in interfaceIntStream
-
asDoubleStream
public DoubleStream asDoubleStream()
- Specified by:
asDoubleStream
in interfaceIntStream
-
iterator
public PrimitiveIterator.OfInt iterator()
-
spliterator
public Spliterator.OfInt spliterator()
- Specified by:
spliterator
in interfaceBaseStream<Integer,IntStream>
- Specified by:
spliterator
in interfaceIntStream
-
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)
-
-