public abstract class LongStream extends Object
BaseStream
,
Stream
Modifier and Type | Class and Description |
---|---|
static class |
LongStream.LongStreamEx |
BaseStream.Splitor
Modifier and Type | Method and Description |
---|---|
<SS extends BaseStream> |
__(Function<? super LongStream,SS> transfer) |
abstract <E extends Exception> |
allMatch(Throwables.LongPredicate<E> predicate) |
abstract <E extends Exception> |
anyMatch(Throwables.LongPredicate<E> predicate) |
abstract LongStream |
append(long... a) |
abstract LongStream |
appendIfEmpty(long... a) |
abstract DoubleStream |
asDoubleStream() |
abstract FloatStream |
asFloatStream() |
abstract u.OptionalDouble |
average() |
abstract Stream<Long> |
boxed() |
S |
carry(C action)
Same as
peek |
void |
close() |
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.
|
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 <R> R |
collect(Supplier<R> supplier,
ObjLongConsumer<? super R> accumulator) |
abstract <R> R |
collect(Supplier<R> supplier,
ObjLongConsumer<? super R> accumulator,
BiConsumer<R,R> combiner) |
static LongStream |
concat(Collection<? extends LongStream> c) |
static LongStream |
concat(long[]... a) |
static LongStream |
concat(LongIterator... a) |
static LongStream |
concat(LongStream... a) |
static LongStream |
empty() |
abstract <E extends Exception> |
findAny(Throwables.LongPredicate<E> predicate) |
abstract <E extends Exception> |
findFirst(Throwables.LongPredicate<E> predicate) |
abstract <E extends Exception,E2 extends Exception> |
findFirstOrLast(Throwables.LongPredicate<E> predicateForFirst,
Throwables.LongPredicate<E> predicateForLast) |
abstract <E extends Exception> |
findLast(Throwables.LongPredicate<E> predicate)
Consider using:
stream.reversed().findFirst(predicate) for better performance if possible. |
abstract LongStream |
flatMap(LongFunction<? extends LongStream> mapper) |
abstract <T> Stream<T> |
flatMappToObj(LongFunction<T[]> 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 Stream<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<long[]> mapper) |
abstract <T> Stream<T> |
flattMapToObj(LongFunction<? extends Collection<T>> mapper) |
abstract <E extends Exception> |
forEach(Throwables.LongConsumer<E> action) |
abstract <E extends Exception> |
forEachIndexed(Throwables.IndexedLongConsumer<E> action) |
static LongStream |
generate(LongSupplier s) |
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 |
interval(long delay,
long interval,
TimeUnit unit)
Generates the long value by the specified period: [0, 1, 2, 3...]
|
boolean |
isParallel() |
static LongStream |
iterate(BooleanSupplier hasNext,
LongSupplier next) |
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) |
LongIterator |
iterator()
Remember to close this Stream after the iteration is done, if needed.
|
String |
join(CharSequence delimiter) |
abstract u.OptionalLong |
kthLargest(int k) |
abstract LongStream |
map(LongUnaryOperator mapper) |
abstract DoubleStream |
mapToDouble(LongToDoubleFunction mapper) |
abstract FloatStream |
mapToFloat(LongToFloatFunction mapper) |
abstract IntStream |
mapToInt(LongToIntFunction mapper) |
abstract <U> Stream<U> |
mapToObj(LongFunction<? extends U> mapper) |
abstract u.OptionalLong |
max() |
static LongStream |
merge(Collection<? extends LongStream> c,
LongBiFunction<MergeResult> nextSelector) |
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) |
abstract LongStream |
merge(LongStream b,
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) |
abstract u.OptionalLong |
min() |
abstract <E extends Exception> |
noneMatch(Throwables.LongPredicate<E> predicate) |
static LongStream |
of(Collection<Long> c) |
static LongStream |
of(long... a) |
static LongStream |
of(Long[] a) |
static LongStream |
of(long[] a,
int startIndex,
int endIndex) |
static LongStream |
of(Long[] a,
int startIndex,
int endIndex) |
static LongStream |
of(LongIterator iterator) |
static LongStream |
of(LongStream stream) |
static LongStream |
of(Supplier<LongList> supplier)
Lazy evaluation.
|
S |
parallel() |
S |
parallel(BaseStream.Splitor splitor) |
S |
parallel(Executor executor) |
S |
parallel(int maxThreadNum) |
S |
parallel(int maxThreadNum,
BaseStream.Splitor splitor)
Returns an equivalent stream that is parallel.
|
S |
parallel(int maxThreadNum,
BaseStream.Splitor splitor,
Executor executor) |
S |
parallel(int maxThreadNum,
Executor executor) |
static LongStream |
parallelMerge(Collection<? extends LongStream> c,
LongBiFunction<MergeResult> nextSelector) |
static LongStream |
parallelMerge(Collection<? extends LongStream> c,
LongBiFunction<MergeResult> nextSelector,
int maxThreadNum) |
abstract LongStream |
prepend(long... a) |
void |
println() |
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<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. |
abstract u.OptionalLong |
reduce(LongBinaryOperator op) |
abstract long |
reduce(long identity,
LongBinaryOperator op) |
static LongStream |
repeat(long element,
long n) |
abstract LongStream |
scan(LongBinaryOperator accumulator)
Returns a
Stream produced by iterative application of a accumulation function
to an initial element init and next element of the current stream. |
abstract LongStream |
scan(long init,
LongBinaryOperator accumulator)
Returns a
Stream produced by iterative application of a accumulation function
to an initial element init and next element of the current stream. |
abstract LongStream |
scan(long init,
LongBinaryOperator accumulator,
boolean initIncluded) |
S |
sequential() |
S |
shuffled()
This method only run sequentially, even in parallel stream and all elements will be loaded to memory. |
LongStream |
skipUntil(LongPredicate predicate) |
Stream<S> |
sliding(int windowSize) |
Stream<PL> |
slidingToList(int windowSize) |
abstract long |
sum() |
abstract LongSummaryStatistics |
summarize() |
abstract Pair<LongSummaryStatistics,u.Optional<Map<Percentage,Long>>> |
summarizeAndPercentiles() |
ImmutableList<T> |
toImmutableList() |
ImmutableSet<T> |
toImmutableSet() |
abstract LongStream |
toJdkStream() |
abstract LongList |
toLongList() |
abstract <K,A,D> Map<K,D> |
toMap(LongFunction<? extends K> keyMapper,
Collector<Long,A,D> downstream) |
abstract <K,A,D,M extends Map<K,D>> |
toMap(LongFunction<? extends K> keyMapper,
Collector<Long,A,D> downstream,
Supplier<? extends M> mapFactory) |
abstract <K,V> Map<K,V> |
toMap(LongFunction<? extends K> keyMapper,
LongFunction<? extends V> valueMapper) |
abstract <K,V> Map<K,V> |
toMap(LongFunction<? extends K> keyMapper,
LongFunction<? extends V> valueMapper,
BinaryOperator<V> mergeFunction) |
abstract <K,V,M extends Map<K,V>> |
toMap(LongFunction<? extends K> keyMapper,
LongFunction<? extends V> valueMapper,
BinaryOperator<V> mergeFunction,
Supplier<? extends M> mapFactory) |
abstract <K,V,M extends Map<K,V>> |
toMap(LongFunction<? extends K> keyMapper,
LongFunction<? extends V> valueMapper,
Supplier<? extends M> mapFactory) |
abstract LongStream |
top(int n)
This method only run sequentially, even in parallel stream. |
abstract LongStream |
top(int n,
Comparator<? super Long> comparator)
This method only run sequentially, even in parallel stream. |
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.
|
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,
LongBinaryOperator zipFunction)
Zip together the "a" and "b" 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(LongIterator a,
LongIterator b,
LongBinaryOperator zipFunction)
Zip together the "a" and "b" iterators until one 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,
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,
LongBinaryOperator zipFunction)
Zip together the "a" and "b" streams 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.
|
abstract LongStream |
zipWith(LongStream b,
LongBinaryOperator zipFunction) |
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) |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
acceptIfNotEmpty, append, appendIfEmpty, applyIfNotEmpty, count, difference, distinct, dropWhile, dropWhile, filter, filter, first, indexed, intersection, join, last, limit, onClose, onlyOne, peek, percentiles, prepend, removeIf, removeIf, reversed, reverseSorted, rotated, shuffled, skip, skip, sliding, slidingToList, sorted, split, split, splitAt, splitBy, splitToList, splitToList, step, symmetricDifference, takeWhile, toArray, toCollection, toList, toLongMultiset, toLongMultiset, toMultiset, toMultiset, toSet
@ParallelSupported @IntermediateOp @Beta public LongStream skipUntil(LongPredicate predicate)
public abstract LongStream map(LongUnaryOperator mapper)
public abstract IntStream mapToInt(LongToIntFunction mapper)
public abstract FloatStream mapToFloat(LongToFloatFunction mapper)
public abstract DoubleStream mapToDouble(LongToDoubleFunction mapper)
public abstract <U> Stream<U> mapToObj(LongFunction<? extends U> mapper)
public abstract LongStream flatMap(LongFunction<? extends LongStream> mapper)
public abstract LongStream flattMap(LongFunction<long[]> mapper)
public abstract IntStream flatMapToInt(LongFunction<? extends IntStream> mapper)
public abstract FloatStream flatMapToFloat(LongFunction<? extends FloatStream> mapper)
public abstract DoubleStream flatMapToDouble(LongFunction<? extends DoubleStream> mapper)
public abstract <T> Stream<T> flatMapToObj(LongFunction<? extends Stream<T>> mapper)
public abstract <T> Stream<T> flattMapToObj(LongFunction<? extends Collection<T>> mapper)
public abstract <T> Stream<T> flatMappToObj(LongFunction<T[]> mapper)
@SequentialOnly public abstract LongStream rangeMap(LongBiPredicate sameRange, LongBinaryOperator mapper)
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.collapse(LongBiPredicate, LongBinaryOperator)
,
Stream.rangeMap(BiPredicate, BiFunction)
@SequentialOnly public abstract <T> Stream<T> rangeMapToObj(LongBiPredicate sameRange, LongBiFunction<T> mapper)
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.Stream.rangeMap(BiPredicate, BiFunction)
@SequentialOnly public abstract Stream<LongList> collapse(LongBiPredicate collapsible)
collapsible
- @SequentialOnly public abstract LongStream collapse(LongBiPredicate collapsible, LongBinaryOperator mergeFunction)
collapsible
- mergeFunction
- @SequentialOnly public abstract LongStream scan(LongBinaryOperator accumulator)
Stream
produced by iterative application of a accumulation function
to an initial element init
and next element of the current stream.
Produces a Stream
consisting of init
, 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]
accumulator
- the accumulation function@SequentialOnly public abstract LongStream scan(long init, LongBinaryOperator accumulator)
Stream
produced by iterative application of a accumulation function
to an initial element init
and next element of the current stream.
Produces a Stream
consisting of init
, 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]
init
- the initial value. it's only used once by accumulator
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@SequentialOnly public abstract LongStream scan(long init, LongBinaryOperator accumulator, boolean initIncluded)
init
- accumulator
- initIncluded
- public abstract LongStream prepend(long... a)
public abstract LongStream append(long... a)
public abstract LongStream appendIfEmpty(long... a)
@SequentialOnly public abstract LongStream top(int n)
n
- @SequentialOnly public abstract LongStream top(int n, Comparator<? super Long> comparator)
n
- public abstract LongList toLongList()
public abstract <K,V> Map<K,V> toMap(LongFunction<? extends K> keyMapper, LongFunction<? extends V> valueMapper)
keyMapper
- valueMapper
- Collectors.toMap(Function, Function)
public abstract <K,V,M extends Map<K,V>> M toMap(LongFunction<? extends K> keyMapper, LongFunction<? extends V> valueMapper, Supplier<? extends M> mapFactory)
keyMapper
- valueMapper
- mapFactory
- Collectors.toMap(Function, Function, Supplier)
public abstract <K,V> Map<K,V> toMap(LongFunction<? extends K> keyMapper, LongFunction<? extends V> valueMapper, BinaryOperator<V> mergeFunction)
keyMapper
- valueMapper
- mergeFunction
- Collectors.toMap(Function, Function, BinaryOperator)
public abstract <K,V,M extends Map<K,V>> M toMap(LongFunction<? extends K> keyMapper, LongFunction<? extends V> valueMapper, BinaryOperator<V> mergeFunction, Supplier<? extends M> mapFactory)
keyMapper
- valueMapper
- mergeFunction
- mapFactory
- Collectors.toMap(Function, Function, BinaryOperator, Supplier)
public abstract <K,A,D> Map<K,D> toMap(LongFunction<? extends K> keyMapper, Collector<Long,A,D> downstream)
keyMapper
- downstream
- Collectors.groupingBy(Function, Collector)
public abstract <K,A,D,M extends Map<K,D>> M toMap(LongFunction<? extends K> keyMapper, Collector<Long,A,D> downstream, Supplier<? extends M> mapFactory)
keyMapper
- downstream
- mapFactory
- Collectors.groupingBy(Function, Collector, Supplier)
public abstract long reduce(long identity, LongBinaryOperator op)
public abstract u.OptionalLong reduce(LongBinaryOperator op)
public abstract <R> R collect(Supplier<R> supplier, ObjLongConsumer<? super R> accumulator, BiConsumer<R,R> combiner)
public abstract <R> R collect(Supplier<R> supplier, ObjLongConsumer<? super R> accumulator)
supplier
- accumulator
- @ParallelSupported @TerminalOp public abstract <E extends Exception> void forEach(Throwables.LongConsumer<E> action) throws E extends Exception
E extends Exception
@ParallelSupported @TerminalOp public abstract <E extends Exception> void forEachIndexed(Throwables.IndexedLongConsumer<E> action) throws E extends Exception
E extends Exception
public abstract <E extends Exception> boolean anyMatch(Throwables.LongPredicate<E> predicate) throws E extends Exception
E extends Exception
public abstract <E extends Exception> boolean allMatch(Throwables.LongPredicate<E> predicate) throws E extends Exception
E extends Exception
public abstract <E extends Exception> boolean noneMatch(Throwables.LongPredicate<E> predicate) throws E extends Exception
E extends Exception
public abstract <E extends Exception> u.OptionalLong findFirst(Throwables.LongPredicate<E> predicate) throws E extends Exception
E extends Exception
public abstract <E extends Exception> u.OptionalLong findLast(Throwables.LongPredicate<E> predicate) throws E extends Exception
stream.reversed().findFirst(predicate)
for better performance if possible.E
- predicate
- E
E extends Exception
public abstract <E extends Exception,E2 extends Exception> u.OptionalLong findFirstOrLast(Throwables.LongPredicate<E> predicateForFirst, Throwables.LongPredicate<E> predicateForLast) throws E extends Exception, E2 extends Exception
E extends Exception
public abstract <E extends Exception> u.OptionalLong findAny(Throwables.LongPredicate<E> predicate) throws E extends Exception
E extends Exception
public abstract u.OptionalLong min()
public abstract u.OptionalLong max()
public abstract u.OptionalLong kthLargest(int k)
k
- public abstract long sum()
public abstract u.OptionalDouble average()
public abstract LongSummaryStatistics summarize()
public abstract Pair<LongSummaryStatistics,u.Optional<Map<Percentage,Long>>> summarizeAndPercentiles()
public abstract LongStream merge(LongStream b, LongBiFunction<MergeResult> nextSelector)
b
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public abstract LongStream zipWith(LongStream b, LongBinaryOperator zipFunction)
public abstract LongStream zipWith(LongStream b, LongStream c, LongTernaryOperator zipFunction)
public abstract LongStream zipWith(LongStream b, long valueForNoneA, long valueForNoneB, LongBinaryOperator zipFunction)
public abstract LongStream zipWith(LongStream b, LongStream c, long valueForNoneA, long valueForNoneB, long valueForNoneC, LongTernaryOperator zipFunction)
public abstract FloatStream asFloatStream()
public abstract DoubleStream asDoubleStream()
public abstract LongStream toJdkStream()
@SequentialOnly public LongIterator iterator()
@SequentialOnly @IntermediateOp @Beta public <SS extends BaseStream> SS __(Function<? super LongStream,SS> transfer)
public static LongStream empty()
@SafeVarargs public static LongStream of(long... a)
public static LongStream of(long[] a, int startIndex, int endIndex)
public static LongStream of(Long[] a)
public static LongStream of(Long[] a, int startIndex, int endIndex)
public static LongStream of(Collection<Long> c)
public static LongStream of(LongIterator iterator)
public static LongStream of(Supplier<LongList> supplier)
supplier
- public static LongStream of(LongStream stream)
public static LongStream flatten(long[][] a)
public static LongStream flatten(long[][] a, boolean vertically)
public static LongStream flatten(long[][] a, long valueForNone, boolean vertically)
public static LongStream flatten(long[][][] a)
public static LongStream range(long startInclusive, long endExclusive)
public static LongStream range(long startInclusive, long endExclusive, long by)
public static LongStream rangeClosed(long startInclusive, long endInclusive)
public static LongStream rangeClosed(long startInclusive, long endInclusive, long by)
public static LongStream repeat(long element, long n)
public static LongStream random()
public static LongStream iterate(BooleanSupplier hasNext, LongSupplier next)
public static LongStream iterate(long init, BooleanSupplier hasNext, LongUnaryOperator f)
public static LongStream iterate(long init, LongPredicate hasNext, LongUnaryOperator f)
init
- hasNext
- test if has next by hasNext.test(init) for first time and hasNext.test(f.apply(previous)) for remaining.f
- public static LongStream iterate(long init, LongUnaryOperator f)
public static LongStream generate(LongSupplier s)
public static LongStream interval(long intervalInMillis)
intervalInMillis
- public static LongStream interval(long delayInMillis, long intervalInMillis)
delayInMillis
- intervalInMillis
- public static LongStream interval(long delay, long interval, TimeUnit unit)
delay
- interval
- unit
- @SafeVarargs public static LongStream concat(long[]... a)
@SafeVarargs public static LongStream concat(LongIterator... a)
@SafeVarargs public static LongStream concat(LongStream... a)
public static LongStream concat(Collection<? extends LongStream> c)
public static LongStream zip(long[] a, long[] b, LongBinaryOperator zipFunction)
a
- b
- public static LongStream zip(long[] a, long[] b, long[] c, LongTernaryOperator zipFunction)
a
- b
- c
- public static LongStream zip(LongIterator a, LongIterator b, LongBinaryOperator zipFunction)
a
- b
- public static LongStream zip(LongIterator a, LongIterator b, LongIterator c, LongTernaryOperator zipFunction)
a
- b
- public static LongStream zip(LongStream a, LongStream b, LongBinaryOperator zipFunction)
a
- b
- public static LongStream zip(LongStream a, LongStream b, LongStream c, LongTernaryOperator zipFunction)
a
- b
- public static LongStream zip(Collection<? extends LongStream> c, LongNFunction<Long> zipFunction)
c
- zipFunction
- public static LongStream zip(long[] a, long[] b, long valueForNoneA, long valueForNoneB, LongBinaryOperator zipFunction)
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
- public static LongStream zip(long[] a, long[] b, long[] c, long valueForNoneA, long valueForNoneB, long valueForNoneC, LongTernaryOperator zipFunction)
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
- public static LongStream zip(LongIterator a, LongIterator b, long valueForNoneA, long valueForNoneB, LongBinaryOperator zipFunction)
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
- public static LongStream zip(LongIterator a, LongIterator b, LongIterator c, long valueForNoneA, long valueForNoneB, long valueForNoneC, LongTernaryOperator zipFunction)
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
- public static LongStream zip(LongStream a, LongStream b, long valueForNoneA, long valueForNoneB, LongBinaryOperator zipFunction)
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
- public static LongStream zip(LongStream a, LongStream b, LongStream c, long valueForNoneA, long valueForNoneB, long valueForNoneC, LongTernaryOperator zipFunction)
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
- public static LongStream zip(Collection<? extends LongStream> c, long[] valuesForNone, LongNFunction<Long> zipFunction)
c
- valuesForNone
- value to fill for any iterator runs out of values.zipFunction
- public static LongStream merge(long[] a, long[] b, LongBiFunction<MergeResult> nextSelector)
a
- b
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static LongStream merge(long[] a, long[] b, long[] c, LongBiFunction<MergeResult> nextSelector)
a
- b
- c
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static LongStream merge(LongIterator a, LongIterator b, LongBiFunction<MergeResult> nextSelector)
a
- b
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static LongStream merge(LongIterator a, LongIterator b, LongIterator c, LongBiFunction<MergeResult> nextSelector)
a
- b
- c
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static LongStream merge(LongStream a, LongStream b, LongBiFunction<MergeResult> nextSelector)
a
- b
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static LongStream merge(LongStream a, LongStream b, LongStream c, LongBiFunction<MergeResult> nextSelector)
a
- b
- c
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static LongStream merge(Collection<? extends LongStream> c, LongBiFunction<MergeResult> nextSelector)
c
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static LongStream parallelMerge(Collection<? extends LongStream> c, LongBiFunction<MergeResult> nextSelector)
c
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static LongStream parallelMerge(Collection<? extends LongStream> c, LongBiFunction<MergeResult> nextSelector, int maxThreadNum)
c
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.maxThreadNum
- public S carry(C action)
BaseStream
peek
carry
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
BaseStream.peek(Object)
public Stream<S> sliding(int windowSize)
sliding
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
BaseStream.sliding(int, int)
public Stream<PL> slidingToList(int windowSize)
slidingToList
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
BaseStream.sliding(int, int)
public S shuffled()
BaseStream
shuffled
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public ImmutableList<T> toImmutableList()
toImmutableList
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public ImmutableSet<T> toImmutableSet()
toImmutableSet
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public String join(CharSequence delimiter)
join
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public void println()
println
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public boolean isParallel()
isParallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public S sequential()
sequential
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public S parallel()
parallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public S parallel(int maxThreadNum)
parallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public S parallel(BaseStream.Splitor splitor)
parallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public S parallel(int maxThreadNum, BaseStream.Splitor splitor)
BaseStream
maxThreadNum
and splitor
as the specified ones.
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();
Here is a sample performance test with computer: CPU Intel i7-3520M 4-cores 2.9 GHz, JDK 1.8.0_101, Windows 7:
public void test_perf() {
final String[] strs = new String[10_000];
N.fill(strs, N.uuid());
final int m = 1;
final Function mapper = str -> {
long result = 0;
for (int i = 0; i < m; i++) {
result += 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().longValue())).printResult();
Profiler.run(threadNum, loopNum, roundNum, "Abcus Parallel",
() -> assertEquals(sum.longValue(), Stream.of(strs).parallel().map(mapper).mapToLong(e -> e).sum().longValue())).printResult();
}
And test result: Unit is milliseconds. N(the number of elements) is 10_000, Q(cost per element of F, the per-element function (usually a lambda), here is 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 |
f = (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.
parallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
maxThreadNum
- Default value is the number of cpu-cores. Steps/operations will be executed sequentially if maxThreadNum
is 1.splitor
- The target array is split by ranges for multiple threads if splitor is splitor.ARRAY
and target stream composed by array. It looks like:
for (int i = 0; i < maxThreadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Runnable() {
public void run() {
int cursor = fromIndex + sliceIndex * sliceSize;
final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
while (cursor < to) {
action.accept(elements[cursor++]);
}
}
}));
}
Otherwise, each thread will get the elements from the target array/iterator in the stream one by one with the target array/iterator synchronized. It looks like:
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Runnable() {
public void run() {
T next = null;
while (true) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
break;
}
}
action.accept(next);
}
}
}));
}
Using splitor.ARRAY
only when F (the per-element function (usually a lambda)) is very tiny and the cost of synchronization on the target array/iterator is too big to it.
For the F involving IO or taking 'long' to complete, choose splitor.ITERATOR
. Default value is splitor.ITERATOR
.MergeResult
,
com.landawn.abacus.util.Profiler#run(int, int, int, String, Runnable)
,
Understanding Parallel Stream Performance in Java SE 8,
When to use parallel Streamspublic S parallel(int maxThreadNum, BaseStream.Splitor splitor, Executor executor)
parallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
executor
- should be able to execute maxThreadNum
* following up operations
in parallel.public S parallel(int maxThreadNum, Executor executor)
parallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
executor
- should be able to execute maxThreadNum
* following up operations
in parallel.public S parallel(Executor executor)
parallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
executor
- should be able to execute maxThreadNum
* following up operations
in parallel.public void close()
close
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
close
in interface AutoCloseable
Copyright © 2020. All rights reserved.