public final class LongStreams extends Object
takeWhile()
,
dropWhile()
) in the LongStream
interface.Modifier and Type | Method and Description |
---|---|
static LongStream.Builder |
builder()
Returns a builder for a
LongStream . |
static LongStream |
concat(LongStream a,
LongStream 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.
|
static LongStream |
dropWhile(LongStream stream,
LongPredicate predicate)
Returns a stream consisting of the remaining elements of the passed
stream after discarding elements that match the given predicate up to,
but not discarding, the first element encountered that does not match
the predicate.
|
static LongStream |
empty()
Returns an empty sequential
LongStream . |
static LongStream |
generate(LongSupplier s)
Returns an infinite sequential unordered stream where each element is
generated by the provided
LongSupplier . |
static LongStream |
iterate(long seed,
LongPredicate hasNext,
LongUnaryOperator next)
Returns a sequential ordered
LongStream produced by iterative
application of the given next function to an initial element,
conditioned on satisfying the given hasNext predicate. |
static LongStream |
iterate(long seed,
LongUnaryOperator f)
Returns an infinite sequential ordered
LongStream 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. |
static LongStream |
of(long... values)
Returns a sequential ordered stream whose elements are the specified values.
|
static LongStream |
of(long t)
Returns a sequential
LongStream containing a single element. |
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 . |
static LongStream |
takeWhile(LongStream stream,
LongPredicate predicate)
Returns a stream consisting of elements of the passed stream that match
the given predicate up to, but discarding, the first element encountered
that does not match the predicate.
|
public static LongStream takeWhile(LongStream stream, LongPredicate predicate)
stream
- the stream to wrap for the takeWhile()
operationpredicate
- a non-interfering,
predicate to apply to each element to determine if it
should be included, or it and all subsequently
encountered elements be discarded.public static LongStream dropWhile(LongStream stream, LongPredicate predicate)
This is a stateful intermediate operation.
stream
- the stream to wrap for the dropWhile()
operationpredicate
- a non-interfering,
predicate to apply to each element to determine if it
should be discarded, or it and all subsequently
encountered elements be included.public static LongStream.Builder builder()
LongStream
.public static LongStream empty()
LongStream
.public static LongStream of(long t)
LongStream
containing a single element.t
- the single elementpublic static LongStream of(long... values)
values
- the elements of the new streampublic static LongStream iterate(long seed, LongUnaryOperator f)
LongStream
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 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
.
The action of applying f
for one element
happens-before
the action of applying f
for subsequent elements. For any given
element the action may be performed in whatever thread the library
chooses.
seed
- the initial elementf
- a function to be applied to the previous element to produce
a new elementLongStream
public static LongStream iterate(long seed, LongPredicate hasNext, LongUnaryOperator next)
LongStream
produced by iterative
application of the given next
function to an initial element,
conditioned on satisfying the given hasNext
predicate. The
stream terminates as soon as the hasNext
predicate returns false.
LongStreams.iterate
should produce the same sequence of elements
as produced by the corresponding for-loop:
for (long index=seed; hasNext.test(index); index = next.applyAsLong(index)) {
...
}
The resulting sequence may be empty if the hasNext
predicate
does not hold on the seed value. Otherwise the first element will be the
supplied seed
value, the next element (if present) will be the
result of applying the next
function to the seed
value,
and so on iteratively until the hasNext
predicate indicates that
the stream should terminate.
The action of applying the hasNext
predicate to an element
happens-before
the action of applying the next
function to that element. The
action of applying the next
function for one element
happens-before the action of applying the hasNext
predicate for subsequent elements. For any given element an action may
be performed in whatever thread the library chooses.
seed
- the initial elementhasNext
- a predicate to apply to elements to determine when the
stream must terminatenext
- a function to be applied to the previous element to produce
a new elementLongStream
public static LongStream generate(LongSupplier s)
LongSupplier
. This is suitable for
generating constant streams, streams of random elements, etc.s
- the LongSupplier
for generated elementsLongStream
public static LongStream range(long startInclusive, long endExclusive)
LongStream
from startInclusive
(inclusive) to endExclusive
(exclusive) by an incremental step of
1
.
API Note:
An equivalent sequence of increasing values can be produced
sequentially using a for
loop as follows:
for (long i = startInclusive; i < endExclusive ; i++) { ... }
startInclusive
- the (inclusive) initial valueendExclusive
- the exclusive upper boundLongStream
for the range of long
elementspublic static LongStream rangeClosed(long startInclusive, long endInclusive)
LongStream
from startInclusive
(inclusive) to endInclusive
(inclusive) by an incremental step of
1
.
API Note:
An equivalent sequence of increasing values can be produced
sequentially using a for
loop as follows:
for (long i = startInclusive; i <= endInclusive ; i++) { ... }
startInclusive
- the (inclusive) initial valueendInclusive
- the inclusive upper boundLongStream
for the range of long
elementspublic static LongStream concat(LongStream a, LongStream b)
This method operates on the two input streams and binds each stream to its source. As a result subsequent modifications to an input stream source may not be reflected in the concatenated stream result.
Implementation Note:
Use caution when constructing streams from repeated concatenation.
Accessing an element of a deeply concatenated stream can result in deep
call chains, or even StackOverflowError
.
Subsequent changes to the sequential/parallel execution mode of the returned stream are not guaranteed to be propagated to the input streams.
API Note:
To preserve optimization opportunities this method binds each stream to
its source and accepts only two streams as parameters. For example, the
exact size of the concatenated stream source can be computed if the exact
size of each input stream source is known.
To concatenate more streams without binding, or without nested calls to
this method, try creating a stream of streams and flat-mapping with the
identity function, for example:
LongStream concat = RefStreams.of(s1, s2, s3, s4).flatMapToLong(s -> s);
a
- the first streamb
- the second streamCopyright © 2020. All rights reserved.