public final class IntStream
extends java.lang.Object
implements java.io.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.
|
OptionalDouble |
average()
Returns the average of elements in this stream.
|
Stream<java.lang.Integer> |
boxed()
Returns a
Stream consisting of the elements of this stream,
each boxed to an Integer . |
<R> R |
chain(Function<IntStream,R> function)
Applies custom operator on stream.
|
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(int[] a,
int[] b) |
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.
|
int |
count()
Returns the count of elements in this 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.
|
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. |
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. |
com.annimon.stream.iterator.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(com.annimon.stream.iterator.PrimitiveIterator.OfInt iterator)
Creates a
IntStream from PrimitiveIterator.OfInt . |
static IntStream |
ofCodePoints(java.lang.CharSequence charSequence)
Creates an
IntStream of code point values from the given sequence. |
IntStream |
onClose(java.lang.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.
|
void |
println() |
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 |
range(int startInclusive,
int endExclusive,
int by) |
static IntStream |
rangeClosed(int startInclusive,
int endInclusive)
Returns a sequential ordered
IntStream from startInclusive
(inclusive) to endInclusive (inclusive) by an incremental step of
1 . |
static IntStream |
rangeClosed(int startInclusive,
int endInclusive,
int by) |
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 |
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. |
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 |
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(int... values)
values
- the elements of the new streamjava.lang.NullPointerException
- if values
is nullpublic static IntStream of(com.annimon.stream.iterator.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 ofCodePoints(java.lang.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 range(int startInclusive, int endExclusive, int by)
public 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 rangeClosed(int startInclusive, int endInclusive, int by)
public 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 the previous element to produce
a new elementIntStream
java.lang.NullPointerException
- 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 onejava.lang.NullPointerException
- 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 streamjava.lang.NullPointerException
- if a
or b
is nullpublic static IntStream concat(int[] a, int[] b)
public com.annimon.stream.iterator.PrimitiveIterator.OfInt iterator()
IntStream
iterator.IntStream
iterator.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 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 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 elementLongStream
flatMap(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 elementDoubleStream
flatMap(com.annimon.stream.function.IntFunction)
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 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 functionjava.lang.NullPointerException
- 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 functionjava.lang.NullPointerException
- 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 elementsIntStream
public 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 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 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 int count()
This is a terminal operation.
public OptionalDouble average()
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 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 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 <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[] toArray()
This is a terminal operation.
public <R> R chain(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.chain(com.annimon.stream.function.Function)
public void println()
public IntStream onClose(java.lang.Runnable closeHandler)
This is an intermediate operation.
closeHandler
- an action to execute when the stream is closedpublic void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable