public final class LongStream
extends java.lang.Object
implements java.io.Closeable
long-valued elements supporting aggregate operations.Stream| Modifier and Type | Class and Description |
|---|---|
static interface |
LongStream.LongMapMultiConsumer
Represents an operation on two input arguments.
|
| 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.
|
LongStream |
append(LongStream stream)
Appends given
LongStream to current and returns a new stream. |
Stream<java.lang.Long> |
boxed()
Returns a
Stream consisting of the elements of this stream,
each boxed to an Long. |
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(LongStream a,
LongStream b)
Lazily concatenates two streams.
|
static LongStream |
concat(LongStream a,
LongStream b,
LongStream... rest)
Lazily concatenates three or more streams.
|
long |
count()
Returns the count of elements in this stream.
|
<R> R |
custom(Function<LongStream,R> function)
Applies custom operator on 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. |
LongStream |
filterIndexed(IndexedLongPredicate predicate)
Returns a
LongStream with elements that satisfy the given IndexedLongPredicate. |
LongStream |
filterIndexed(int from,
int step,
IndexedLongPredicate predicate)
Returns a
LongStream with elements that satisfy the given IndexedLongPredicate. |
LongStream |
filterNot(LongPredicate predicate)
Returns
LongStream with elements that does not satisfy the given predicate. |
OptionalLong |
findFirst()
Returns the first element wrapped by
OptionalLong class. |
long |
findFirstOrElse(long other)
Returns the first element if stream is not empty,
otherwise returns
other. |
OptionalLong |
findLast()
Returns the last element wrapped by
OptionalLong class. |
OptionalLong |
findSingle()
Returns the single 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.
|
void |
forEachIndexed(IndexedLongConsumer action)
Performs the given indexed action on each element.
|
void |
forEachIndexed(int from,
int step,
IndexedLongConsumer action)
Performs the given indexed action on each element.
|
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. |
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. |
LongStream |
mapIndexed(IndexedLongUnaryOperator mapper)
Returns a
LongStream with elements that obtained
by applying the given IndexedLongUnaryOperator. |
LongStream |
mapIndexed(int from,
int step,
IndexedLongUnaryOperator mapper)
Returns a
LongStream with elements that obtained
by applying the given IndexedLongUnaryOperator. |
LongStream |
mapMulti(LongStream.LongMapMultiConsumer 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.
|
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(long t)
Returns stream which contains single element passed as param
|
static LongStream |
of(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.
|
LongStream |
prepend(LongStream stream)
Prepends given
LongStream to current and returns a new stream. |
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 |
rangeClosed(long startInclusive,
long endInclusive)
Returns a sequential ordered
LongStream from startInclusive
(inclusive) to endInclusive (inclusive) by an incremental step of
1. |
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 |
sample(int stepWidth)
Samples the
LongStream by emitting every n-th element. |
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. |
long |
single()
Returns the single element of 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.
|
@NotNull public static LongStream empty()
@NotNull public static LongStream of(@NotNull PrimitiveIterator.OfLong iterator)
LongStream from PrimitiveIterator.OfLong.iterator - the iterator with elements to be passed to streamLongStreamjava.lang.NullPointerException - if iterator is null@NotNull public static LongStream of(@NotNull long... values)
LongStream from the specified values.values - the elements of the new streamjava.lang.NullPointerException - if values is null@NotNull public static LongStream of(long t)
t - element of the stream@NotNull public 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
elements@NotNull 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
elements@NotNull public static LongStream generate(@NotNull LongSupplier s)
LongStream by elements that generated by LongSupplier.s - the LongSupplier for generated elementsLongStreamjava.lang.NullPointerException - if s is null@NotNull public static LongStream iterate(long seed, @NotNull 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 elementLongStreamjava.lang.NullPointerException - if f is null@NotNull public static LongStream iterate(long seed, @NotNull LongPredicate predicate, @NotNull 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 null@NotNull public static LongStream concat(@NotNull LongStream a, @NotNull 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 null@NotNull public static LongStream concat(@NotNull LongStream a, @NotNull LongStream b, @NotNull LongStream... rest)
Example:
stream a: [1, 2, 3, 4] stream b: [5, 6] stream c: [7] stream d: [8, 9, 10] result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a - the first streamb - the second streamrest - the rest of streamsjava.lang.NullPointerException - if a or b
or rest is nullpublic PrimitiveIterator.OfLong iterator()
LongStream iterator.LongStream iterator.@Nullable
public <R> R custom(@NotNull
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.custom(com.annimon.stream.function.Function)@NotNull 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@NotNull public LongStream prepend(@NotNull LongStream stream)
LongStream to current and returns a new stream.
This is similar to LongStream.concat(stream, this)
Example:
current: [1, 2, 3] stream: [4, 5, 6] result: [4, 5, 6, 1, 2, 3]
stream - the stream to prependconcat(LongStream, LongStream)@NotNull public LongStream append(@NotNull LongStream stream)
LongStream to current and returns a new stream.
This is similar to LongStream.concat(this, stream)
Example:
current: [1, 2, 3] stream: [4, 5, 6] result: [1, 2, 3, 4, 5, 6]
stream - the stream to appendconcat(LongStream, LongStream)@NotNull public LongStream filter(@NotNull 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 elements@NotNull public LongStream filterIndexed(@NotNull IndexedLongPredicate predicate)
LongStream with elements that satisfy the given IndexedLongPredicate.
This is an intermediate operation.
Example:
predicate: (index, value) -> (index + value) > 6 stream: [1, 2, 3, 4, 0, 11] index: [0, 1, 2, 3, 4, 5] sum: [1, 3, 5, 7, 4, 16] filter: [ 7, 16] result: [4, 11]
predicate - the IndexedLongPredicate used to filter elements@NotNull public LongStream filterIndexed(int from, int step, @NotNull IndexedLongPredicate predicate)
LongStream with elements that satisfy the given IndexedLongPredicate.
This is an intermediate operation.
Example:
from: 4 step: 3 predicate: (index, value) -> (index + value) > 15 stream: [1, 2, 3, 4, 0, 11] index: [4, 7, 10, 13, 16, 19] sum: [5, 9, 13, 17, 16, 30] filter: [ 17, 16, 30] result: [4, 0, 11]
from - the initial value of the index (inclusive)step - the step of the indexpredicate - the IndexedLongPredicate used to filter elements@NotNull public LongStream filterNot(@NotNull LongPredicate predicate)
LongStream with elements that does not satisfy the given predicate.
This is an intermediate operation.
predicate - the predicate used to filter elements@NotNull public LongStream map(@NotNull 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)@NotNull public LongStream mapIndexed(@NotNull IndexedLongUnaryOperator mapper)
LongStream with elements that obtained
by applying the given IndexedLongUnaryOperator.
This is an intermediate operation.
Example:
mapper: (index, value) -> (index * value) stream: [1, 2, 3, 4] index: [0, 1, 2, 3] result: [0, 2, 6, 12]
mapper - the mapper function used to apply to each element@NotNull public LongStream mapIndexed(int from, int step, @NotNull IndexedLongUnaryOperator mapper)
LongStream with elements that obtained
by applying the given IndexedLongUnaryOperator.
This is an intermediate operation.
Example:
from: -2 step: 2 mapper: (index, value) -> (index * value) stream: [ 1, 2, 3, 4] index: [-2, 0, 2, 4] result: [-2, 0, 6, 16]
from - the initial value of the index (inclusive)step - the step of the indexmapper - the mapper function used to apply to each element@NotNull public <R> Stream<R> mapToObj(@NotNull 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@NotNull public IntStream mapToInt(@NotNull 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@NotNull public DoubleStream mapToDouble(@NotNull 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@NotNull public LongStream flatMap(@NotNull 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)@NotNull public LongStream mapMulti(@NotNull LongStream.LongMapMultiConsumer mapper)
This is an intermediate operation.
mapper - the mapper function used to apply to each element for producing replacing elementsStream.mapMulti(com.annimon.stream.function.BiConsumer),
flatMap(com.annimon.stream.function.LongFunction)@NotNull public LongStream distinct()
This is a stateful intermediate operation.
Example:
stream: [1, 4, 2, 3, 3, 4, 1] result: [1, 4, 2, 3]
@NotNull public LongStream sorted()
This is a stateful intermediate operation.
Example:
stream: [3, 4, 1, 2] result: [1, 2, 3, 4]
@NotNull public LongStream sorted(@Nullable 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@NotNull public LongStream sample(int stepWidth)
LongStream 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 widthLongStreamjava.lang.IllegalArgumentException - if stepWidth is zero or negativeStream.sample(int)@NotNull public LongStream peek(@NotNull LongConsumer action)
This is an intermediate operation.
action - the action to be performed on each element@NotNull public LongStream scan(@NotNull 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 null@NotNull public LongStream scan(long identity, @NotNull 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 null@NotNull public LongStream takeWhile(@NotNull 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@NotNull public LongStream takeUntil(@NotNull 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@NotNull public LongStream dropWhile(@NotNull 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@NotNull 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 negative@NotNull public 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(@NotNull
LongConsumer action)
This is a terminal operation.
action - the action to be performed on each elementpublic void forEachIndexed(@NotNull
IndexedLongConsumer action)
This is a terminal operation.
action - the action to be performed on each elementpublic void forEachIndexed(int from,
int step,
@NotNull
IndexedLongConsumer action)
This is a terminal operation.
from - the initial value of the index (inclusive)step - the step of the indexaction - the action to be performed on each elementpublic long reduce(long identity,
@NotNull
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
@NotNull public OptionalLong reduce(@NotNull 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)@NotNull public long[] toArray()
This is a terminal operation.
@Nullable
public <R> R collect(@NotNull
Supplier<R> supplier,
@NotNull
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()
@NotNull 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.
@NotNull 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 long count()
This is a terminal operation.
public boolean anyMatch(@NotNull
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 falsepublic boolean allMatch(@NotNull
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 falsepublic boolean noneMatch(@NotNull
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@NotNull 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 long findFirstOrElse(long other)
other.
This is a short-circuiting terminal operation.
other - the value to be returned if stream is emptyother if stream is empty@NotNull public 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 long 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 element@NotNull public OptionalLong findSingle()
OptionalLong class.
If stream is empty, returns OptionalLong.empty().
If stream contains more than one element, throws IllegalStateException.
This is a short-circuiting terminal operation.
Example:
stream: [] result: OptionalLong.empty() stream: [1] result: OptionalLong.of(1) stream: [1, 2, 3] result: IllegalStateException
OptionalLong with single element
or OptionalLong.empty() if stream is emptyjava.lang.IllegalStateException - if stream contains more than one element@NotNull public LongStream onClose(@NotNull 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.Closeableclose in interface java.lang.AutoCloseable