public final class LongStream
extends java.lang.Object
implements java.io.Closeable
long
-valued elements supporting aggregate operations.Stream
Modifier and Type | Method and Description |
---|---|
boolean |
allMatch(LongPredicate predicate)
Tests whether all elements match the given predicate.
|
boolean |
anyMatch(LongPredicate predicate)
Tests whether all elements match the given predicate.
|
OptionalDouble |
average()
Returns the average of elements in this stream.
|
Stream<java.lang.Long> |
boxed()
Returns a
Stream consisting of the elements of this stream,
each boxed to an Long . |
<R> R |
chain(Function<LongStream,R> function)
Applies custom operator on stream.
|
void |
close()
Causes close handler to be invoked if it exists.
|
<R> R |
collect(Supplier<R> supplier,
ObjLongConsumer<R> accumulator)
Collects elements to
supplier provided container by applying the given accumulation function. |
static LongStream |
concat(long[] a,
long[] b) |
static LongStream |
concat(LongStream a,
LongStream b)
Concatenates two streams.
|
int |
count()
Returns the count of elements in this stream.
|
LongStream |
distinct()
Returns a stream consisting of the distinct elements of this stream.
|
LongStream |
dropWhile(LongPredicate predicate)
Drops elements while the predicate is true and returns the rest.
|
static LongStream |
empty()
Returns an empty stream.
|
LongStream |
filter(LongPredicate predicate)
Returns
LongStream with elements that satisfy the given predicate. |
OptionalLong |
findFirst()
Returns the first element wrapped by
OptionalLong class. |
OptionalLong |
findLast()
Returns the last element wrapped by
OptionalLong class. |
LongStream |
flatMap(LongFunction<? extends LongStream> 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(LongConsumer action)
Performs an action for each element of this stream.
|
static LongStream |
generate(LongSupplier s)
Creates a
LongStream by elements that generated by LongSupplier . |
static LongStream |
iterate(long seed,
LongPredicate predicate,
LongUnaryOperator op)
Creates an
LongStream by iterative application LongUnaryOperator function
to an initial element seed , conditioned on satisfying the supplied predicate. |
static LongStream |
iterate(long seed,
LongUnaryOperator f)
Creates a
LongStream by iterative application LongUnaryOperator function
to an initial element seed . |
com.annimon.stream.iterator.PrimitiveIterator.OfLong |
iterator()
Returns internal
LongStream iterator. |
LongStream |
limit(long maxSize)
Returns a stream consisting of the elements of this stream, truncated
to be no longer than
maxSize in length. |
LongStream |
map(LongUnaryOperator mapper)
Returns an
LongStream consisting of the results of applying the given
function to the elements of this stream. |
DoubleStream |
mapToDouble(LongToDoubleFunction mapper)
Returns an
DoubleStream consisting of the results of applying the given
function to the elements of this stream. |
IntStream |
mapToInt(LongToIntFunction mapper)
Returns an
IntStream consisting of the results of applying the given
function to the elements of this stream. |
<R> Stream<R> |
mapToObj(LongFunction<? extends R> mapper)
Returns a
Stream consisting of the results of applying the given
function to the elements of this stream. |
OptionalLong |
max()
Returns an
OptionalLong describing the maximum element of this
stream, or an empty optional if this stream is empty. |
OptionalLong |
min()
Returns an
OptionalLong describing the minimum element of this
stream, or an empty optional if this stream is empty. |
boolean |
noneMatch(LongPredicate predicate)
Tests whether no elements match the given predicate.
|
static LongStream |
of(long... values)
Creates a
LongStream from the specified values. |
static LongStream |
of(com.annimon.stream.iterator.PrimitiveIterator.OfLong iterator)
Creates a
LongStream from PrimitiveIterator.OfLong . |
LongStream |
onClose(java.lang.Runnable closeHandler)
Adds close handler to the current stream.
|
LongStream |
peek(LongConsumer action)
Performs provided action on each element.
|
void |
println() |
static LongStream |
range(long startInclusive,
long endExclusive)
Returns a sequential ordered
LongStream from startInclusive
(inclusive) to endExclusive (exclusive) by an incremental step of
1 . |
static LongStream |
range(long startInclusive,
long endExclusive,
long by) |
static LongStream |
rangeClosed(long startInclusive,
long endInclusive)
Returns a sequential ordered
LongStream from startInclusive
(inclusive) to endInclusive (inclusive) by an incremental step of
1 . |
static LongStream |
rangeClosed(long startInclusive,
long endInclusive,
long by) |
OptionalLong |
reduce(LongBinaryOperator accumulator)
Performs a reduction on the elements of this stream, using an
associative accumulation function, and returns an
OptionalLong
describing the reduced value, if any. |
long |
reduce(long identity,
LongBinaryOperator accumulator)
Performs a reduction on the elements of this stream, using the provided
identity value and an associative accumulation function, and returns the
reduced value.
|
LongStream |
scan(LongBinaryOperator accumulator)
Returns a
LongStream produced by iterative application of a accumulation function
to reduction value and next element of the current stream. |
LongStream |
scan(long identity,
LongBinaryOperator accumulator)
Returns a
LongStream produced by iterative application of a accumulation function
to an initial element identity and next element of the current stream. |
LongStream |
skip(long n)
Skips first
n elements and returns LongStream with remaining elements. |
LongStream |
sorted()
Returns a stream consisting of the elements of this stream in sorted order.
|
LongStream |
sorted(java.util.Comparator<java.lang.Long> comparator)
Returns a stream consisting of the elements of this stream
in sorted order as determinated by provided
Comparator . |
long |
sum()
Returns the sum of elements in this stream.
|
LongStream |
takeUntil(LongPredicate stopPredicate)
Takes elements while the predicate returns
false . |
LongStream |
takeWhile(LongPredicate predicate)
Takes elements while the predicate returns
true . |
long[] |
toArray()
Returns an array containing the elements of this stream.
|
public static LongStream empty()
public static LongStream of(long... values)
LongStream
from the specified values.values
- the elements of the new streamjava.lang.NullPointerException
- if values
is nullpublic static LongStream of(com.annimon.stream.iterator.PrimitiveIterator.OfLong iterator)
LongStream
from PrimitiveIterator.OfLong
.iterator
- the iterator with elements to be passed to streamLongStream
java.lang.NullPointerException
- if iterator
is nullpublic static LongStream range(long startInclusive, long endExclusive)
LongStream
from startInclusive
(inclusive) to endExclusive
(exclusive) by an incremental step of
1
.startInclusive
- the (inclusive) initial valueendExclusive
- the exclusive upper boundLongStream
for the range of long
elementspublic static LongStream range(long startInclusive, long endExclusive, long by)
public static LongStream rangeClosed(long startInclusive, long endInclusive)
LongStream
from startInclusive
(inclusive) to endInclusive
(inclusive) by an incremental step of
1
.startInclusive
- the (inclusive) initial valueendInclusive
- the inclusive upper boundLongStream
for the range of long
elementspublic static LongStream rangeClosed(long startInclusive, long endInclusive, long by)
public static LongStream generate(LongSupplier s)
LongStream
by elements that generated by LongSupplier
.s
- the LongSupplier
for generated elementsLongStream
java.lang.NullPointerException
- if s
is nullpublic static LongStream iterate(long seed, LongUnaryOperator f)
LongStream
by iterative application LongUnaryOperator
function
to an initial element seed
. Produces LongStream
consisting of
seed
, f(seed)
, f(f(seed))
, etc.
The first element (position 0
) in the LongStream
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 elementLongStream
java.lang.NullPointerException
- if f
is nullpublic static LongStream iterate(long seed, LongPredicate predicate, LongUnaryOperator op)
LongStream
by iterative application LongUnaryOperator
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 LongStream concat(LongStream a, LongStream 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 LongStream concat(long[] a, long[] b)
public com.annimon.stream.iterator.PrimitiveIterator.OfLong iterator()
LongStream
iterator.LongStream
iterator.public Stream<java.lang.Long> boxed()
Stream
consisting of the elements of this stream,
each boxed to an Long
.
This is an lazy intermediate operation.
Stream
consistent of the elements of this stream,
each boxed to an Long
public LongStream filter(LongPredicate predicate)
LongStream
with elements that satisfy the given predicate.
This is an intermediate operation.
Example:
predicate: (a) -> a > 2 stream: [1, 2, 3, 4, -8, 0, 11] result: [3, 4, 11]
predicate
- the predicate used to filter elementspublic LongStream map(LongUnaryOperator mapper)
LongStream
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
- the mapper function used to apply to each elementStream.map(com.annimon.stream.function.Function)
public <R> Stream<R> mapToObj(LongFunction<? 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 mapToInt(LongToIntFunction mapper)
IntStream
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 elementIntStream
public DoubleStream mapToDouble(LongToDoubleFunction 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
public LongStream flatMap(LongFunction<? extends LongStream> 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
- the mapper function used to apply to each elementStream.flatMap(com.annimon.stream.function.Function)
public LongStream distinct()
This is a stateful intermediate operation.
Example:
stream: [1, 4, 2, 3, 3, 4, 1] result: [1, 4, 2, 3]
public LongStream sorted()
This is a stateful intermediate operation.
Example:
stream: [3, 4, 1, 2] result: [1, 2, 3, 4]
public LongStream sorted(java.util.Comparator<java.lang.Long> comparator)
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 elementsLongStream
public LongStream peek(LongConsumer action)
This is an intermediate operation.
action
- the action to be performed on each elementpublic LongStream scan(LongBinaryOperator accumulator)
LongStream
produced by iterative application of a accumulation function
to reduction value and next element of the current stream.
Produces a LongStream
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 LongStream scan(long identity, LongBinaryOperator accumulator)
LongStream
produced by iterative application of a accumulation function
to an initial element identity
and next element of the current stream.
Produces a LongStream
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 LongStream takeWhile(LongPredicate 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 elementsLongStream
public LongStream takeUntil(LongPredicate 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 elementsLongStream
public LongStream dropWhile(LongPredicate 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 elementsLongStream
public LongStream 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 LongStream skip(long n)
n
elements and returns LongStream
with remaining elements.
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 elements to skipjava.lang.IllegalArgumentException
- if n
is negativepublic void forEach(LongConsumer action)
This is a terminal operation.
action
- the action to be performed on each elementpublic long reduce(long identity, LongBinaryOperator accumulator)
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 OptionalLong reduce(LongBinaryOperator accumulator)
OptionalLong
describing the reduced value, if any.
The accumulator
function must be an associative function.
This is a terminal operation.
accumulator
- the accumulation functionreduce(com.annimon.stream.function.LongBinaryOperator)
public long[] toArray()
This is a terminal operation.
public <R> R collect(Supplier<R> supplier, ObjLongConsumer<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 long sum()
public OptionalLong min()
OptionalLong
describing the minimum element of this
stream, or an empty optional if this stream is empty.
This is a terminal operation.
public OptionalLong max()
OptionalLong
describing the maximum element of this
stream, or an empty optional if this stream is empty.
This is a terminal operation.
public int count()
This is a terminal operation.
public OptionalDouble average()
This is a terminal operation.
public boolean anyMatch(LongPredicate 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
- the predicate used to match elementstrue
if any elements of the stream match the provided
predicate, otherwise false
public boolean allMatch(LongPredicate 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
- the predicate used to match elementstrue
if either all elements of the stream match the
provided predicate or the stream is empty, otherwise false
public boolean noneMatch(LongPredicate 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
- the predicate used to match elementstrue
if either no elements of the stream match the
provided predicate or the stream is empty, otherwise false
public OptionalLong findFirst()
OptionalLong
class.
If stream is empty, returns OptionalLong.empty()
.
This is a short-circuiting terminal operation.
OptionalLong
with first element
or OptionalLong.empty()
if stream is emptypublic OptionalLong findLast()
OptionalLong
class.
If stream is empty, returns OptionalLong.empty()
.
This is a short-circuiting terminal operation.
OptionalLong
with the last element
or OptionalLong.empty()
if the stream is emptypublic <R> R chain(Function<LongStream,R> function)
LongStream
for intermediate operations,
or any value for terminal operation.
Operator examples:
// Intermediate operator
public class Zip implements Function<LongStream, LongStream> {
private final LongStream secondStream;
private final LongBinaryOperator combiner;
public Zip(LongStream secondStream, LongBinaryOperator combiner) {
this.secondStream = secondStream;
this.combiner = combiner;
}
@Override
public LongStream apply(LongStream firstStream) {
final PrimitiveIterator.OfLong it1 = firstStream.iterator();
final PrimitiveIterator.OfLong it2 = secondStream.iterator();
return LongStream.of(new PrimitiveIterator.OfLong() {
@Override
public boolean hasNext() {
return it1.hasNext() && it2.hasNext();
}
@Override
public long nextLong() {
return combiner.applyAsLong(it1.nextLong(), it2.nextLong());
}
});
}
}
// Intermediate operator based on existing stream operators
public class SkipAndLimit implements UnaryOperator<LongStream> {
private final int skip, limit;
public SkipAndLimit(int skip, int limit) {
this.skip = skip;
this.limit = limit;
}
@Override
public LongStream apply(LongStream stream) {
return stream.skip(skip).limit(limit);
}
}
// Terminal operator
public class LongSummaryStatistics implements Function<LongStream, long[]> {
@Override
public long[] apply(LongStream stream) {
long count = 0;
long sum = 0;
final PrimitiveIterator.OfLong it = stream.iterator();
while (it.hasNext()) {
count++;
sum += it.nextLong();
}
return new long[] {count, sum};
}
}
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 LongStream 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