Class ParallelLongStreamSupport
- java.lang.Object
-
- com.github.ferstl.streams.ParallelLongStreamSupport
-
- All Implemented Interfaces:
AutoCloseable,BaseStream<Long,LongStream>,LongStream
public class ParallelLongStreamSupport extends Object implements LongStream
An implementation of
LongStreamwhich uses a customForkJoinPoolfor parallel aggregate operations. This is thelongprimitive specialization ofParallelStreamSupport.The following example illustrates an aggregate operation using
ParallelStreamSupportandParallelLongStreamSupportwith a customForkJoinPool, 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.
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
LongStream, which are meaningful for parallel streams Arrays.stream(long[])StreamSupport.longStream(Spliterator.OfLong, boolean)StreamSupport.longStream(Supplier, int, boolean)
- API Note:
Internally, this stream wraps a
longstream 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,ParallelIntStreamSupportorParallelDoubleStreamSupport.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.LongStream
LongStream.Builder
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanallMatch(LongPredicate predicate)booleananyMatch(LongPredicate predicate)DoubleStreamasDoubleStream()OptionalDoubleaverage()Stream<Long>boxed()voidclose()<R> Rcollect(Supplier<R> supplier, ObjLongConsumer<R> accumulator, BiConsumer<R,R> combiner)static LongStreamconcat(LongStream a, LongStream b, ForkJoinPool workerPool)Creates a lazily concatenated parallellongstream whose elements are all the elements of the first stream followed by all the elements of the second stream.longcount()LongStreamdistinct()LongStreamdropWhile(LongPredicate predicate)protected voidexecute(Runnable terminalOperation)protected <R> Rexecute(Callable<R> terminalOperation)LongStreamfilter(LongPredicate predicate)OptionalLongfindAny()OptionalLongfindFirst()LongStreamflatMap(LongFunction<? extends LongStream> mapper)voidforEach(LongConsumer action)voidforEachOrdered(LongConsumer action)static LongStreamgenerate(LongSupplier supplier, ForkJoinPool workerPool)Creates a parallel infinite sequential unorderedlongstream where each element is generated by the providedLongSupplier.booleanisParallel()static LongStreamiterate(long seed, LongUnaryOperator operator, ForkJoinPool workerPool)Creates a parallel infinite orderedlongstream produced by iterative application of a functionfto an initial elementseed.PrimitiveIterator.OfLongiterator()LongStreamlimit(long maxSize)LongStreammap(LongUnaryOperator mapper)DoubleStreammapToDouble(LongToDoubleFunction mapper)IntStreammapToInt(LongToIntFunction mapper)<U> Stream<U>mapToObj(LongFunction<? extends U> mapper)OptionalLongmax()OptionalLongmin()booleannoneMatch(LongPredicate predicate)SonClose(Runnable closeHandler)Sparallel()static LongStreamparallelStream(long[] array, ForkJoinPool workerPool)Creates a parallellongstream from the given Array.static LongStreamparallelStream(Supplier<? extends Spliterator.OfLong> supplier, int characteristics, ForkJoinPool workerPool)Creates a parallellongstream from the given Spliterator supplier.static LongStreamparallelStream(Spliterator.OfLong spliterator, ForkJoinPool workerPool)Creates a parallellongstream from the given Spliterator.static LongStreamparallelStream(LongStream.Builder builder, ForkJoinPool workerPool)Creates a parallellongstream from the givenLongStream.Builder.LongStreampeek(LongConsumer action)static LongStreamrange(long startInclusive, long endExclusive, ForkJoinPool workerPool)Creates a parallel orderedlongstream fromstartInclusive(inclusive) toendExclusive(exclusive) by an incremental step of1.static LongStreamrangeClosed(long startInclusive, long endInclusive, ForkJoinPool workerPool)Creates a parallel orderedlongstream fromstartInclusive(inclusive) toendInclusive(inclusive) by an incremental step of1.longreduce(long identity, LongBinaryOperator op)OptionalLongreduce(LongBinaryOperator op)Ssequential()LongStreamskip(long n)LongStreamsorted()Spliterator.OfLongspliterator()longsum()LongSummaryStatisticssummaryStatistics()LongStreamtakeWhile(LongPredicate predicate)long[]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.LongStream
parallel, sequential
-
-
-
-
Method Detail
-
parallelStream
public static LongStream parallelStream(long[] array, ForkJoinPool workerPool)
Creates a parallellongstream 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
longstream that executes a terminal operation in the givenForkJoinPool. - See Also:
Arrays.stream(long[])
-
parallelStream
public static LongStream parallelStream(Spliterator.OfLong spliterator, ForkJoinPool workerPool)
Creates a parallellongstream from the given Spliterator. This operation is similar to callingStreamSupport.longStream(spliterator, true)with the difference that a parallel terminal operation will be executed in the givenForkJoinPool.- Parameters:
spliterator- ASpliterator.OfLongdescribing the stream elements. Must not benull.workerPool- Thread pool for parallel execution of a terminal operation. Must not benull.- Returns:
- A parallel
longstream that executes a terminal operation in the givenForkJoinPool. - See Also:
StreamSupport.longStream(Spliterator.OfLong, boolean)
-
parallelStream
public static LongStream parallelStream(Supplier<? extends Spliterator.OfLong> supplier, int characteristics, ForkJoinPool workerPool)
Creates a parallellongstream from the given Spliterator supplier. This operation is similar to callingStreamSupport.longStream(supplier, characteristics, true)with the difference that a parallel terminal operation will be executed in the givenForkJoinPool.- Parameters:
supplier- ASupplierof aSpliterator.OfLong. 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
longstream that executes a terminal operation in the givenForkJoinPool. - See Also:
StreamSupport.longStream(Supplier, int, boolean)
-
parallelStream
public static LongStream parallelStream(LongStream.Builder builder, ForkJoinPool workerPool)
Creates a parallellongstream from the givenLongStream.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
longstream that executes a terminal operation in the givenForkJoinPool. builder()
-
iterate
public static LongStream iterate(long seed, LongUnaryOperator operator, ForkJoinPool workerPool)
Creates a parallel infinite orderedlongstream produced by iterative application of a functionfto an initial elementseed. This operation is similar to callingLongStream.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
longstream that executes a terminal operation in the givenForkJoinPool. - See Also:
LongStream.iterate(long, LongUnaryOperator)
-
generate
public static LongStream generate(LongSupplier supplier, ForkJoinPool workerPool)
Creates a parallel infinite sequential unorderedlongstream where each element is generated by the providedLongSupplier. This operation is similar to callingLongStream.generate(supplier).parallel()with the difference that a parallel terminal operation will be executed in the givenForkJoinPool.- Parameters:
supplier- TheLongSupplierof generated elements. Must not benull.workerPool- Thread pool for parallel execution of a terminal operation. Must not benull.- Returns:
- A parallel
longstream that executes a terminal operation in the givenForkJoinPool. - See Also:
LongStream.generate(LongSupplier)
-
range
public static LongStream range(long startInclusive, long endExclusive, ForkJoinPool workerPool)
Creates a parallel orderedlongstream fromstartInclusive(inclusive) toendExclusive(exclusive) by an incremental step of1. This operation is similar to callingLongStream.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
longstream that executes a terminal operation in the givenForkJoinPoolfor the range oflongelements. - See Also:
LongStream.range(long, long)
-
rangeClosed
public static LongStream rangeClosed(long startInclusive, long endInclusive, ForkJoinPool workerPool)
Creates a parallel orderedlongstream fromstartInclusive(inclusive) toendInclusive(inclusive) by an incremental step of1. This operation is similar to callingLongStream.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
longstream that executes a terminal operation in the givenForkJoinPoolfor the range oflongelements. - See Also:
LongStream.rangeClosed(long, long)
-
concat
public static LongStream concat(LongStream a, LongStream b, ForkJoinPool workerPool)
Creates a lazily concatenated parallellongstream whose elements are all the elements of the first stream followed by all the elements of the second stream. This operation is similar to callingLongStream.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:
LongStream.concat(LongStream, LongStream)
-
filter
public LongStream filter(LongPredicate predicate)
- Specified by:
filterin interfaceLongStream
-
map
public LongStream map(LongUnaryOperator mapper)
- Specified by:
mapin interfaceLongStream
-
mapToObj
public <U> Stream<U> mapToObj(LongFunction<? extends U> mapper)
- Specified by:
mapToObjin interfaceLongStream
-
mapToInt
public IntStream mapToInt(LongToIntFunction mapper)
- Specified by:
mapToIntin interfaceLongStream
-
mapToDouble
public DoubleStream mapToDouble(LongToDoubleFunction mapper)
- Specified by:
mapToDoublein interfaceLongStream
-
flatMap
public LongStream flatMap(LongFunction<? extends LongStream> mapper)
- Specified by:
flatMapin interfaceLongStream
-
distinct
public LongStream distinct()
- Specified by:
distinctin interfaceLongStream
-
sorted
public LongStream sorted()
- Specified by:
sortedin interfaceLongStream
-
peek
public LongStream peek(LongConsumer action)
- Specified by:
peekin interfaceLongStream
-
limit
public LongStream limit(long maxSize)
- Specified by:
limitin interfaceLongStream
-
skip
public LongStream skip(long n)
- Specified by:
skipin interfaceLongStream
-
takeWhile
public LongStream takeWhile(LongPredicate predicate)
- Specified by:
takeWhilein interfaceLongStream
-
dropWhile
public LongStream dropWhile(LongPredicate predicate)
- Specified by:
dropWhilein interfaceLongStream
-
forEach
public void forEach(LongConsumer action)
- Specified by:
forEachin interfaceLongStream
-
forEachOrdered
public void forEachOrdered(LongConsumer action)
- Specified by:
forEachOrderedin interfaceLongStream
-
toArray
public long[] toArray()
- Specified by:
toArrayin interfaceLongStream
-
reduce
public long reduce(long identity, LongBinaryOperator op)- Specified by:
reducein interfaceLongStream
-
reduce
public OptionalLong reduce(LongBinaryOperator op)
- Specified by:
reducein interfaceLongStream
-
collect
public <R> R collect(Supplier<R> supplier, ObjLongConsumer<R> accumulator, BiConsumer<R,R> combiner)
- Specified by:
collectin interfaceLongStream
-
sum
public long sum()
- Specified by:
sumin interfaceLongStream
-
min
public OptionalLong min()
- Specified by:
minin interfaceLongStream
-
max
public OptionalLong max()
- Specified by:
maxin interfaceLongStream
-
count
public long count()
- Specified by:
countin interfaceLongStream
-
average
public OptionalDouble average()
- Specified by:
averagein interfaceLongStream
-
summaryStatistics
public LongSummaryStatistics summaryStatistics()
- Specified by:
summaryStatisticsin interfaceLongStream
-
anyMatch
public boolean anyMatch(LongPredicate predicate)
- Specified by:
anyMatchin interfaceLongStream
-
allMatch
public boolean allMatch(LongPredicate predicate)
- Specified by:
allMatchin interfaceLongStream
-
noneMatch
public boolean noneMatch(LongPredicate predicate)
- Specified by:
noneMatchin interfaceLongStream
-
findFirst
public OptionalLong findFirst()
- Specified by:
findFirstin interfaceLongStream
-
findAny
public OptionalLong findAny()
- Specified by:
findAnyin interfaceLongStream
-
asDoubleStream
public DoubleStream asDoubleStream()
- Specified by:
asDoubleStreamin interfaceLongStream
-
boxed
public Stream<Long> boxed()
- Specified by:
boxedin interfaceLongStream
-
iterator
public PrimitiveIterator.OfLong iterator()
- Specified by:
iteratorin interfaceBaseStream<Long,LongStream>- Specified by:
iteratorin interfaceLongStream
-
spliterator
public Spliterator.OfLong spliterator()
- Specified by:
spliteratorin interfaceBaseStream<Long,LongStream>- Specified by:
spliteratorin interfaceLongStream
-
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)
-
-