Class ParallelIntStreamSupport
- java.lang.Object
-
- com.github.ferstl.streams.ParallelIntStreamSupport
-
- All Implemented Interfaces:
AutoCloseable,BaseStream<Integer,IntStream>,IntStream
public class ParallelIntStreamSupport extends Object implements IntStream
An implementation of
IntStreamwhich uses a customForkJoinPoolfor parallel aggregate operations. This is theintprimitive specialization ofParallelStreamSupport.The following example illustrates an aggregate operation using
ParallelStreamSupportandParallelIntStreamSupportwith 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 asForkJoinTaskin 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)
- API Note:
Internally, this stream wraps an
intstream 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 ofParallelStreamSupport,ParallelLongStreamSupportorParallelDoubleStreamSupport.Although each factory method returns a parallel stream, calling
BaseStream.sequential()is still possible and leads to sequential execution of a terminal operation within the calling thread.- Implementation Note:
See the class documentation for
Streamand the package documentation for java.util.stream for additional specification.
-
-
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 booleanallMatch(IntPredicate predicate)booleananyMatch(IntPredicate predicate)DoubleStreamasDoubleStream()LongStreamasLongStream()OptionalDoubleaverage()Stream<Integer>boxed()voidclose()<R> Rcollect(Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R,R> combiner)static IntStreamconcat(IntStream a, IntStream b, ForkJoinPool workerPool)Creates a lazily concatenated parallelintstream whose elements are all the elements of the first stream followed by all the elements of the second stream.longcount()IntStreamdistinct()IntStreamdropWhile(IntPredicate predicate)protected voidexecute(Runnable terminalOperation)protected <R> Rexecute(Callable<R> terminalOperation)IntStreamfilter(IntPredicate predicate)OptionalIntfindAny()OptionalIntfindFirst()IntStreamflatMap(IntFunction<? extends IntStream> mapper)voidforEach(IntConsumer action)voidforEachOrdered(IntConsumer action)static IntStreamgenerate(IntSupplier supplier, ForkJoinPool workerPool)Creates a parallel infinite sequential unorderedintstream where each element is generated by the providedIntSupplier.booleanisParallel()static IntStreamiterate(int seed, IntUnaryOperator operator, ForkJoinPool workerPool)Creates a parallel infinite orderedintstream produced by iterative application of a functionfto an initial elementseed.PrimitiveIterator.OfIntiterator()IntStreamlimit(long maxSize)IntStreammap(IntUnaryOperator mapper)DoubleStreammapToDouble(IntToDoubleFunction mapper)LongStreammapToLong(IntToLongFunction mapper)<U> Stream<U>mapToObj(IntFunction<? extends U> mapper)OptionalIntmax()OptionalIntmin()booleannoneMatch(IntPredicate predicate)SonClose(Runnable closeHandler)Sparallel()static IntStreamparallelStream(int[] array, ForkJoinPool workerPool)Creates a parallelintstream from the given Array.static IntStreamparallelStream(Supplier<? extends Spliterator.OfInt> supplier, int characteristics, ForkJoinPool workerPool)Creates a parallelintstream from the given Spliterator supplier.static IntStreamparallelStream(Spliterator.OfInt spliterator, ForkJoinPool workerPool)Creates a parallelintstream from the given Spliterator.static IntStreamparallelStream(IntStream.Builder builder, ForkJoinPool workerPool)Creates a parallelintstream from the givenIntStream.Builder.IntStreampeek(IntConsumer action)static IntStreamrange(int startInclusive, int endExclusive, ForkJoinPool workerPool)Creates a parallel orderedintstream fromstartInclusive(inclusive) toendExclusive(exclusive) by an incremental step of1.static IntStreamrangeClosed(int startInclusive, int endInclusive, ForkJoinPool workerPool)Creates a parallel orderedintstream fromstartInclusive(inclusive) toendInclusive(inclusive) by an incremental step of1.intreduce(int identity, IntBinaryOperator op)OptionalIntreduce(IntBinaryOperator op)Ssequential()IntStreamskip(long n)IntStreamsorted()Spliterator.OfIntspliterator()intsum()IntSummaryStatisticssummaryStatistics()IntStreamtakeWhile(IntPredicate predicate)int[]toArray()Sunordered()-
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
parallel, sequential
-
-
-
-
Method Detail
-
parallelStream
public static IntStream parallelStream(int[] array, ForkJoinPool workerPool)
Creates a parallelintstream 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
intstream that executes a terminal operation in the givenForkJoinPool. - See Also:
Arrays.stream(int[])
-
parallelStream
public static IntStream parallelStream(Spliterator.OfInt spliterator, ForkJoinPool workerPool)
Creates a parallelintstream 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.OfIntdescribing the stream elements. Must not benull.workerPool- Thread pool for parallel execution of a terminal operation. Must not benull.- Returns:
- A parallel
intstream that executes a terminal operation in the givenForkJoinPool. - See Also:
StreamSupport.intStream(Spliterator.OfInt, boolean)
-
parallelStream
public static IntStream parallelStream(Supplier<? extends Spliterator.OfInt> supplier, int characteristics, ForkJoinPool workerPool)
Creates a parallelintstream 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- ASupplierof 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
intstream that executes a terminal operation in the givenForkJoinPool. - See Also:
StreamSupport.intStream(Supplier, int, boolean)
-
parallelStream
public static IntStream parallelStream(IntStream.Builder builder, ForkJoinPool workerPool)
Creates a parallelintstream 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
intstream 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 orderedintstream produced by iterative application of a functionfto 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
intstream 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 unorderedintstream 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- TheIntSupplierof generated elements. Must not benull.workerPool- Thread pool for parallel execution of a terminal operation. Must not benull.- Returns:
- A parallel
intstream 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 orderedintstream 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
intstream that executes a terminal operation in the givenForkJoinPoolfor the range ofintelements. - See Also:
IntStream.range(int, int)
-
rangeClosed
public static IntStream rangeClosed(int startInclusive, int endInclusive, ForkJoinPool workerPool)
Creates a parallel orderedintstream 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
intstream that executes a terminal operation in the givenForkJoinPoolfor the range ofintelements. - See Also:
IntStream.rangeClosed(int, int)
-
concat
public static IntStream concat(IntStream a, IntStream b, ForkJoinPool workerPool)
Creates a lazily concatenated parallelintstream 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:
mapToDoublein interfaceIntStream
-
flatMap
public IntStream flatMap(IntFunction<? extends IntStream> mapper)
-
peek
public IntStream peek(IntConsumer action)
-
takeWhile
public IntStream takeWhile(IntPredicate predicate)
-
dropWhile
public IntStream dropWhile(IntPredicate predicate)
-
forEach
public void forEach(IntConsumer action)
-
forEachOrdered
public void forEachOrdered(IntConsumer action)
- Specified by:
forEachOrderedin 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:
summaryStatisticsin 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:
asLongStreamin interfaceIntStream
-
asDoubleStream
public DoubleStream asDoubleStream()
- Specified by:
asDoubleStreamin interfaceIntStream
-
iterator
public PrimitiveIterator.OfInt iterator()
-
spliterator
public Spliterator.OfInt spliterator()
- Specified by:
spliteratorin interfaceBaseStream<Integer,IntStream>- Specified by:
spliteratorin interfaceIntStream
-
isParallel
public boolean isParallel()
- Specified by:
isParallelin interfaceBaseStream<T,S extends BaseStream<T,S>>
-
sequential
public S sequential()
- Specified by:
sequentialin interfaceBaseStream<T,S extends BaseStream<T,S>>
-
parallel
public S parallel()
- Specified by:
parallelin interfaceBaseStream<T,S extends BaseStream<T,S>>
-
unordered
public S unordered()
- Specified by:
unorderedin interfaceBaseStream<T,S extends BaseStream<T,S>>
-
onClose
public S onClose(Runnable closeHandler)
- Specified by:
onClosein interfaceBaseStream<T,S extends BaseStream<T,S>>
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceBaseStream<T,S extends BaseStream<T,S>>
-
execute
protected void execute(Runnable terminalOperation)
-
execute
protected <R> R execute(Callable<R> terminalOperation)
-
-