Package com.landawn.abacus.util.stream
Class LongStream
java.lang.Object
com.landawn.abacus.util.stream.LongStream
- All Implemented Interfaces:
Immutable
,BaseStream<Long,
,long[], LongPredicate, LongConsumer, LongList, u.OptionalLong, IndexedLong, LongIterator, LongStream> Closeable
,AutoCloseable
- Direct Known Subclasses:
LongStream.LongStreamEx
The Stream will be automatically closed after execution(A terminal method is executed/triggered).
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.landawn.abacus.util.stream.BaseStream
BaseStream.ParallelSettings, BaseStream.Splitor
-
Method Summary
Modifier and TypeMethodDescription<SS extends BaseStream>
SS__
(Function<? super LongStream, ? extends SS> transfer) Deprecated.abstract <E extends Exception>
booleanallMatch
(Throwables.LongPredicate<E> predicate) abstract <E extends Exception>
booleananyMatch
(Throwables.LongPredicate<E> predicate) abstract LongStream
append
(long... a) abstract LongStream
appendIfEmpty
(long... a) abstract DoubleStream
abstract FloatStream
Deprecated.abstract u.OptionalDouble
average()
boxed()
void
close()
It will be called by terminal operations in final.collapse
(LongBiPredicate collapsible) Merge series of adjacent elements which satisfy the given predicate using the merger function and return a new stream.abstract LongStream
collapse
(LongBiPredicate collapsible, LongBinaryOperator mergeFunction) Merge series of adjacent elements which satisfy the given predicate using the merger function and return a new stream.abstract LongStream
collapse
(LongTriPredicate collapsible, LongBinaryOperator mergeFunction) abstract <R> R
collect
(Supplier<R> supplier, ObjLongConsumer<? super R> accumulator) Only call this method when the returned typeR
is one types:Collection/Map/StringBuilder/Multiset/LongMultiset/Multimap/BooleanList/IntList/.../DoubleList
.abstract <R> R
collect
(Supplier<R> supplier, ObjLongConsumer<? super R> accumulator, BiConsumer<R, R> combiner) static LongStream
concat
(long[]... a) static LongStream
concat
(LongIterator... a) static LongStream
concat
(LongStream... a) static LongStream
concat
(Collection<? extends LongStream> c) static LongStream
static LongStream
concatIterators
(Collection<? extends LongIterator> c) static LongStream
defer
(Supplier<LongStream> supplier) Lazy evaluation.elementAt
(long position) static LongStream
empty()
abstract <E extends Exception>
u.OptionalLongfindAny
(Throwables.LongPredicate<E> predicate) abstract <E extends Exception>
u.OptionalLongfindFirst
(Throwables.LongPredicate<E> predicate) abstract <E extends Exception>
u.OptionalLongfindFirstOrAny
(Throwables.LongPredicate<E> predicateForFirst) Returns the first element matched bypredicateForFirst
if found or the first element if this stream is not empty Otherwise an emptyOptionalLong
will be returned.abstract <E extends Exception>
u.OptionalLongfindFirstOrLast
(Throwables.LongPredicate<E> predicateForFirst) Returns the first element matched bypredicateForFirst
if found or the last element if this stream is not empty Otherwise an emptyOptionalLong
will be returned.abstract <E extends Exception>
u.OptionalLongfindLast
(Throwables.LongPredicate<E> predicate) Consider using:stream.reversed().findFirst(predicate)
for better performance if possible.abstract LongStream
flatmap
(LongFunction<long[]> mapper) abstract LongStream
flatMap
(LongFunction<? extends LongStream> mapper) abstract DoubleStream
flatMapToDouble
(LongFunction<? extends DoubleStream> mapper) abstract FloatStream
flatMapToFloat
(LongFunction<? extends FloatStream> mapper) abstract IntStream
flatMapToInt
(LongFunction<? extends IntStream> mapper) abstract <T> Stream<T>
flatmapToObj
(LongFunction<? extends Collection<? extends T>> mapper) abstract <T> Stream<T>
flatMapToObj
(LongFunction<? extends Stream<? extends T>> mapper) static LongStream
flatten
(long[][] a) static LongStream
flatten
(long[][][] a) static LongStream
flatten
(long[][] a, boolean vertically) static LongStream
flatten
(long[][] a, long valueForNone, boolean vertically) abstract LongStream
flattmap
(LongFunction<? extends LongStream> mapper) abstract <T> Stream<T>
flattMapToObj
(LongFunction<T[]> mapper) abstract <E extends Exception>
voidforEach
(Throwables.LongConsumer<E> action) abstract <E extends Exception>
voidforEachIndexed
(Throwables.IndexedLongConsumer<E> action) static LongStream
groupTo
(Throwables.LongFunction<? extends K, E> keyMapper, Collector<? super Long, ?, D> downstream) groupTo
(Throwables.LongFunction<? extends K, E> keyMapper, Collector<? super Long, ?, D> downstream, Supplier<? extends M> mapFactory) static LongStream
interval
(long intervalInMillis) static LongStream
interval
(long delayInMillis, long intervalInMillis) Generates the long value by the specified period: [0, 1, 2, 3...]static LongStream
Generates the long value by the specified period: [0, 1, 2, 3...]boolean
static LongStream
iterate
(long init, BooleanSupplier hasNext, LongUnaryOperator f) static LongStream
iterate
(long init, LongPredicate hasNext, LongUnaryOperator f) static LongStream
iterate
(long init, LongUnaryOperator f) static LongStream
iterate
(BooleanSupplier hasNext, LongSupplier next) iterator()
Remember to close this Stream after the iteration is done, if needed.join
(CharSequence delimiter) abstract u.OptionalLong
kthLargest
(int k) abstract LongStream
map
(LongUnaryOperator mapper) abstract LongStream
mapMulti
(LongMapMultiConsumer mapper) abstract LongStream
mapPartial
(LongFunction<u.OptionalLong> mapper) Note: copied from StreamEx: https://github.com/amaembo/streamexabstract LongStream
mapPartialJdk
(LongFunction<OptionalLong> mapper) Note: copied from StreamEx: https://github.com/amaembo/streamexabstract DoubleStream
mapToDouble
(LongToDoubleFunction mapper) abstract FloatStream
mapToFloat
(LongToFloatFunction mapper) abstract IntStream
mapToInt
(LongToIntFunction mapper) abstract <T> Stream<T>
mapToObj
(LongFunction<? extends T> mapper) abstract u.OptionalLong
max()
static LongStream
merge
(long[] a, long[] b, long[] c, LongBiFunction<MergeResult> nextSelector) static LongStream
merge
(long[] a, long[] b, LongBiFunction<MergeResult> nextSelector) static LongStream
merge
(LongIterator a, LongIterator b, LongBiFunction<MergeResult> nextSelector) static LongStream
merge
(LongIterator a, LongIterator b, LongIterator c, LongBiFunction<MergeResult> nextSelector) static LongStream
merge
(LongStream a, LongStream b, LongBiFunction<MergeResult> nextSelector) static LongStream
merge
(LongStream a, LongStream b, LongStream c, LongBiFunction<MergeResult> nextSelector) static LongStream
merge
(Collection<? extends LongStream> c, LongBiFunction<MergeResult> nextSelector) abstract LongStream
mergeWith
(LongStream b, LongBiFunction<MergeResult> nextSelector) abstract u.OptionalLong
min()
abstract <E extends Exception>
booleannoneMatch
(Throwables.LongPredicate<E> predicate) static LongStream
of
(long... a) static LongStream
of
(long[] a, int startIndex, int endIndex) static LongStream
of
(LongIterator iterator) static LongStream
of
(u.OptionalLong op) static LongStream
static LongStream
static LongStream
of
(LongBuffer buf) static LongStream
of
(Collection<Long> c) static LongStream
of
(OptionalLong op) static LongStream
of
(LongStream stream) static LongStream
ofNullable
(Long e) parallel()
Consider usingsps(Function)
if only next operation need to be parallelized.parallel
(int maxThreadNum) Consider usingsps(int, Function)
if only next operation need to be parallelized.parallel
(int maxThreadNum, BaseStream.Splitor splitor) Returns an equivalent stream that is parallel.parallel
(int maxThreadNum, BaseStream.Splitor splitor, Executor executor) stream.parallel(maxThreadNum, splitor, executor).map(f).filter(p)...; // Replace above line of code with "sps" if only "f" need to be parallelized.// Replace above line of code with "sps" if only "f" need to be parallelized.stream.parallel(parallelSettings).map(f).filter(p)...; // Replace above line of code with "sps" if only "f" need to be parallelized.parallel
(BaseStream.Splitor splitor) stream.parallel(executor).map(f).filter(p)...; // Replace above line of code with "sps" if only "f" need to be parallelized.static LongStream
parallelMerge
(Collection<? extends LongStream> c, LongBiFunction<MergeResult> nextSelector) All the elements from each inputCollection/Iterator/Stream
will be merged into two queues by multiple threads.static LongStream
parallelMerge
(Collection<? extends LongStream> c, LongBiFunction<MergeResult> nextSelector, int maxThreadNum) All the elements from each inputCollection/Iterator/Stream
will be merged into two queues by multiple threads.peek
(LongConsumer action) abstract LongStream
prepend
(long... a) void
println()
<SS extends BaseStream>
SSpsp
(Function<? super LongStream, ? extends SS> ops) Temporarily switch the stream to sequence stream for operationops
and then switch back to parallel stream with samemaxThreadNum/splitor/asyncExecutor
.static LongStream
random()
static LongStream
range
(long startInclusive, long endExclusive) static LongStream
range
(long startInclusive, long endExclusive, long by) static LongStream
rangeClosed
(long startInclusive, long endInclusive) static LongStream
rangeClosed
(long startInclusive, long endInclusive, long by) abstract LongStream
rangeMap
(LongBiPredicate sameRange, LongBinaryOperator mapper) Note: copied from StreamEx: https://github.com/amaembo/streamex
Returns a stream consisting of results of applying the given function to the ranges created from the source elements.abstract <T> Stream<T>
rangeMapToObj
(LongBiPredicate sameRange, LongBiFunction<? extends T> mapper) Note: copied from StreamEx: https://github.com/amaembo/streamex
Returns a stream consisting of results of applying the given function to the ranges created from the source elements.rateLimited
(double permitsPerSecond) rateLimited
(RateLimiter rateLimiter) abstract long
reduce
(long identity, LongBinaryOperator op) abstract u.OptionalLong
static LongStream
repeat
(long element, long n) abstract LongStream
scan
(long init, LongBinaryOperator accumulator) Returns aStream
produced by iterative application of a accumulation function to an initial elementinit
and next element of the current stream.abstract LongStream
scan
(long init, LongBinaryOperator accumulator, boolean initIncluded) abstract LongStream
scan
(LongBinaryOperator accumulator) Returns aStream
produced by iterative application of a accumulation function to an initial elementinit
and next element of the current stream.shuffled()
This method only runs sequentially, even in parallel stream and all elements will be loaded to memory.skipUntil
(LongPredicate predicate) sliding
(int windowSize) slidingToList
(int windowSize) <SS extends BaseStream>
SSsps
(int maxThreadNum, Function<? super LongStream, ? extends SS> ops) Temporarily switch the stream to parallel stream for operationops
and then switch back to sequence stream.<SS extends BaseStream>
SSsps
(Function<? super LongStream, ? extends SS> ops) Temporarily switch the stream to parallel stream for operationops
and then switch back to sequence stream.abstract long
sum()
abstract LongSummaryStatistics
abstract Pair<LongSummaryStatistics,
u.Optional<Map<Percentage, Long>>> throwIfEmpty
(Supplier<? extends RuntimeException> exceptionSupplier) long[]
toArray()
abstract LongStream
abstract LongList
toMap
(Throwables.LongFunction<? extends K, E> keyMapper, Throwables.LongFunction<? extends V, E2> valueMapper) toMap
(Throwables.LongFunction<? extends K, E> keyMapper, Throwables.LongFunction<? extends V, E2> valueMapper, BinaryOperator<V> mergeFunction) toMap
(Throwables.LongFunction<? extends K, E> keyMapper, Throwables.LongFunction<? extends V, E2> valueMapper, BinaryOperator<V> mergeFunction, Supplier<? extends M> mapFactory) toMap
(Throwables.LongFunction<? extends K, E> keyMapper, Throwables.LongFunction<? extends V, E2> valueMapper, Supplier<? extends M> mapFactory) abstract LongStream
top
(int n)
This method only runs sequentially, even in parallel stream.abstract LongStream
top
(int n, Comparator<? super Long> comparator)
This method only runs sequentially, even in parallel stream.<SS extends BaseStream>
SStransform
(Function<? super LongStream, ? extends SS> transfer) static LongStream
zip
(long[] a, long[] b, long[] c, long valueForNoneA, long valueForNoneB, long valueForNoneC, LongTernaryOperator zipFunction) Zip together the "a", "b" and "c" iterators until all of them runs out of values.static LongStream
zip
(long[] a, long[] b, long[] c, LongTernaryOperator zipFunction) Zip together the "a", "b" and "c" arrays until one of them runs out of values.static LongStream
zip
(long[] a, long[] b, long valueForNoneA, long valueForNoneB, LongBinaryOperator zipFunction) Zip together the "a" and "b" iterators until all of them runs out of values.static LongStream
zip
(long[] a, long[] b, LongBinaryOperator zipFunction) Zip together the "a" and "b" arrays until one of them runs out of values.static LongStream
zip
(LongIterator a, LongIterator b, long valueForNoneA, long valueForNoneB, LongBinaryOperator zipFunction) Zip together the "a" and "b" iterators until all of them runs out of values.static LongStream
zip
(LongIterator a, LongIterator b, LongIterator c, long valueForNoneA, long valueForNoneB, long valueForNoneC, LongTernaryOperator zipFunction) Zip together the "a", "b" and "c" iterators until all of them runs out of values.static LongStream
zip
(LongIterator a, LongIterator b, LongIterator c, LongTernaryOperator zipFunction) Zip together the "a", "b" and "c" iterators until one of them runs out of values.static LongStream
zip
(LongIterator a, LongIterator b, LongBinaryOperator zipFunction) Zip together the "a" and "b" iterators until one of them runs out of values.static LongStream
zip
(LongStream a, LongStream b, long valueForNoneA, long valueForNoneB, LongBinaryOperator zipFunction) Zip together the "a" and "b" iterators until all of them runs out of values.static LongStream
zip
(LongStream a, LongStream b, LongStream c, long valueForNoneA, long valueForNoneB, long valueForNoneC, LongTernaryOperator zipFunction) Zip together the "a", "b" and "c" iterators until all of them runs out of values.static LongStream
zip
(LongStream a, LongStream b, LongStream c, LongTernaryOperator zipFunction) Zip together the "a", "b" and "c" streams until one of them runs out of values.static LongStream
zip
(LongStream a, LongStream b, LongBinaryOperator zipFunction) Zip together the "a" and "b" streams until one of them runs out of values.static LongStream
zip
(Collection<? extends LongStream> c, long[] valuesForNone, LongNFunction<Long> zipFunction) Zip together the iterators until all of them runs out of values.static LongStream
zip
(Collection<? extends LongStream> c, LongNFunction<Long> zipFunction) Zip together the iterators until one of them runs out of values.abstract LongStream
zipWith
(LongStream b, long valueForNoneA, long valueForNoneB, LongBinaryOperator zipFunction) abstract LongStream
zipWith
(LongStream b, LongStream c, long valueForNoneA, long valueForNoneB, long valueForNoneC, LongTernaryOperator zipFunction) abstract LongStream
zipWith
(LongStream b, LongStream c, LongTernaryOperator zipFunction) abstract LongStream
zipWith
(LongStream b, LongBinaryOperator zipFunction) Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.landawn.abacus.util.stream.BaseStream
acceptIfNotEmpty, append, append, appendIfEmpty, applyIfNotEmpty, count, cycled, cycled, difference, distinct, dropWhile, dropWhile, filter, filter, first, indexed, intersection, join, last, limit, onClose, onEach, onlyOne, percentiles, prepend, prepend, reversed, reverseSorted, rotated, shuffled, skip, skip, sliding, slidingToList, sorted, split, split, splitAt, splitAt, splitToList, splitToList, step, symmetricDifference, takeWhile, toCollection, toList, toLongMultiset, toLongMultiset, toMultiset, toMultiset, toSet
-
Method Details
-
rateLimited
- Returns:
- See Also:
-
delay
- Returns:
-
skipUntil
- Returns:
- See Also:
-
map
-
mapToInt
-
mapToFloat
@ParallelSupported @IntermediateOp public abstract FloatStream mapToFloat(LongToFloatFunction mapper) -
mapToDouble
@ParallelSupported @IntermediateOp public abstract DoubleStream mapToDouble(LongToDoubleFunction mapper) -
mapToObj
@ParallelSupported @IntermediateOp public abstract <T> Stream<T> mapToObj(LongFunction<? extends T> mapper) -
flatMap
@ParallelSupported @IntermediateOp public abstract LongStream flatMap(LongFunction<? extends LongStream> mapper) -
flatmap
-
flattmap
@Beta @ParallelSupported @IntermediateOp public abstract LongStream flattmap(LongFunction<? extends LongStream> mapper) -
flatMapToInt
@ParallelSupported @IntermediateOp public abstract IntStream flatMapToInt(LongFunction<? extends IntStream> mapper) -
flatMapToFloat
@ParallelSupported @IntermediateOp public abstract FloatStream flatMapToFloat(LongFunction<? extends FloatStream> mapper) -
flatMapToDouble
@ParallelSupported @IntermediateOp public abstract DoubleStream flatMapToDouble(LongFunction<? extends DoubleStream> mapper) -
flatMapToObj
@ParallelSupported @IntermediateOp public abstract <T> Stream<T> flatMapToObj(LongFunction<? extends Stream<? extends T>> mapper) -
flatmapToObj
@ParallelSupported @IntermediateOp public abstract <T> Stream<T> flatmapToObj(LongFunction<? extends Collection<? extends T>> mapper) -
flattMapToObj
@ParallelSupported @IntermediateOp public abstract <T> Stream<T> flattMapToObj(LongFunction<T[]> mapper) -
mapMulti
-
mapPartial
@Beta @ParallelSupported @IntermediateOp public abstract LongStream mapPartial(LongFunction<u.OptionalLong> mapper) Note: copied from StreamEx: https://github.com/amaembo/streamex- Type Parameters:
R
-- Parameters:
mapper
-- Returns:
-
mapPartialJdk
@Beta @ParallelSupported @IntermediateOp public abstract LongStream mapPartialJdk(LongFunction<OptionalLong> mapper) Note: copied from StreamEx: https://github.com/amaembo/streamex- Type Parameters:
R
-- Parameters:
mapper
-- Returns:
-
rangeMap
@SequentialOnly @IntermediateOp public abstract LongStream rangeMap(LongBiPredicate sameRange, LongBinaryOperator mapper) Note: copied from StreamEx: https://github.com/amaembo/streamex
Returns a stream consisting of results of applying the given function to the ranges created from the source elements. This is a quasi-intermediate partial reduction operation.- Parameters:
sameRange
- a non-interfering, stateless predicate to apply to the leftmost and next elements which returns true for elements which belong to the same range.mapper
- a non-interfering, stateless function to apply to the range borders and produce the resulting element. If value was not merged to the interval, then mapper will receive the same value twice, otherwise it will receive the leftmost and the rightmost values which were merged to the range.- Returns:
- See Also:
-
rangeMapToObj
@SequentialOnly @IntermediateOp public abstract <T> Stream<T> rangeMapToObj(LongBiPredicate sameRange, LongBiFunction<? extends T> mapper) Note: copied from StreamEx: https://github.com/amaembo/streamex
Returns a stream consisting of results of applying the given function to the ranges created from the source elements. This is a quasi-intermediate partial reduction operation.- Parameters:
sameRange
- a non-interfering, stateless predicate to apply to the leftmost and next elements which returns true for elements which belong to the same range.mapper
- a non-interfering, stateless function to apply to the range borders and produce the resulting element. If value was not merged to the interval, then mapper will receive the same value twice, otherwise it will receive the leftmost and the rightmost values which were merged to the range.- Returns:
- See Also:
-
collapse
@SequentialOnly @IntermediateOp public abstract Stream<LongList> collapse(LongBiPredicate collapsible) Merge series of adjacent elements which satisfy the given predicate using the merger function and return a new stream.
This method only runs sequentially, even in parallel stream.- Parameters:
collapsible
-- Returns:
-
collapse
@SequentialOnly @IntermediateOp public abstract LongStream collapse(LongBiPredicate collapsible, LongBinaryOperator mergeFunction) Merge series of adjacent elements which satisfy the given predicate using the merger function and return a new stream.
This method only runs sequentially, even in parallel stream.- Parameters:
collapsible
-mergeFunction
-- Returns:
-
collapse
@SequentialOnly @IntermediateOp public abstract LongStream collapse(LongTriPredicate collapsible, LongBinaryOperator mergeFunction) - Parameters:
collapsible
- test the current element with the first element and previous element in the series. The first parameter is the first element of this series, the second parameter is the previous element and the third parameter is the current element.mergeFunction
-- Returns:
-
scan
Returns aStream
produced by iterative application of a accumulation function to an initial elementinit
and next element of the current stream. Produces aStream
consisting ofinit
,acc(init, value1)
,acc(acc(init, value1), value2)
, etc.This is an intermediate operation.
Example:
accumulator: (a, b) -> a + b stream: [1, 2, 3, 4, 5] result: [1, 3, 6, 10, 15]
This method only runs sequentially, even in parallel stream.- Parameters:
accumulator
- the accumulation function- Returns:
-
scan
@SequentialOnly @IntermediateOp public abstract LongStream scan(long init, LongBinaryOperator accumulator) Returns aStream
produced by iterative application of a accumulation function to an initial elementinit
and next element of the current stream. Produces aStream
consisting ofinit
,acc(init, value1)
,acc(acc(init, value1), value2)
, etc.This is an intermediate operation.
Example:
init:10 accumulator: (a, b) -> a + b stream: [1, 2, 3, 4, 5] result: [11, 13, 16, 20, 25]
This method only runs sequentially, even in parallel stream.- Parameters:
init
- the initial value. it's only used once byaccumulator
to calculate the fist element in the returned stream. It will be ignored if this stream is empty and won't be the first element of the returned stream.accumulator
- the accumulation function- Returns:
-
scan
@SequentialOnly @IntermediateOp public abstract LongStream scan(long init, LongBinaryOperator accumulator, boolean initIncluded) - Parameters:
init
-accumulator
-initIncluded
-- Returns:
-
prepend
-
append
-
appendIfEmpty
-
top
This method only runs sequentially, even in parallel stream.- Parameters:
n
-- Returns:
-
top
@SequentialOnly @IntermediateOp public abstract LongStream top(int n, Comparator<? super Long> comparator)
This method only runs sequentially, even in parallel stream.- Parameters:
n
-- Returns:
-
toLongList
-
toMap
@ParallelSupported @TerminalOp public abstract <K,V, Map<K,E extends Exception, E2 extends Exception> V> toMap(Throwables.LongFunction<? extends K, E> keyMapper, Throwables.LongFunction<? extends V, throws E, E2E2> valueMapper) -
toMap
@ParallelSupported @TerminalOp public abstract <K,V, M toMapM extends Map<K, V>, E extends Exception, E2 extends Exception> (Throwables.LongFunction<? extends K, E> keyMapper, Throwables.LongFunction<? extends V, throws E, E2E2> valueMapper, Supplier<? extends M> mapFactory) -
toMap
@ParallelSupported @TerminalOp public abstract <K,V, Map<K,E extends Exception, E2 extends Exception> V> toMap(Throwables.LongFunction<? extends K, E> keyMapper, Throwables.LongFunction<? extends V, throws E, E2E2> valueMapper, BinaryOperator<V> mergeFunction) -
toMap
@ParallelSupported @TerminalOp public abstract <K,V, M toMapM extends Map<K, V>, E extends Exception, E2 extends Exception> (Throwables.LongFunction<? extends K, E> keyMapper, Throwables.LongFunction<? extends V, throws E, E2E2> valueMapper, BinaryOperator<V> mergeFunction, Supplier<? extends M> mapFactory) -
groupTo
@ParallelSupported @TerminalOp public abstract <K,D, Map<K,E extends Exception> D> groupTo(Throwables.LongFunction<? extends K, E> keyMapper, Collector<? super Long, throws E?, D> downstream) - Parameters:
keyMapper
-downstream
-- Returns:
- Throws:
E extends Exception
- See Also:
-
Collectors#groupingBy(Throwables.Function, Collector)
-
groupTo
@ParallelSupported @TerminalOp public abstract <K,D, M groupToM extends Map<K, D>, E extends Exception> (Throwables.LongFunction<? extends K, E> keyMapper, Collector<? super Long, throws E?, D> downstream, Supplier<? extends M> mapFactory) - Parameters:
keyMapper
-downstream
-mapFactory
-- Returns:
- Throws:
E extends Exception
- See Also:
-
Collectors#groupingBy(Throwables.Function, Collector, Supplier)
-
reduce
-
reduce
-
collect
@ParallelSupported @TerminalOp public abstract <R> R collect(Supplier<R> supplier, ObjLongConsumer<? super R> accumulator, BiConsumer<R, R> combiner) - Type Parameters:
R
-- Parameters:
supplier
-accumulator
-combiner
-- Returns:
- See Also:
-
collect
@ParallelSupported @TerminalOp public abstract <R> R collect(Supplier<R> supplier, ObjLongConsumer<? super R> accumulator) Only call this method when the returned typeR
is one types:Collection/Map/StringBuilder/Multiset/LongMultiset/Multimap/BooleanList/IntList/.../DoubleList
. Otherwise, please callcollect(Supplier, ObjLongConsumer, BiConsumer)
.- Parameters:
supplier
-accumulator
-- Returns:
- See Also:
-
forEach
@ParallelSupported @TerminalOp public abstract <E extends Exception> void forEach(Throwables.LongConsumer<E> action) throws E - Throws:
E extends Exception
-
forEachIndexed
@ParallelSupported @TerminalOp public abstract <E extends Exception> void forEachIndexed(Throwables.IndexedLongConsumer<E> action) throws E - Throws:
E extends Exception
-
anyMatch
@ParallelSupported @TerminalOp public abstract <E extends Exception> boolean anyMatch(Throwables.LongPredicate<E> predicate) throws E - Throws:
E extends Exception
-
allMatch
@ParallelSupported @TerminalOp public abstract <E extends Exception> boolean allMatch(Throwables.LongPredicate<E> predicate) throws E - Throws:
E extends Exception
-
noneMatch
@ParallelSupported @TerminalOp public abstract <E extends Exception> boolean noneMatch(Throwables.LongPredicate<E> predicate) throws E - Throws:
E extends Exception
-
findFirst
@ParallelSupported @TerminalOp public abstract <E extends Exception> u.OptionalLong findFirst(Throwables.LongPredicate<E> predicate) throws E - Throws:
E extends Exception
-
findFirstOrAny
@ParallelSupported @TerminalOp public abstract <E extends Exception> u.OptionalLong findFirstOrAny(Throwables.LongPredicate<E> predicateForFirst) throws E Returns the first element matched bypredicateForFirst
if found or the first element if this stream is not empty Otherwise an emptyOptionalLong
will be returned.- Type Parameters:
E
-- Parameters:
predicateForFirst
-- Returns:
- Throws:
E
-
findFirstOrLast
@ParallelSupported @TerminalOp public abstract <E extends Exception> u.OptionalLong findFirstOrLast(Throwables.LongPredicate<E> predicateForFirst) throws E Returns the first element matched bypredicateForFirst
if found or the last element if this stream is not empty Otherwise an emptyOptionalLong
will be returned.- Type Parameters:
E
-- Parameters:
predicateForFirst
-- Returns:
- Throws:
E
-
findLast
@ParallelSupported @TerminalOp public abstract <E extends Exception> u.OptionalLong findLast(Throwables.LongPredicate<E> predicate) throws E Consider using:stream.reversed().findFirst(predicate)
for better performance if possible.- Type Parameters:
E
-- Parameters:
predicate
-- Returns:
- Throws:
E
-
findAny
@ParallelSupported @TerminalOp public abstract <E extends Exception> u.OptionalLong findAny(Throwables.LongPredicate<E> predicate) throws E - Throws:
E extends Exception
-
min
-
max
-
kthLargest
- Parameters:
k
-- Returns:
- OptionalByte.empty() if there is no element or count less than k, otherwise the kth largest element.
-
sum
-
average
-
summarize
-
summarizeAndPercentiles
@SequentialOnly @TerminalOp public abstract Pair<LongSummaryStatistics,u.Optional<Map<Percentage, summarizeAndPercentiles()Long>>> -
mergeWith
@SequentialOnly @IntermediateOp public abstract LongStream mergeWith(LongStream b, LongBiFunction<MergeResult> nextSelector) - Parameters:
b
-nextSelector
- first parameter is selected ifNth.FIRST
is returned, otherwise the second parameter is selected.- Returns:
-
zipWith
@ParallelSupported @IntermediateOp public abstract LongStream zipWith(LongStream b, LongBinaryOperator zipFunction) -
zipWith
@ParallelSupported @IntermediateOp public abstract LongStream zipWith(LongStream b, LongStream c, LongTernaryOperator zipFunction) -
zipWith
@ParallelSupported @IntermediateOp public abstract LongStream zipWith(LongStream b, long valueForNoneA, long valueForNoneB, LongBinaryOperator zipFunction) -
zipWith
@ParallelSupported @IntermediateOp public abstract LongStream zipWith(LongStream b, LongStream c, long valueForNoneA, long valueForNoneB, long valueForNoneC, LongTernaryOperator zipFunction) -
asFloatStream
Deprecated.- Returns:
-
asDoubleStream
-
toJdkStream
-
boxed
-
iterator
Remember to close this Stream after the iteration is done, if needed.- Returns:
-
empty
-
ofNullable
-
of
-
of
-
of
-
of
-
of
-
of
-
of
-
of
-
of
-
of
-
defer
Lazy evaluation.
This is equal to:Stream.just(supplier).flatMapToLong(it -> it.get())
.- Type Parameters:
T
-- Parameters:
supplier
-- Returns:
-
flatten
-
flatten
-
flatten
-
flatten
-
range
-
range
-
rangeClosed
-
rangeClosed
-
repeat
-
random
-
iterate
-
iterate
-
iterate
- Parameters:
init
-hasNext
- test if has next by hasNext.test(init) for first time and hasNext.test(f.apply(previous)) for remaining.f
-- Returns:
-
iterate
-
generate
-
interval
- Parameters:
intervalInMillis
-- Returns:
-
interval
Generates the long value by the specified period: [0, 1, 2, 3...]- Parameters:
delayInMillis
-intervalInMillis
-- Returns:
-
interval
Generates the long value by the specified period: [0, 1, 2, 3...]- Parameters:
delay
-interval
-unit
-- Returns:
-
concat
-
concat
-
concat
-
concat
-
concat
-
concatIterators
-
zip
Zip together the "a" and "b" arrays until one of them runs out of values. Each pair of values is combined into a single value using the supplied zipFunction function.- Parameters:
a
-b
-- Returns:
-
zip
Zip together the "a", "b" and "c" arrays until one of them runs out of values. Each triple of values is combined into a single value using the supplied zipFunction function.- Parameters:
a
-b
-c
-- Returns:
-
zip
Zip together the "a" and "b" iterators until one of them runs out of values. Each pair of values is combined into a single value using the supplied zipFunction function.- Parameters:
a
-b
-- Returns:
-
zip
public static LongStream zip(LongIterator a, LongIterator b, LongIterator c, LongTernaryOperator zipFunction) Zip together the "a", "b" and "c" iterators until one of them runs out of values. Each triple of values is combined into a single value using the supplied zipFunction function.- Parameters:
a
-b
-- Returns:
-
zip
Zip together the "a" and "b" streams until one of them runs out of values. Each pair of values is combined into a single value using the supplied zipFunction function.- Parameters:
a
-b
-- Returns:
-
zip
public static LongStream zip(LongStream a, LongStream b, LongStream c, LongTernaryOperator zipFunction) Zip together the "a", "b" and "c" streams until one of them runs out of values. Each triple of values is combined into a single value using the supplied zipFunction function.- Parameters:
a
-b
-- Returns:
-
zip
Zip together the iterators until one of them runs out of values. Each array of values is combined into a single value using the supplied zipFunction function.- Parameters:
c
-zipFunction
-- Returns:
-
zip
public static LongStream zip(long[] a, long[] b, long valueForNoneA, long valueForNoneB, LongBinaryOperator zipFunction) Zip together the "a" and "b" iterators until all of them runs out of values. Each pair of values is combined into a single value using the supplied zipFunction function.- Parameters:
a
-b
-valueForNoneA
- value to fill if "a" runs out of values first.valueForNoneB
- value to fill if "b" runs out of values first.zipFunction
-- Returns:
-
zip
public static LongStream zip(long[] a, long[] b, long[] c, long valueForNoneA, long valueForNoneB, long valueForNoneC, LongTernaryOperator zipFunction) Zip together the "a", "b" and "c" iterators until all of them runs out of values. Each triple of values is combined into a single value using the supplied zipFunction function.- Parameters:
a
-b
-c
-valueForNoneA
- value to fill if "a" runs out of values.valueForNoneB
- value to fill if "b" runs out of values.valueForNoneC
- value to fill if "c" runs out of values.zipFunction
-- Returns:
-
zip
public static LongStream zip(LongIterator a, LongIterator b, long valueForNoneA, long valueForNoneB, LongBinaryOperator zipFunction) Zip together the "a" and "b" iterators until all of them runs out of values. Each pair of values is combined into a single value using the supplied zipFunction function.- Parameters:
a
-b
-valueForNoneA
- value to fill if "a" runs out of values first.valueForNoneB
- value to fill if "b" runs out of values first.zipFunction
-- Returns:
-
zip
public static LongStream zip(LongIterator a, LongIterator b, LongIterator c, long valueForNoneA, long valueForNoneB, long valueForNoneC, LongTernaryOperator zipFunction) Zip together the "a", "b" and "c" iterators until all of them runs out of values. Each triple of values is combined into a single value using the supplied zipFunction function.- Parameters:
a
-b
-c
-valueForNoneA
- value to fill if "a" runs out of values.valueForNoneB
- value to fill if "b" runs out of values.valueForNoneC
- value to fill if "c" runs out of values.zipFunction
-- Returns:
-
zip
public static LongStream zip(LongStream a, LongStream b, long valueForNoneA, long valueForNoneB, LongBinaryOperator zipFunction) Zip together the "a" and "b" iterators until all of them runs out of values. Each pair of values is combined into a single value using the supplied zipFunction function.- Parameters:
a
-b
-valueForNoneA
- value to fill if "a" runs out of values first.valueForNoneB
- value to fill if "b" runs out of values first.zipFunction
-- Returns:
-
zip
public static LongStream zip(LongStream a, LongStream b, LongStream c, long valueForNoneA, long valueForNoneB, long valueForNoneC, LongTernaryOperator zipFunction) Zip together the "a", "b" and "c" iterators until all of them runs out of values. Each triple of values is combined into a single value using the supplied zipFunction function.- Parameters:
a
-b
-c
-valueForNoneA
- value to fill if "a" runs out of values.valueForNoneB
- value to fill if "b" runs out of values.valueForNoneC
- value to fill if "c" runs out of values.zipFunction
-- Returns:
-
zip
public static LongStream zip(Collection<? extends LongStream> c, long[] valuesForNone, LongNFunction<Long> zipFunction) Zip together the iterators until all of them runs out of values. Each array of values is combined into a single value using the supplied zipFunction function.- Parameters:
c
-valuesForNone
- value to fill for any iterator runs out of values.zipFunction
-- Returns:
-
merge
- Parameters:
a
-b
-nextSelector
- first parameter is selected ifNth.FIRST
is returned, otherwise the second parameter is selected.- Returns:
-
merge
public static LongStream merge(long[] a, long[] b, long[] c, LongBiFunction<MergeResult> nextSelector) - Parameters:
a
-b
-c
-nextSelector
- first parameter is selected ifNth.FIRST
is returned, otherwise the second parameter is selected.- Returns:
-
merge
public static LongStream merge(LongIterator a, LongIterator b, LongBiFunction<MergeResult> nextSelector) - Parameters:
a
-b
-nextSelector
- first parameter is selected ifNth.FIRST
is returned, otherwise the second parameter is selected.- Returns:
-
merge
public static LongStream merge(LongIterator a, LongIterator b, LongIterator c, LongBiFunction<MergeResult> nextSelector) - Parameters:
a
-b
-c
-nextSelector
- first parameter is selected ifNth.FIRST
is returned, otherwise the second parameter is selected.- Returns:
-
merge
public static LongStream merge(LongStream a, LongStream b, LongBiFunction<MergeResult> nextSelector) - Parameters:
a
-b
-nextSelector
- first parameter is selected ifNth.FIRST
is returned, otherwise the second parameter is selected.- Returns:
-
merge
public static LongStream merge(LongStream a, LongStream b, LongStream c, LongBiFunction<MergeResult> nextSelector) - Parameters:
a
-b
-c
-nextSelector
- first parameter is selected ifNth.FIRST
is returned, otherwise the second parameter is selected.- Returns:
-
merge
public static LongStream merge(Collection<? extends LongStream> c, LongBiFunction<MergeResult> nextSelector) - Parameters:
c
-nextSelector
- first parameter is selected ifNth.FIRST
is returned, otherwise the second parameter is selected.- Returns:
-
parallelMerge
public static LongStream parallelMerge(Collection<? extends LongStream> c, LongBiFunction<MergeResult> nextSelector) All the elements from each inputCollection/Iterator/Stream
will be merged into two queues by multiple threads. Then these two new queues will be merged into oneIterator/Stream
by one thread. So it's not totally lazy evaluation and may cause out of memory error if there are too many elements merged into the new queues. Consider usingmerge
, which is totally lazy evaluation.- Parameters:
c
-nextSelector
- first parameter is selected ifNth.FIRST
is returned, otherwise the second parameter is selected.- Returns:
-
parallelMerge
public static LongStream parallelMerge(Collection<? extends LongStream> c, LongBiFunction<MergeResult> nextSelector, int maxThreadNum) All the elements from each inputCollection/Iterator/Stream
will be merged into two queues by multiple threads. Then these two new queues will be merged into oneIterator/Stream
by one thread. So it's not totally lazy evaluation and may cause out of memory error if there are too many elements merged into the new queues. Consider usingmerge
, which is totally lazy evaluation.- Parameters:
c
-nextSelector
- first parameter is selected ifNth.FIRST
is returned, otherwise the second parameter is selected.maxThreadNum
-- Returns:
-
elementAt
- Specified by:
elementAt
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Parameters:
position
-- Returns:
-
rateLimited
- Specified by:
rateLimited
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Parameters:
permitsPerSecond
-- Returns:
- See Also:
-
peek
- Specified by:
peek
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Parameters:
action
-- Returns:
-
sliding
- Specified by:
sliding
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Parameters:
windowSize
-- Returns:
- See Also:
-
slidingToList
- Specified by:
slidingToList
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Parameters:
windowSize
-- Returns:
- See Also:
-
shuffled
Description copied from interface:BaseStream
This method only runs sequentially, even in parallel stream and all elements will be loaded to memory.- Specified by:
shuffled
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Returns:
-
throwIfEmpty
@SequentialOnly @IntermediateOp public LongStream throwIfEmpty(Supplier<? extends RuntimeException> exceptionSupplier) - Specified by:
throwIfEmpty
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Parameters:
exceptionSupplier
-- Returns:
-
toImmutableList
- Specified by:
toImmutableList
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Returns:
-
toImmutableSet
- Specified by:
toImmutableSet
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Returns:
-
join
- Specified by:
join
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Parameters:
delimiter
-- Returns:
-
println
public void println()- Specified by:
println
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>>
-
transform
- Specified by:
transform
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Type Parameters:
SS
-- Parameters:
transfer
-- Returns:
-
__
@Deprecated public <SS extends BaseStream> SS __(Function<? super LongStream, ? extends SS> transfer) Deprecated.replaced byBaseStream.transform(Function)
- Specified by:
__
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Type Parameters:
SS
-- Parameters:
transfer
-- Returns:
-
isParallel
public boolean isParallel()- Specified by:
isParallel
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Returns:
-
sequential
- Specified by:
sequential
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Returns:
-
parallel
Description copied from interface:BaseStream
Consider usingsps(Function)
if only next operation need to be parallelized. For example:stream.parallel().map(f).filter(p)...; // Replace above line of code with "sps" if only "f" need to be parallelized. And "p" is fast enough to be executed in sequential Stream. stream.sps(s -> s.map(f)).filter(p)...; // Or switch the stream back sequential stream if don't use "sps". stream.parallel().map(f).sequential().filter(p)...;
In most scenarios, there could be only one operation need be parallelized in the stream. Sosps(Function)
is recommended in most of scenarios.- Specified by:
parallel
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Returns:
- See Also:
-
parallel
Description copied from interface:BaseStream
Consider usingsps(int, Function)
if only next operation need to be parallelized. For example:stream.parallel(maxThreadNum).map(f).filter(p)...; // Replace above line of code with "sps" if only "f" need to be parallelized. And "p" is fast enough to be executed in sequential Stream. stream.sps(maxThreadNum, s -> s.map(f)).filter(p)...; // Or switch the stream back sequential stream if don't use "sps". stream.parallel(maxThreadNum).map(f).sequential().filter(p)...;
In most scenarios, there could be only one operation need be parallelized in the stream. Sosps(int, Function)
is recommended in most of scenarios.- Specified by:
parallel
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Parameters:
maxThreadNum
-- Returns:
- See Also:
-
#maxThreadNumPerOperation()
-
parallel
- Specified by:
parallel
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Parameters:
splitor
-- Returns:
- See Also:
-
parallel
Description copied from interface:BaseStream
Returns an equivalent stream that is parallel. May return itself if the stream was already parallel with the samemaxThreadNum
andsplitor
as the specified ones.
When to use parallel Streams?- First of all, do NOT and should NOT use parallel Streams if you don't have any problem with sequential Streams, because using parallel Streams has extra cost.
- Consider using parallel Streams only when N(the number of elements) * Q(cost per element of F, the per-element function (usually a lambda)) is big enough(e.g. IO involved. Network: DB/web service request..., Reading/Writing file...).
- It's easy to test out the differences of performance by sequential Streams and parallel Streams with Profiler:
Profiler.run(1, 1, 3, "sequential", () -> Stream.of(list).operation(F)...).printResult(); Profiler.run(1, 1, 3, "parallel", () -> Stream.of(list).parallel().operation(F)...).printResult();
public void test_perf() { final String[] strs = new String[10_000]; N.fill(strs, N.uuid()); final int m = 10; final Function<String, Long> mapper = str -> { long result = 0; for (int i = 0; i < m; i++) { result += N.sum(str.toCharArray()) + 1; } return result; }; final MutableLong sum = MutableLong.of(0); for (int i = 0, len = strs.length; i < len; i++) { sum.add(mapper.apply(strs[i])); } final int threadNum = 1, loopNum = 100, roundNum = 3; Profiler.run(threadNum, loopNum, roundNum, "For Loop", () -> { long result = 0; for (int i = 0, len = strs.length; i < len; i++) { result += mapper.apply(strs[i]); } assertEquals(sum.longValue(), result); }).printResult(); Profiler.run(threadNum, loopNum, roundNum, "JDK Sequential", () -> assertEquals(sum.longValue(), java.util.stream.Stream.of(strs).map(mapper).mapToLong(e -> e).sum())).printResult(); Profiler.run(threadNum, loopNum, roundNum, "JDK Parallel", () -> assertEquals(sum.longValue(), java.util.stream.Stream.of(strs).parallel().map(mapper).mapToLong(e -> e).sum())).printResult(); Profiler.run(threadNum, loopNum, roundNum, "Abcus Sequential", () -> assertEquals(sum.longValue(), Stream.of(strs).map(mapper).mapToLong(e -> e).sum())) .printResult(); Profiler.run(threadNum, loopNum, roundNum, "Abcus Parallel", () -> assertEquals(sum.longValue(), Stream.of(strs).parallel().map(mapper).mapToLong(e -> e).sum())).printResult(); Profiler.run(threadNum, loopNum, roundNum, "Abcus Parallel by chunck", () -> assertEquals(sum.longValue(), Stream.of(strs).splitToList(100).parallel().map(it -> N.sumLong(it, e -> mapper.apply(e))).mapToLong(e -> e).sum())).printResult(); }
mapper
) is calculated by: value of 'For loop' / N(10_000).m = 1 m = 10 m = 50 m = 100 m = 500 m = 1000 Q 0.00002 0.0002 0.001 0.002 0.01 0.02 For Loop 0.23 2.3 11 22 110 219 JDK Sequential 0.28 2.3 11 22 114 212 JDK Parallel 0.22 1.3 6 12 66 122 Abcus Sequential 0.3 2 11 22 112 212 Abcus Parallel 11 11 11 16 77 128 - Again, do NOT and should NOT use parallel Streams if you don't have any performance problem with sequential Streams, because using parallel Streams has extra cost.
- Again, consider using parallel Streams only when N(the number of elements) * Q(cost per element of F, the per-element function (usually a lambda)) is big enough.
- The implementation of parallel Streams in Abacus is more than 10 times, slower than parallel Streams in JDK when Q is tiny(here is less than 0.0002 milliseconds by the test):
- The implementation of parallel Streams in JDK 8 still can beat the sequential/for loop when Q is tiny(Here is 0.00002 milliseconds by the test). That's amazing, considering the extra cost brought by parallel computation. It's well done.
- The implementation of parallel Streams in Abacus is pretty simple and straight forward. The extra cost(starting threads/synchronization/queue...) brought by parallel Streams in Abacus is too bigger to tiny Q(Here is less than 0.001 milliseconds by the test). But it starts to be faster than sequential Streams when Q is big enough(Here is 0.001 milliseconds by the test) and starts to catch the parallel Streams in JDK when Q is bigger(Here is 0.01 milliseconds by the test).
- Consider using the parallel Streams in Abacus when Q is big enough, specially when IO involved in F. Because one IO operation(e.g. DB/web service request..., Reading/Writing file...) usually takes 1 to 1000 milliseconds, or even longer. By the parallel Streams APIs in Abacus, it's very simple to specify max thread numbers. Sometimes, it's much faster to execute IO/Network requests with a bit more threads. It's fair to say that the parallel Streams in Abacus is high efficient, may same as or faster than the parallel Streams in JDK when Q is big enough, except F is heavy cpu-used operation. Most of the times, the Q is big enough to consider using parallel Stream is because IO/Network is involved in F.
- JDK 7 is supported by the Streams in Abacus. It's perfect to work with retrolambda on Android
- All primitive types are supported by Stream APIs in Abacus except boolean
A bit more about Lambdas/Stream APIs, you may heard that Lambdas/Stream APIs is 5 time slower than imperative programming. It's true when Q and F is VERY, VERY tiny, likef = (int a, int b) -> a + b;
. But if we look into the samples in the article and think about it: it just takes less than 1 milliseconds to get the max value in 100k numbers. There is potential performance issue only if the "get the max value in 100K numbers" call many, many times in your API or single request. Otherwise, the difference between 0.1 milliseconds to 0.5 milliseconds can be totally ignored. Usually we meet performance issue only if Q and F is big enough. However, the performance of Lambdas/Streams APIs is closed to for loop when Q and F is big enough. No matter in which scenario, We don't need and should not concern the performance of Lambdas/Stream APIs.
Although it's is parallel Streams, it doesn't means all the methods are executed in parallel. Because the sequential way is as fast, or even faster than the parallel way for some methods, or is pretty difficult, if not possible, to implement the method by parallel approach. Here are the methods which are executed sequentially even in parallel Streams.
splitXXX/splitAt/splitBy/slidingXXX/collapse, distinct, reverse, rotate, shuffle, indexed, cached, top, kthLargest, count, toArray, toList, toList, toSet, toMultiset, toLongMultiset, intersection(Collection c), difference(Collection c), symmetricDifference(Collection c), forEach(identity, accumulator, predicate), findFirstOrLast, findFirstAndLast- Specified by:
parallel
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Parameters:
maxThreadNum
-splitor
-- Returns:
- See Also:
-
#maxThreadNumPerOperation()
-
parallel
Description copied from interface:BaseStream
// Replace above line of code with "sps" if only "f" need to be parallelized. And "p" is fast enough to be executed in sequential Stream. stream.sps(SP.create(maxThreadNum, executor), s -> s.map(f)).filter(p)...; // Or switch the stream back sequential stream if don't use "sps". stream.parallel(maxThreadNum, executor).map(f).sequential().filter(p)...;- Specified by:
parallel
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Parameters:
maxThreadNum
-executor
-- Returns:
- See Also:
-
parallel
Description copied from interface:BaseStream
stream.parallel(executor).map(f).filter(p)...; // Replace above line of code with "sps" if only "f" need to be parallelized. And "p" is fast enough to be executed in sequential Stream. stream.sps(SP.create(executor), s -> s.map(f)).filter(p)...; // Or switch the stream back sequential stream if don't use "sps". stream.parallel(executor).map(f).sequential().filter(p)...;
- Specified by:
parallel
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Parameters:
executor
-- Returns:
- See Also:
-
parallel
Description copied from interface:BaseStream
stream.parallel(maxThreadNum, splitor, executor).map(f).filter(p)...; // Replace above line of code with "sps" if only "f" need to be parallelized. And "p" is fast enough to be executed in sequential Stream. stream.sps(SP.create(maxThreadNum, splitor, executor), s -> s.map(f)).filter(p)...; // Or switch the stream back sequential stream if don't use "sps". stream.parallel(maxThreadNum, splitor, executor).map(f).sequential().filter(p)...;
- Specified by:
parallel
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Parameters:
maxThreadNum
-splitor
-executor
-- Returns:
- See Also:
-
parallel
Description copied from interface:BaseStream
stream.parallel(parallelSettings).map(f).filter(p)...; // Replace above line of code with "sps" if only "f" need to be parallelized. And "p" is fast enough to be executed in sequential Stream. stream.sps(SP.create(parallelSettings), s -> s.map(f)).filter(p)...; // Or switch the stream back sequential stream if don't use "sps". stream.parallel(parallelSettings).map(f).sequential().filter(p)...;
- Specified by:
parallel
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Parameters:
ps
-- Returns:
- See Also:
-
sps
Description copied from interface:BaseStream
Temporarily switch the stream to parallel stream for operationops
and then switch back to sequence stream.
stream().parallel().ops(map/filter/...).sequence()
- Specified by:
sps
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Type Parameters:
SS
-- Parameters:
ops
-- Returns:
-
sps
public <SS extends BaseStream> SS sps(int maxThreadNum, Function<? super LongStream, ? extends SS> ops) Description copied from interface:BaseStream
Temporarily switch the stream to parallel stream for operationops
and then switch back to sequence stream.
stream().parallel(maxThreadNum).ops(map/filter/...).sequence()
- Specified by:
sps
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Type Parameters:
SS
-- Parameters:
maxThreadNum
-ops
-- Returns:
-
psp
Description copied from interface:BaseStream
Temporarily switch the stream to sequence stream for operationops
and then switch back to parallel stream with samemaxThreadNum/splitor/asyncExecutor
.
stream().sequence().ops(map/filter/...).parallel(sameMaxThreadNum, sameSplitor, sameAsyncExecutor)
- Specified by:
psp
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Type Parameters:
SS
-- Parameters:
ops
-- Returns:
-
toArray
public long[] toArray()- Specified by:
toArray
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Returns:
-
close
public void close()Description copied from interface:BaseStream
It will be called by terminal operations in final.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceBaseStream<T,
A, P, C, PL, OT, IT, ITER extends Iterator<T>, S extends com.landawn.abacus.util.stream.StreamBase<T, A, P, C, PL, OT, IT, ITER, S>> - Specified by:
close
in interfaceCloseable
-
BaseStream.transform(Function)