public final class IntStream
extends java.lang.Object
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<java.lang.Integer> |
boxed()
Returns a
Stream consisting of the elements of this stream,
each boxed to an Integer . |
<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 |
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,
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. |
<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 param
|
static IntStream |
of(PrimitiveIterator.OfInt iterator)
Creates a
IntStream from PrimitiveIterator.OfInt . |
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. |
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(java.util.Comparator<java.lang.Integer> comparator)
Returns
IntStream with sorted elements (as determinated by provided Comparator ). |
int |
sum()
Returns the sum of elements in this stream.
|
IntStream |
takeWhile(IntPredicate predicate)
Takes elements while the predicate is 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 streamIntStream
java.lang.NullPointerException
- if iterator
is nullpublic static IntStream of(int... values)
values
- the elements of the new streamjava.lang.NullPointerException
- if values
is nullpublic static IntStream of(int t)
t
- element of the streampublic 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 elementsIntStream
java.lang.NullPointerException
- 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 to the previous element to produce
a new elementIntStream
java.lang.NullPointerException
- if f
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 streamjava.lang.NullPointerException
- 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 functionjava.lang.NullPointerException
- if function
is nullStream.custom(com.annimon.stream.function.Function)
public Stream<java.lang.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 Integer
public 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 elementIntStream
public <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 elementStream
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(java.util.Comparator<java.lang.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 elementsIntStream
public 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 widthIntStream
java.lang.IllegalArgumentException
- 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 takeWhile(IntPredicate predicate)
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 elementsIntStream
public 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 elementsIntStream
public 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 tojava.lang.IllegalArgumentException
- 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 skipjava.lang.IllegalArgumentException
- 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 false
public 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 false
public 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 false
public 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 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
java.util.NoSuchElementException
- if stream is emptyjava.lang.IllegalStateException
- 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 emptyjava.lang.IllegalStateException
- if stream contains more than one element