public abstract class IntStream extends Object
BaseStream
,
Stream
Modifier and Type | Class and Description |
---|---|
static class |
IntStream.IntStreamEx |
BaseStream.Splitor
Modifier and Type | Method and Description |
---|---|
<SS extends BaseStream> |
__(Function<? super IntStream,SS> transfer) |
abstract <E extends Exception> |
allMatch(Throwables.IntPredicate<E> predicate) |
abstract <E extends Exception> |
anyMatch(Throwables.IntPredicate<E> predicate) |
abstract IntStream |
append(int... a) |
abstract IntStream |
appendIfEmpty(int... a) |
abstract DoubleStream |
asDoubleStream() |
abstract FloatStream |
asFloatStream() |
abstract LongStream |
asLongStream() |
abstract u.OptionalDouble |
average() |
abstract Stream<Integer> |
boxed() |
S |
carry(C action)
Same as
peek |
void |
close() |
abstract Stream<IntList> |
collapse(IntBiPredicate collapsible)
Merge series of adjacent elements which satisfy the given predicate using
the merger function and return a new stream.
|
abstract IntStream |
collapse(IntBiPredicate collapsible,
IntBinaryOperator 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,
ObjIntConsumer<? super R> accumulator) |
abstract <R> R |
collect(Supplier<R> supplier,
ObjIntConsumer<? super R> accumulator,
BiConsumer<R,R> combiner) |
static IntStream |
concat(Collection<? extends IntStream> c) |
static IntStream |
concat(int[]... a) |
static IntStream |
concat(IntIterator... a) |
static IntStream |
concat(IntStream... a) |
static IntStream |
empty() |
abstract <E extends Exception> |
findAny(Throwables.IntPredicate<E> predicate) |
abstract <E extends Exception> |
findFirst(Throwables.IntPredicate<E> predicate) |
abstract <E extends Exception,E2 extends Exception> |
findFirstOrLast(Throwables.IntPredicate<E> predicateForFirst,
Throwables.IntPredicate<E> predicateForLast) |
abstract <E extends Exception> |
findLast(Throwables.IntPredicate<E> predicate)
Consider using:
stream.reversed().findFirst(predicate) for better performance if possible. |
abstract IntStream |
flatMap(IntFunction<? extends IntStream> mapper) |
abstract <T> Stream<T> |
flatMappToObj(IntFunction<T[]> mapper) |
abstract ByteStream |
flatMapToByte(IntFunction<? extends ByteStream> mapper) |
abstract CharStream |
flatMapToChar(IntFunction<? extends CharStream> mapper) |
abstract DoubleStream |
flatMapToDouble(IntFunction<? extends DoubleStream> mapper) |
abstract FloatStream |
flatMapToFloat(IntFunction<? extends FloatStream> mapper) |
abstract LongStream |
flatMapToLong(IntFunction<? extends LongStream> mapper) |
abstract <T> Stream<T> |
flatMapToObj(IntFunction<? extends Stream<T>> mapper) |
abstract ShortStream |
flatMapToShort(IntFunction<? extends ShortStream> mapper) |
static IntStream |
flatten(int[][] a) |
static IntStream |
flatten(int[][][] a) |
static IntStream |
flatten(int[][] a,
boolean vertically) |
static IntStream |
flatten(int[][] a,
int valueForNone,
boolean vertically) |
abstract IntStream |
flattMap(IntFunction<int[]> mapper) |
abstract <T> Stream<T> |
flattMapToObj(IntFunction<? extends Collection<T>> mapper) |
abstract <E extends Exception> |
forEach(Throwables.IntConsumer<E> action) |
abstract <E extends Exception> |
forEachIndexed(Throwables.IndexedIntConsumer<E> action) |
static IntStream |
from(byte... a) |
static IntStream |
from(byte[] a,
int fromIndex,
int toIndex) |
static IntStream |
from(char... a) |
static IntStream |
from(char[] a,
int fromIndex,
int toIndex) |
static IntStream |
from(short... a) |
static IntStream |
from(short[] a,
int fromIndex,
int toIndex) |
static IntStream |
generate(IntSupplier s) |
boolean |
isParallel() |
static IntStream |
iterate(BooleanSupplier hasNext,
IntSupplier next) |
static IntStream |
iterate(int init,
BooleanSupplier hasNext,
IntUnaryOperator f) |
static IntStream |
iterate(int init,
IntPredicate hasNext,
IntUnaryOperator f) |
static IntStream |
iterate(int init,
IntUnaryOperator f) |
IntIterator |
iterator()
Remember to close this Stream after the iteration is done, if needed.
|
String |
join(CharSequence delimiter) |
abstract u.OptionalInt |
kthLargest(int k) |
abstract IntStream |
map(IntUnaryOperator mapper) |
abstract ByteStream |
mapToByte(IntToByteFunction mapper) |
abstract CharStream |
mapToChar(IntToCharFunction mapper) |
abstract DoubleStream |
mapToDouble(IntToDoubleFunction mapper) |
abstract FloatStream |
mapToFloat(IntToFloatFunction mapper) |
abstract LongStream |
mapToLong(IntToLongFunction mapper) |
abstract <U> Stream<U> |
mapToObj(IntFunction<? extends U> mapper) |
abstract ShortStream |
mapToShort(IntToShortFunction mapper) |
abstract u.OptionalInt |
max() |
static IntStream |
merge(Collection<? extends IntStream> c,
IntBiFunction<MergeResult> nextSelector) |
static IntStream |
merge(int[] a,
int[] b,
int[] c,
IntBiFunction<MergeResult> nextSelector) |
static IntStream |
merge(int[] a,
int[] b,
IntBiFunction<MergeResult> nextSelector) |
static IntStream |
merge(IntIterator a,
IntIterator b,
IntBiFunction<MergeResult> nextSelector) |
static IntStream |
merge(IntIterator a,
IntIterator b,
IntIterator c,
IntBiFunction<MergeResult> nextSelector) |
abstract IntStream |
merge(IntStream b,
IntBiFunction<MergeResult> nextSelector) |
static IntStream |
merge(IntStream a,
IntStream b,
IntBiFunction<MergeResult> nextSelector) |
static IntStream |
merge(IntStream a,
IntStream b,
IntStream c,
IntBiFunction<MergeResult> nextSelector) |
abstract u.OptionalInt |
min() |
abstract <E extends Exception> |
noneMatch(Throwables.IntPredicate<E> predicate) |
static IntStream |
of(Collection<Integer> c) |
static IntStream |
of(int... a) |
static IntStream |
of(int[] a,
int startIndex,
int endIndex) |
static IntStream |
of(Integer[] a) |
static IntStream |
of(Integer[] a,
int startIndex,
int endIndex) |
static IntStream |
of(IntIterator iterator) |
static IntStream |
of(IntStream stream) |
static IntStream |
of(Supplier<IntList> supplier)
Lazy evaluation.
|
static IntStream |
ofCodePoints(CharSequence str) |
static <AC> IntStream |
ofIndices(AC source,
BiFunction<? super AC,Integer,Integer> indexFunc) |
static <AC> IntStream |
ofIndices(AC source,
int fromIndex,
BiFunction<? super AC,Integer,Integer> indexFunc) |
static <AC> IntStream |
ofIndices(AC source,
int fromIndex,
int increment,
BiFunction<? super AC,Integer,Integer> indexFunc)
// Forwards:
int[] a = {1, 2, 3, 2, 5, 1};
IntStream.ofIndices(a, N::indexOf).println(); // [0, 5]
IntStream.ofIndices(a, 1, N::indexOf).println(); // [5]
// Backwards
IntStream.ofIndices(a, 5, -1, N::lastIndexOf).println(); // [5, 0]
IntStream.ofIndices(a, 4, -1, N::lastIndexOf).println(); // [0]
// OR
// Forwards:
int[] source = { 1, 2, 3, 1, 2, 1 };
int[] targetSubArray = { 1, 2 };
IntStream.ofIndices(source, (a, fromIndex) -> Index.ofSubArray(a, fromIndex, targetSubArray, 0, targetSubArray.length).orElse(-1)).println(); // [0, 3]
// Backwards
IntStream.ofIndices(source, 5, -2, (a, fromIndex) -> Index.ofSubArray(a, fromIndex, targetSubArray, 0, targetSubArray.length).orElse(-1))
.println(); // [3, 0]
|
static <AC> IntStream |
ofIndices(AC source,
int fromIndex,
int increment,
int sourceLen,
BiFunction<? super AC,Integer,Integer> indexFunc) |
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 IntStream |
parallelMerge(Collection<? extends IntStream> c,
IntBiFunction<MergeResult> nextSelector) |
static IntStream |
parallelMerge(Collection<? extends IntStream> c,
IntBiFunction<MergeResult> nextSelector,
int maxThreadNum) |
abstract IntStream |
prepend(int... a) |
void |
println() |
static IntStream |
random() |
static IntStream |
random(int startInclusive,
int endExclusive) |
static IntStream |
range(int startInclusive,
int endExclusive) |
static IntStream |
range(int startInclusive,
int endExclusive,
int by) |
static IntStream |
rangeClosed(int startInclusive,
int endInclusive) |
static IntStream |
rangeClosed(int startInclusive,
int endInclusive,
int by) |
abstract IntStream |
rangeMap(IntBiPredicate sameRange,
IntBinaryOperator 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(IntBiPredicate sameRange,
IntBiFunction<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.OptionalInt |
reduce(IntBinaryOperator op) |
abstract int |
reduce(int identity,
IntBinaryOperator op) |
static IntStream |
repeat(int element,
long n) |
abstract IntStream |
scan(IntBinaryOperator 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 IntStream |
scan(int init,
IntBinaryOperator 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 IntStream |
scan(int init,
IntBinaryOperator accumulator,
boolean initIncluded) |
S |
sequential() |
S |
shuffled()
This method only run sequentially, even in parallel stream and all elements will be loaded to memory. |
IntStream |
skipUntil(IntPredicate predicate) |
Stream<S> |
sliding(int windowSize) |
Stream<PL> |
slidingToList(int windowSize) |
abstract long |
sum() |
abstract IntSummaryStatistics |
summarize() |
abstract Pair<IntSummaryStatistics,u.Optional<Map<Percentage,Integer>>> |
summarizeAndPercentiles() |
ImmutableList<T> |
toImmutableList() |
ImmutableSet<T> |
toImmutableSet() |
abstract IntList |
toIntList() |
abstract IntStream |
toJdkStream() |
abstract <K,A,D> Map<K,D> |
toMap(IntFunction<? extends K> keyMapper,
Collector<Integer,A,D> downstream) |
abstract <K,A,D,M extends Map<K,D>> |
toMap(IntFunction<? extends K> keyMapper,
Collector<Integer,A,D> downstream,
Supplier<? extends M> mapFactory) |
abstract <K,V> Map<K,V> |
toMap(IntFunction<? extends K> keyMapper,
IntFunction<? extends V> valueMapper) |
abstract <K,V> Map<K,V> |
toMap(IntFunction<? extends K> keyMapper,
IntFunction<? extends V> valueMapper,
BinaryOperator<V> mergeFunction) |
abstract <K,V,M extends Map<K,V>> |
toMap(IntFunction<? extends K> keyMapper,
IntFunction<? extends V> valueMapper,
BinaryOperator<V> mergeFunction,
Supplier<? extends M> mapFactory) |
abstract <K,V,M extends Map<K,V>> |
toMap(IntFunction<? extends K> keyMapper,
IntFunction<? extends V> valueMapper,
Supplier<? extends M> mapFactory) |
abstract IntStream |
top(int n)
This method only run sequentially, even in parallel stream. |
abstract IntStream |
top(int n,
Comparator<? super Integer> comparator)
This method only run sequentially, even in parallel stream. |
static IntStream |
zip(Collection<? extends IntStream> c,
int[] valuesForNone,
IntNFunction<Integer> zipFunction)
Zip together the iterators until all of them runs out of values.
|
static IntStream |
zip(Collection<? extends IntStream> c,
IntNFunction<Integer> zipFunction)
Zip together the iterators until one of them runs out of values.
|
static IntStream |
zip(int[] a,
int[] b,
int[] c,
int valueForNoneA,
int valueForNoneB,
int valueForNoneC,
IntTernaryOperator zipFunction)
Zip together the "a", "b" and "c" iterators until all of them runs out of values.
|
static IntStream |
zip(int[] a,
int[] b,
int[] c,
IntTernaryOperator zipFunction)
Zip together the "a", "b" and "c" arrays until one of them runs out of values.
|
static IntStream |
zip(int[] a,
int[] b,
IntBinaryOperator zipFunction)
Zip together the "a" and "b" arrays until one of them runs out of values.
|
static IntStream |
zip(int[] a,
int[] b,
int valueForNoneA,
int valueForNoneB,
IntBinaryOperator zipFunction)
Zip together the "a" and "b" iterators until all of them runs out of values.
|
static IntStream |
zip(IntIterator a,
IntIterator b,
IntBinaryOperator zipFunction)
Zip together the "a" and "b" iterators until one of them runs out of values.
|
static IntStream |
zip(IntIterator a,
IntIterator b,
int valueForNoneA,
int valueForNoneB,
IntBinaryOperator zipFunction)
Zip together the "a" and "b" iterators until all of them runs out of values.
|
static IntStream |
zip(IntIterator a,
IntIterator b,
IntIterator c,
int valueForNoneA,
int valueForNoneB,
int valueForNoneC,
IntTernaryOperator zipFunction)
Zip together the "a", "b" and "c" iterators until all of them runs out of values.
|
static IntStream |
zip(IntIterator a,
IntIterator b,
IntIterator c,
IntTernaryOperator zipFunction)
Zip together the "a", "b" and "c" iterators until one of them runs out of values.
|
static IntStream |
zip(IntStream a,
IntStream b,
IntBinaryOperator zipFunction)
Zip together the "a" and "b" streams until one of them runs out of values.
|
static IntStream |
zip(IntStream a,
IntStream b,
int valueForNoneA,
int valueForNoneB,
IntBinaryOperator zipFunction)
Zip together the "a" and "b" iterators until all of them runs out of values.
|
static IntStream |
zip(IntStream a,
IntStream b,
IntStream c,
int valueForNoneA,
int valueForNoneB,
int valueForNoneC,
IntTernaryOperator zipFunction)
Zip together the "a", "b" and "c" iterators until all of them runs out of values.
|
static IntStream |
zip(IntStream a,
IntStream b,
IntStream c,
IntTernaryOperator zipFunction)
Zip together the "a", "b" and "c" streams until one of them runs out of values.
|
abstract IntStream |
zipWith(IntStream b,
IntBinaryOperator zipFunction) |
abstract IntStream |
zipWith(IntStream b,
int valueForNoneA,
int valueForNoneB,
IntBinaryOperator zipFunction) |
abstract IntStream |
zipWith(IntStream b,
IntStream c,
int valueForNoneA,
int valueForNoneB,
int valueForNoneC,
IntTernaryOperator zipFunction) |
abstract IntStream |
zipWith(IntStream b,
IntStream c,
IntTernaryOperator 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 IntStream skipUntil(IntPredicate predicate)
public abstract IntStream map(IntUnaryOperator mapper)
public abstract CharStream mapToChar(IntToCharFunction mapper)
public abstract ByteStream mapToByte(IntToByteFunction mapper)
public abstract ShortStream mapToShort(IntToShortFunction mapper)
public abstract LongStream mapToLong(IntToLongFunction mapper)
public abstract FloatStream mapToFloat(IntToFloatFunction mapper)
public abstract DoubleStream mapToDouble(IntToDoubleFunction mapper)
public abstract <U> Stream<U> mapToObj(IntFunction<? extends U> mapper)
public abstract IntStream flatMap(IntFunction<? extends IntStream> mapper)
public abstract IntStream flattMap(IntFunction<int[]> mapper)
public abstract CharStream flatMapToChar(IntFunction<? extends CharStream> mapper)
public abstract ByteStream flatMapToByte(IntFunction<? extends ByteStream> mapper)
public abstract ShortStream flatMapToShort(IntFunction<? extends ShortStream> mapper)
public abstract LongStream flatMapToLong(IntFunction<? extends LongStream> mapper)
public abstract FloatStream flatMapToFloat(IntFunction<? extends FloatStream> mapper)
public abstract DoubleStream flatMapToDouble(IntFunction<? extends DoubleStream> mapper)
public abstract <T> Stream<T> flatMapToObj(IntFunction<? extends Stream<T>> mapper)
public abstract <T> Stream<T> flattMapToObj(IntFunction<? extends Collection<T>> mapper)
public abstract <T> Stream<T> flatMappToObj(IntFunction<T[]> mapper)
@SequentialOnly public abstract IntStream rangeMap(IntBiPredicate sameRange, IntBinaryOperator 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(IntBiPredicate, IntBinaryOperator)
,
Stream.rangeMap(BiPredicate, BiFunction)
@SequentialOnly public abstract <T> Stream<T> rangeMapToObj(IntBiPredicate sameRange, IntBiFunction<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<IntList> collapse(IntBiPredicate collapsible)
collapsible
- @SequentialOnly public abstract IntStream collapse(IntBiPredicate collapsible, IntBinaryOperator mergeFunction)
collapsible
- mergeFunction
- @SequentialOnly public abstract IntStream scan(IntBinaryOperator 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 IntStream scan(int init, IntBinaryOperator 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 IntStream scan(int init, IntBinaryOperator accumulator, boolean initIncluded)
init
- accumulator
- initIncluded
- public abstract IntStream prepend(int... a)
public abstract IntStream append(int... a)
public abstract IntStream appendIfEmpty(int... a)
@SequentialOnly public abstract IntStream top(int n)
n
- @SequentialOnly public abstract IntStream top(int n, Comparator<? super Integer> comparator)
n
- comparator
- public abstract IntList toIntList()
public abstract <K,V> Map<K,V> toMap(IntFunction<? extends K> keyMapper, IntFunction<? extends V> valueMapper)
keyMapper
- valueMapper
- Collectors.toMap(Function, Function)
public abstract <K,V,M extends Map<K,V>> M toMap(IntFunction<? extends K> keyMapper, IntFunction<? extends V> valueMapper, Supplier<? extends M> mapFactory)
keyMapper
- valueMapper
- mapFactory
- Collectors.toMap(Function, Function, Supplier)
public abstract <K,V> Map<K,V> toMap(IntFunction<? extends K> keyMapper, IntFunction<? 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(IntFunction<? extends K> keyMapper, IntFunction<? 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(IntFunction<? extends K> keyMapper, Collector<Integer,A,D> downstream)
keyMapper
- downstream
- Collectors.groupingBy(Function, Collector)
public abstract <K,A,D,M extends Map<K,D>> M toMap(IntFunction<? extends K> keyMapper, Collector<Integer,A,D> downstream, Supplier<? extends M> mapFactory)
keyMapper
- downstream
- mapFactory
- Collectors.groupingBy(Function, Collector, Supplier)
public abstract int reduce(int identity, IntBinaryOperator op)
public abstract u.OptionalInt reduce(IntBinaryOperator op)
public abstract <R> R collect(Supplier<R> supplier, ObjIntConsumer<? super R> accumulator, BiConsumer<R,R> combiner)
public abstract <R> R collect(Supplier<R> supplier, ObjIntConsumer<? super R> accumulator)
supplier
- accumulator
- @ParallelSupported @TerminalOp public abstract <E extends Exception> void forEach(Throwables.IntConsumer<E> action) throws E extends Exception
E extends Exception
@ParallelSupported @TerminalOp public abstract <E extends Exception> void forEachIndexed(Throwables.IndexedIntConsumer<E> action) throws E extends Exception
E extends Exception
public abstract <E extends Exception> boolean anyMatch(Throwables.IntPredicate<E> predicate) throws E extends Exception
E extends Exception
public abstract <E extends Exception> boolean allMatch(Throwables.IntPredicate<E> predicate) throws E extends Exception
E extends Exception
public abstract <E extends Exception> boolean noneMatch(Throwables.IntPredicate<E> predicate) throws E extends Exception
E extends Exception
public abstract <E extends Exception> u.OptionalInt findFirst(Throwables.IntPredicate<E> predicate) throws E extends Exception
E extends Exception
public abstract <E extends Exception> u.OptionalInt findLast(Throwables.IntPredicate<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.OptionalInt findFirstOrLast(Throwables.IntPredicate<E> predicateForFirst, Throwables.IntPredicate<E> predicateForLast) throws E extends Exception, E2 extends Exception
E extends Exception
public abstract <E extends Exception> u.OptionalInt findAny(Throwables.IntPredicate<E> predicate) throws E extends Exception
E extends Exception
public abstract u.OptionalInt min()
public abstract u.OptionalInt max()
public abstract u.OptionalInt kthLargest(int k)
k
- public abstract long sum()
public abstract u.OptionalDouble average()
public abstract IntSummaryStatistics summarize()
public abstract Pair<IntSummaryStatistics,u.Optional<Map<Percentage,Integer>>> summarizeAndPercentiles()
public abstract IntStream merge(IntStream b, IntBiFunction<MergeResult> nextSelector)
b
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public abstract IntStream zipWith(IntStream b, IntBinaryOperator zipFunction)
public abstract IntStream zipWith(IntStream b, IntStream c, IntTernaryOperator zipFunction)
public abstract IntStream zipWith(IntStream b, int valueForNoneA, int valueForNoneB, IntBinaryOperator zipFunction)
public abstract IntStream zipWith(IntStream b, IntStream c, int valueForNoneA, int valueForNoneB, int valueForNoneC, IntTernaryOperator zipFunction)
public abstract LongStream asLongStream()
public abstract FloatStream asFloatStream()
public abstract DoubleStream asDoubleStream()
public abstract IntStream toJdkStream()
@SequentialOnly public IntIterator iterator()
@SequentialOnly @IntermediateOp @Beta public <SS extends BaseStream> SS __(Function<? super IntStream,SS> transfer)
public static IntStream empty()
@SafeVarargs public static IntStream of(int... a)
public static IntStream of(int[] a, int startIndex, int endIndex)
public static IntStream of(Collection<Integer> c)
public static IntStream of(IntIterator iterator)
public static IntStream of(Supplier<IntList> supplier)
supplier
- public static IntStream ofCodePoints(CharSequence str)
public static IntStream flatten(int[][] a)
public static IntStream flatten(int[][] a, boolean vertically)
public static IntStream flatten(int[][] a, int valueForNone, boolean vertically)
public static IntStream flatten(int[][][] a)
@SafeVarargs public static IntStream from(char... a)
public static IntStream from(char[] a, int fromIndex, int toIndex)
@SafeVarargs public static IntStream from(byte... a)
public static IntStream from(byte[] a, int fromIndex, int toIndex)
@SafeVarargs public static IntStream from(short... a)
public static IntStream from(short[] a, int fromIndex, int toIndex)
public static IntStream range(int startInclusive, int endExclusive)
public static IntStream range(int startInclusive, int endExclusive, int by)
public static IntStream rangeClosed(int startInclusive, int endInclusive)
public static IntStream rangeClosed(int startInclusive, int endInclusive, int by)
public static IntStream repeat(int element, long n)
public static IntStream random()
public static IntStream random(int startInclusive, int endExclusive)
public static IntStream iterate(BooleanSupplier hasNext, IntSupplier next)
public static IntStream iterate(int init, BooleanSupplier hasNext, IntUnaryOperator f)
public static IntStream iterate(int init, IntPredicate hasNext, IntUnaryOperator 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 IntStream iterate(int init, IntUnaryOperator f)
public static IntStream generate(IntSupplier s)
public static <AC> IntStream ofIndices(AC source, BiFunction<? super AC,Integer,Integer> indexFunc)
source
- indexFunc
- ofIndices(Object, int, int, BiFunction)
public static <AC> IntStream ofIndices(AC source, int fromIndex, BiFunction<? super AC,Integer,Integer> indexFunc)
source
- fromIndex
- indexFunc
- ofIndices(Object, int, int, BiFunction)
public static <AC> IntStream ofIndices(AC source, int fromIndex, int increment, BiFunction<? super AC,Integer,Integer> indexFunc)
// Forwards:
int[] a = {1, 2, 3, 2, 5, 1};
IntStream.ofIndices(a, N::indexOf).println(); // [0, 5]
IntStream.ofIndices(a, 1, N::indexOf).println(); // [5]
// Backwards
IntStream.ofIndices(a, 5, -1, N::lastIndexOf).println(); // [5, 0]
IntStream.ofIndices(a, 4, -1, N::lastIndexOf).println(); // [0]
// OR
// Forwards:
int[] source = { 1, 2, 3, 1, 2, 1 };
int[] targetSubArray = { 1, 2 };
IntStream.ofIndices(source, (a, fromIndex) -> Index.ofSubArray(a, fromIndex, targetSubArray, 0, targetSubArray.length).orElse(-1)).println(); // [0, 3]
// Backwards
IntStream.ofIndices(source, 5, -2, (a, fromIndex) -> Index.ofSubArray(a, fromIndex, targetSubArray, 0, targetSubArray.length).orElse(-1))
.println(); // [3, 0]
source
- fromIndex
- increment
- indexFunc
- ofIndices(Object, int, int, BiFunction)
public static <AC> IntStream ofIndices(AC source, int fromIndex, int increment, int sourceLen, BiFunction<? super AC,Integer,Integer> indexFunc)
@SafeVarargs public static IntStream concat(int[]... a)
@SafeVarargs public static IntStream concat(IntIterator... a)
@SafeVarargs public static IntStream concat(IntStream... a)
public static IntStream concat(Collection<? extends IntStream> c)
public static IntStream zip(int[] a, int[] b, IntBinaryOperator zipFunction)
a
- b
- public static IntStream zip(int[] a, int[] b, int[] c, IntTernaryOperator zipFunction)
a
- b
- c
- public static IntStream zip(IntIterator a, IntIterator b, IntBinaryOperator zipFunction)
a
- b
- public static IntStream zip(IntIterator a, IntIterator b, IntIterator c, IntTernaryOperator zipFunction)
a
- b
- public static IntStream zip(IntStream a, IntStream b, IntBinaryOperator zipFunction)
a
- b
- public static IntStream zip(IntStream a, IntStream b, IntStream c, IntTernaryOperator zipFunction)
a
- b
- public static IntStream zip(Collection<? extends IntStream> c, IntNFunction<Integer> zipFunction)
c
- zipFunction
- public static IntStream zip(int[] a, int[] b, int valueForNoneA, int valueForNoneB, IntBinaryOperator 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 IntStream zip(int[] a, int[] b, int[] c, int valueForNoneA, int valueForNoneB, int valueForNoneC, IntTernaryOperator 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 IntStream zip(IntIterator a, IntIterator b, int valueForNoneA, int valueForNoneB, IntBinaryOperator 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 IntStream zip(IntIterator a, IntIterator b, IntIterator c, int valueForNoneA, int valueForNoneB, int valueForNoneC, IntTernaryOperator 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 IntStream zip(IntStream a, IntStream b, int valueForNoneA, int valueForNoneB, IntBinaryOperator 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 IntStream zip(IntStream a, IntStream b, IntStream c, int valueForNoneA, int valueForNoneB, int valueForNoneC, IntTernaryOperator 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 IntStream zip(Collection<? extends IntStream> c, int[] valuesForNone, IntNFunction<Integer> zipFunction)
c
- valuesForNone
- value to fill for any iterator runs out of values.zipFunction
- public static IntStream merge(int[] a, int[] b, IntBiFunction<MergeResult> nextSelector)
a
- b
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static IntStream merge(int[] a, int[] b, int[] c, IntBiFunction<MergeResult> nextSelector)
a
- b
- c
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static IntStream merge(IntIterator a, IntIterator b, IntBiFunction<MergeResult> nextSelector)
a
- b
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static IntStream merge(IntIterator a, IntIterator b, IntIterator c, IntBiFunction<MergeResult> nextSelector)
a
- b
- c
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static IntStream merge(IntStream a, IntStream b, IntBiFunction<MergeResult> nextSelector)
a
- b
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static IntStream merge(IntStream a, IntStream b, IntStream c, IntBiFunction<MergeResult> nextSelector)
a
- b
- c
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static IntStream merge(Collection<? extends IntStream> c, IntBiFunction<MergeResult> nextSelector)
c
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static IntStream parallelMerge(Collection<? extends IntStream> c, IntBiFunction<MergeResult> nextSelector)
c
- nextSelector
- first parameter is selected if Nth.FIRST
is returned, otherwise the second parameter is selected.public static IntStream parallelMerge(Collection<? extends IntStream> c, IntBiFunction<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.