public final class IntStream extends Object implements Closeable
int
primitive specialization of Stream.| Modifier and Type | Method and Description |
|---|---|
boolean |
allMatch(IntPredicate predicate)
Returns whether all elements of this stream match the provided predicate.
|
boolean |
anyMatch(IntPredicate predicate)
Returns whether any elements of this stream match the provided
predicate.
|
Stream<Integer> |
boxed()
Returns a
Stream consisting of the elements of this stream,
each boxed to an Integer. |
void |
close()
Causes close handler to be invoked if it exists.
|
<R> R |
collect(Supplier<R> supplier,
ObjIntConsumer<R> accumulator)
Collects elements to
supplier provided container by applying the given accumulation function. |
static IntStream |
concat(IntStream a,
IntStream b)
Creates a lazily concatenated stream whose elements are all the
elements of the first stream followed by all the elements of the
second stream.
|
long |
count()
Returns the count of elements in this stream.
|
<R> R |
custom(Function<IntStream,R> function)
Applies custom operator on stream.
|
IntStream |
distinct()
Returns a stream consisting of the distinct elements of this stream.
|
IntStream |
dropWhile(IntPredicate predicate)
Drops elements while the predicate is true and returns the rest.
|
static IntStream |
empty()
Returns an empty stream.
|
IntStream |
filter(IntPredicate predicate)
Returns a stream consisting of the elements of this stream that match
the given predicate.
|
IntStream |
filterNot(IntPredicate predicate)
Returns a stream consisting of the elements of this stream that don't
match the given predicate.
|
OptionalInt |
findFirst()
Returns an
OptionalInt describing the first element of this
stream, or an empty OptionalInt if the stream is empty. |
OptionalInt |
findLast()
Returns the last element wrapped by
OptionalInt class. |
OptionalInt |
findSingle()
Returns the single element wrapped by
OptionalInt class. |
IntStream |
flatMap(IntFunction<? extends IntStream> mapper)
Returns a stream consisting of the results of replacing each element of
this stream with the contents of a mapped stream produced by applying
the provided mapping function to each element.
|
void |
forEach(IntConsumer action)
Performs an action for each element of this stream.
|
static IntStream |
generate(IntSupplier s)
Returns an infinite sequential unordered stream where each element is
generated by the provided
IntSupplier. |
static IntStream |
iterate(int seed,
IntPredicate predicate,
IntUnaryOperator op)
Creates an
IntStream by iterative application IntUnaryOperator function
to an initial element seed, conditioned on satisfying the supplied predicate. |
static IntStream |
iterate(int seed,
IntUnaryOperator f)
Returns an infinite sequential ordered
IntStream produced by iterative
application of a function f to an initial element seed,
producing a Stream consisting of seed, f(seed),
f(f(seed)), etc. |
PrimitiveIterator.OfInt |
iterator()
Returns internal
IntStream iterator. |
IntStream |
limit(long maxSize)
Returns a stream consisting of the elements of this stream, truncated
to be no longer than
maxSize in length. |
IntStream |
map(IntUnaryOperator mapper)
Returns an
IntStream consisting of the results of applying the given
function to the elements of this stream. |
DoubleStream |
mapToDouble(IntToDoubleFunction mapper)
Returns a
DoubleStream consisting of the results of applying the given
function to the elements of this stream. |
LongStream |
mapToLong(IntToLongFunction mapper)
Returns a
LongStream consisting of the results of applying the given
function to the elements of this stream. |
<R> Stream<R> |
mapToObj(IntFunction<? extends R> mapper)
Returns a
Stream consisting of the results of applying the given
function to the elements of this stream. |
OptionalInt |
max()
Returns an
OptionalInt describing the maximum element of this
stream, or an empty optional if this stream is empty. |
OptionalInt |
min()
Returns an
OptionalInt describing the minimum element of this
stream, or an empty optional if this stream is empty. |
boolean |
noneMatch(IntPredicate predicate)
Returns whether no elements of this stream match the provided predicate.
|
static IntStream |
of(int... values)
Returns stream whose elements are the specified values.
|
static IntStream |
of(int t)
Returns stream which contains single element passed as parameter.
|
static IntStream |
of(PrimitiveIterator.OfInt iterator)
Creates a
IntStream from PrimitiveIterator.OfInt. |
static IntStream |
ofCodePoints(CharSequence charSequence)
Creates an
IntStream of code point values from the given sequence. |
IntStream |
onClose(Runnable closeHandler)
Adds close handler to the current stream.
|
IntStream |
peek(IntConsumer action)
Returns a stream consisting of the elements of this stream, additionally
performing the provided action on each element as elements are consumed
from the resulting stream.
|
static IntStream |
range(int startInclusive,
int endExclusive)
Returns a sequential ordered
IntStream from startInclusive
(inclusive) to endExclusive (exclusive) by an incremental step of
1. |
static IntStream |
rangeClosed(int startInclusive,
int endInclusive)
Returns a sequential ordered
IntStream from startInclusive
(inclusive) to endInclusive (inclusive) by an incremental step of
1. |
OptionalInt |
reduce(IntBinaryOperator op)
Performs a reduction on the elements of this stream, using an
associative accumulation function, and returns an
OptionalInt
describing the reduced value, if any. |
int |
reduce(int identity,
IntBinaryOperator op)
Performs a reduction on the elements of this stream, using the provided
identity value and an associative accumulation function, and returns the
reduced value.
|
IntStream |
sample(int stepWidth)
Samples the
IntStream by emitting every n-th element. |
IntStream |
scan(IntBinaryOperator accumulator)
Returns a
IntStream produced by iterative application of a accumulation function
to reduction value and next element of the current stream. |
IntStream |
scan(int identity,
IntBinaryOperator accumulator)
Returns a
IntStream produced by iterative application of a accumulation function
to an initial element identity and next element of the current stream. |
int |
single()
Returns the single element of stream.
|
IntStream |
skip(long n)
Returns a stream consisting of the remaining elements of this stream
after discarding the first
n elements of the stream. |
IntStream |
sorted()
Returns a stream consisting of the elements of this stream in sorted
order.
|
IntStream |
sorted(Comparator<Integer> comparator)
Returns
IntStream with sorted elements (as determinated by provided Comparator). |
int |
sum()
Returns the sum of elements in this stream.
|
IntStream |
takeUntil(IntPredicate stopPredicate)
Takes elements while the predicate returns
false. |
IntStream |
takeWhile(IntPredicate predicate)
Takes elements while the predicate returns
true. |
int[] |
toArray()
Returns an array containing the elements of this stream.
|
public static IntStream empty()
public static IntStream of(PrimitiveIterator.OfInt iterator)
IntStream from PrimitiveIterator.OfInt.iterator - the iterator with elements to be passed to streamIntStreamNullPointerException - if iterator is nullpublic static IntStream of(int... values)
values - the elements of the new streamNullPointerException - if values is nullpublic static IntStream of(int t)
t - element of the streampublic static IntStream ofCodePoints(CharSequence charSequence)
IntStream of code point values from the given sequence.
Any surrogate pairs encountered in the sequence are combined as if by Character.toCodePoint and the result is passed to the stream.
Any other code units, including ordinary BMP characters, unpaired surrogates, and
undefined code units, are zero-extended to int values which are then
passed to the stream.charSequence - the sequence where to get all code points values.public static IntStream range(int startInclusive, int endExclusive)
IntStream from startInclusive
(inclusive) to endExclusive (exclusive) by an incremental step of
1.startInclusive - the (inclusive) initial valueendExclusive - the exclusive upper boundIntStream for the range of int
elementspublic static IntStream rangeClosed(int startInclusive, int endInclusive)
IntStream from startInclusive
(inclusive) to endInclusive (inclusive) by an incremental step of
1.startInclusive - the (inclusive) initial valueendInclusive - the inclusive upper boundIntStream for the range of int
elementspublic static IntStream generate(IntSupplier s)
IntSupplier. This is suitable for
generating constant streams, streams of random elements, etc.s - the IntSupplier for generated elementsIntStreamNullPointerException - if s is nullpublic static IntStream iterate(int seed, IntUnaryOperator f)
IntStream produced by iterative
application of a function f to an initial element seed,
producing a Stream consisting of seed, f(seed),
f(f(seed)), etc.
The first element (position 0) in the IntStream will be
the provided seed. For n > 0, the element at position
n, will be the result of applying the function f to the
element at position n - 1.
Example:
seed: 1 f: (a) -> a + 5 result: [1, 6, 11, 16, ...]
seed - the initial elementf - a function to be applied to the previous element to produce
a new elementIntStreamNullPointerException - if f is nullpublic static IntStream iterate(int seed, IntPredicate predicate, IntUnaryOperator op)
IntStream by iterative application IntUnaryOperator function
to an initial element seed, conditioned on satisfying the supplied predicate.
Example:
seed: 0 predicate: (a) -> a < 20 f: (a) -> a + 5 result: [0, 5, 10, 15]
seed - the initial valuepredicate - a predicate to determine when the stream must terminateop - operator to produce new element by previous oneNullPointerException - if op is nullpublic static IntStream concat(IntStream a, IntStream b)
Example:
stream a: [1, 2, 3, 4] stream b: [5, 6] result: [1, 2, 3, 4, 5, 6]
a - the first streamb - the second streamNullPointerException - if a or b is nullpublic PrimitiveIterator.OfInt iterator()
IntStream iterator.IntStream iterator.public <R> R custom(Function<IntStream,R> function)
IntStream for intermediate operations,
or any value for terminal operation.
Operator examples:
// Intermediate operator
public class Zip<T> implements Function<IntStream, IntStream> {
@Override
public IntStream apply(IntStream firstStream) {
final PrimitiveIterator.OfInt it1 = firstStream.iterator();
final PrimitiveIterator.OfInt it2 = secondStream.iterator();
return IntStream.of(new PrimitiveIterator.OfInt() {
@Override
public boolean hasNext() {
return it1.hasNext() && it2.hasNext();
}
@Override
public int nextInt() {
return combiner.applyAsInt(it1.nextInt(), it2.nextInt());
}
});
}
}
// Intermediate operator based on existing stream operators
public class SkipAndLimit implements UnaryOperator<IntStream> {
private final int skip, limit;
public SkipAndLimit(int skip, int limit) {
this.skip = skip;
this.limit = limit;
}
@Override
public IntStream apply(IntStream stream) {
return stream.skip(skip).limit(limit);
}
}
// Terminal operator
public class Average implements Function<IntStream, Double> {
long count = 0, sum = 0;
@Override
public Double apply(IntStream stream) {
final PrimitiveIterator.OfInt it = stream.iterator();
while (it.hasNext()) {
count++;
sum += it.nextInt();
}
return (count == 0) ? 0 : sum / (double) count;
}
}
R - the type of the resultfunction - a transforming functionNullPointerException - if function is nullStream.custom(com.annimon.stream.function.Function)public Stream<Integer> boxed()
Stream consisting of the elements of this stream,
each boxed to an Integer.
This is an lazy intermediate operation.
Stream consistent of the elements of this stream,
each boxed to an Integerpublic IntStream filter(IntPredicate predicate)
This is an intermediate operation.
Example:
predicate: (a) -> a > 2 stream: [1, 2, 3, 4, -8, 0, 11] result: [3, 4, 11]
predicate - non-interfering, stateless predicate to apply to each
element to determine if it should be includedpublic IntStream filterNot(IntPredicate predicate)
This is an intermediate operation.
predicate - non-interfering, stateless predicate to apply to each
element to determine if it should not be includedpublic IntStream map(IntUnaryOperator mapper)
IntStream consisting of the results of applying the given
function to the elements of this stream.
This is an intermediate operation.
Example:
mapper: (a) -> a + 5 stream: [1, 2, 3, 4] result: [6, 7, 8, 9]
mapper - a non-interfering stateless function to apply to
each elementIntStreampublic <R> Stream<R> mapToObj(IntFunction<? extends R> mapper)
Stream consisting of the results of applying the given
function to the elements of this stream.
This is an intermediate operation.
R - the type resultmapper - the mapper function used to apply to each elementStreampublic LongStream mapToLong(IntToLongFunction mapper)
LongStream consisting of the results of applying the given
function to the elements of this stream.
This is an intermediate operation.
mapper - the mapper function used to apply to each elementLongStreamflatMap(com.annimon.stream.function.IntFunction)public DoubleStream mapToDouble(IntToDoubleFunction mapper)
DoubleStream consisting of the results of applying the given
function to the elements of this stream.
This is an intermediate operation.
mapper - the mapper function used to apply to each elementDoubleStreamflatMap(com.annimon.stream.function.IntFunction)public IntStream flatMap(IntFunction<? extends IntStream> mapper)
This is an intermediate operation.
Example:
mapper: (a) -> [a, a + 5] stream: [1, 2, 3, 4] result: [1, 6, 2, 7, 3, 8, 4, 9]
mapper - a non-interfering stateless function to apply to each
element which produces an IntStream of new valuesStream.flatMap(Function)public IntStream distinct()
This is a stateful intermediate operation.
Example:
stream: [1, 4, 2, 3, 3, 4, 1] result: [1, 4, 2, 3]
public IntStream sorted()
This is a stateful intermediate operation.
Example:
stream: [3, 4, 1, 2] result: [1, 2, 3, 4]
public IntStream sorted(Comparator<Integer> comparator)
IntStream with sorted elements (as determinated by provided Comparator).
This is a stateful intermediate operation.
Example:
comparator: (a, b) -> -a.compareTo(b) stream: [1, 2, 3, 4] result: [4, 3, 2, 1]
comparator - the Comparator to compare elementsIntStreampublic IntStream sample(int stepWidth)
IntStream by emitting every n-th element.
This is an intermediate operation.
Example:
stepWidth: 3 stream: [1, 2, 3, 4, 5, 6, 7, 8] result: [1, 4, 7]
stepWidth - step widthIntStreamIllegalArgumentException - if stepWidth is zero or negativeStream.sample(int)public IntStream peek(IntConsumer action)
This is an intermediate operation.
action - the action to be performed on each elementpublic IntStream scan(IntBinaryOperator accumulator)
IntStream produced by iterative application of a accumulation function
to reduction value and next element of the current stream.
Produces a IntStream consisting of value1, acc(value1, value2),
acc(acc(value1, value2), value3), 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 functionNullPointerException - if accumulator is nullpublic IntStream scan(int identity, IntBinaryOperator accumulator)
IntStream produced by iterative application of a accumulation function
to an initial element identity and next element of the current stream.
Produces a IntStream consisting of identity, acc(identity, value1),
acc(acc(identity, value1), value2), etc.
This is an intermediate operation.
Example:
identity: 0 accumulator: (a, b) -> a + b stream: [1, 2, 3, 4, 5] result: [0, 1, 3, 6, 10, 15]
identity - the initial valueaccumulator - the accumulation functionNullPointerException - if accumulator is nullpublic IntStream takeWhile(IntPredicate predicate)
true.
This is an intermediate operation.
Example:
predicate: (a) -> a < 3 stream: [1, 2, 3, 4, 1, 2, 3, 4] result: [1, 2]
predicate - the predicate used to take elementsIntStreampublic IntStream takeUntil(IntPredicate stopPredicate)
false.
Once predicate condition is satisfied by an element, the stream
finishes with this element.
This is an intermediate operation.
Example:
stopPredicate: (a) -> a > 2 stream: [1, 2, 3, 4, 1, 2, 3, 4] result: [1, 2, 3]
stopPredicate - the predicate used to take elementsIntStreampublic IntStream dropWhile(IntPredicate predicate)
This is an intermediate operation.
Example:
predicate: (a) -> a < 3 stream: [1, 2, 3, 4, 1, 2, 3, 4] result: [3, 4, 1, 2, 3, 4]
predicate - the predicate used to drop elementsIntStreampublic IntStream limit(long maxSize)
maxSize in length.
This is a short-circuiting stateful intermediate operation.
Example:
maxSize: 3 stream: [1, 2, 3, 4, 5] result: [1, 2, 3] maxSize: 10 stream: [1, 2] result: [1, 2]
maxSize - the number of elements the stream should be limited toIllegalArgumentException - if maxSize is negativepublic IntStream skip(long n)
n elements of the stream.
If this stream contains fewer than n elements then an
empty stream will be returned.
This is a stateful intermediate operation.
Example:
n: 3 stream: [1, 2, 3, 4, 5] result: [4, 5] n: 10 stream: [1, 2] result: []
n - the number of leading elements to skipIllegalArgumentException - if n is negativepublic void forEach(IntConsumer action)
This is a terminal operation.
action - a non-interfering action to perform on the elementspublic int reduce(int identity,
IntBinaryOperator op)
The identity value must be an identity for the accumulator
function. This means that for all x,
accumulator.apply(identity, x) is equal to x.
The accumulator function must be an associative function.
This is a terminal operation.
Example:
identity: 0 accumulator: (a, b) -> a + b stream: [1, 2, 3, 4, 5] result: 15
public OptionalInt reduce(IntBinaryOperator op)
OptionalInt
describing the reduced value, if any.
The op function must be an associative function.
This is a terminal operation.
op - an associative, non-interfering, stateless function for
combining two valuesreduce(int, IntBinaryOperator)public int[] toArray()
This is a terminal operation.
public <R> R collect(Supplier<R> supplier, ObjIntConsumer<R> accumulator)
supplier provided container by applying the given accumulation function.
This is a terminal operation.
R - the type of the resultsupplier - the supplier function that provides containeraccumulator - the accumulation functionStream.collect(com.annimon.stream.function.Supplier, com.annimon.stream.function.BiConsumer)public int sum()
public OptionalInt min()
OptionalInt describing the minimum element of this
stream, or an empty optional if this stream is empty.
This is a terminal operation.
OptionalInt containing the minimum element of this
stream, or an empty OptionalInt if the stream is emptypublic OptionalInt max()
OptionalInt describing the maximum element of this
stream, or an empty optional if this stream is empty.
This is a terminal operation.
OptionalInt containing the maximum element of this
stream, or an empty OptionalInt if the stream is emptypublic long count()
This is a terminal operation.
public boolean anyMatch(IntPredicate predicate)
false is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
Example:
predicate: (a) -> a == 5 stream: [1, 2, 3, 4, 5] result: true predicate: (a) -> a == 5 stream: [5, 5, 5] result: true
predicate - a non-interfering stateless predicate to apply
to elements of this streamtrue if any elements of the stream match the provided
predicate, otherwise falsepublic boolean allMatch(IntPredicate predicate)
true is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
Example:
predicate: (a) -> a == 5 stream: [1, 2, 3, 4, 5] result: false predicate: (a) -> a == 5 stream: [5, 5, 5] result: true
predicate - a non-interfering stateless predicate to apply to
elements of this streamtrue if either all elements of the stream match the
provided predicate or the stream is empty, otherwise falsepublic boolean noneMatch(IntPredicate predicate)
true is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
Example:
predicate: (a) -> a == 5 stream: [1, 2, 3, 4, 5] result: false predicate: (a) -> a == 5 stream: [1, 2, 3] result: true
predicate - a non-interfering stateless predicate to apply to
elements of this streamtrue if either no elements of the stream match the
provided predicate or the stream is empty, otherwise falsepublic OptionalInt findFirst()
OptionalInt describing the first element of this
stream, or an empty OptionalInt if the stream is empty.
This is a short-circuiting terminal operation.
OptionalInt describing the first element of this stream,
or an empty OptionalInt if the stream is emptypublic OptionalInt findLast()
OptionalInt class.
If stream is empty, returns OptionalInt.empty().
This is a short-circuiting terminal operation.
OptionalInt with the last element
or OptionalInt.empty() if the stream is emptypublic int single()
NoSuchElementException.
If stream contains more than one element, throws IllegalStateException.
This is a short-circuiting terminal operation.
Example:
stream: [] result: NoSuchElementException stream: [1] result: 1 stream: [1, 2, 3] result: IllegalStateException
NoSuchElementException - if stream is emptyIllegalStateException - if stream contains more than one elementpublic OptionalInt findSingle()
OptionalInt class.
If stream is empty, returns OptionalInt.empty().
If stream contains more than one element, throws IllegalStateException.
This is a short-circuiting terminal operation.
Example:
stream: [] result: OptionalInt.empty() stream: [1] result: OptionalInt.of(1) stream: [1, 2, 3] result: IllegalStateException
OptionalInt with single element or OptionalInt.empty() if stream is emptyIllegalStateException - if stream contains more than one elementpublic IntStream onClose(Runnable closeHandler)
This is an intermediate operation.
closeHandler - an action to execute when the stream is closedpublic void close()
close in interface Closeableclose in interface AutoCloseableCopyright © 2017. All rights reserved.