Modifier and Type | Method and Description |
---|---|
static <T> Stream.Builder<T> |
builder()
Returns a builder for a
Stream . |
static <T> Stream<T> |
concat(Stream<? extends T> a,
Stream<? extends T> 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 <T> Stream<T> |
empty()
Returns an empty sequential
Stream . |
static <T> Stream<T> |
generate(Supplier<? extends T> s)
Returns an infinite sequential unordered
Stream where each
element is generated by the provided Supplier . |
static <T,S extends T> |
iterate(S seed,
Predicate<? super S> hasNext,
UnaryOperator<S> next)
Returns a sequential ordered
Stream produced by iterative
application of the given next function to an initial element,
conditioned on satisfying the given hasNext predicate. |
static <T,S extends T> |
iterate(S seed,
UnaryOperator<S> f)
Returns an infinite sequential ordered
Stream 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 <T> Stream<T> |
of(T... values)
Returns a sequential ordered
Stream whose elements are the
specified values. |
static <T> Stream<T> |
of(T t)
Returns a sequential
Stream containing a single element. |
static <T> Stream<T> |
ofNullable(T t)
Returns a sequential
Stream containing a single element, if
non-null, otherwise returns an empty Stream . |
public static <T> Stream.Builder<T> builder()
Stream
.T
- type of elementspublic static <T> Stream<T> empty()
Stream
.T
- the type of stream elementspublic static <T> Stream<T> of(T t)
Stream
containing a single element.T
- the type of stream elementst
- the single elementpublic static <T> Stream<T> ofNullable(T t)
Stream
containing a single element, if
non-null, otherwise returns an empty Stream
.T
- the type of stream elementst
- the single elementpublic static <T> Stream<T> of(T... values)
Stream
whose elements are the
specified values.T
- the type of stream elementsvalues
- the elements of the new streampublic static <T,S extends T> Stream<T> iterate(S seed, UnaryOperator<S> f)
Stream
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 Stream
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.
S
- the type of the operand and seed, a subtype of TT
- the type of stream elementsseed
- the initial elementf
- a function to be applied to the previous element to produce
a new elementStream
public static <T,S extends T> Stream<T> iterate(S seed, Predicate<? super S> hasNext, UnaryOperator<S> next)
Stream
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.
RefStreams.iterate
should produce the same sequence of elements as
produced by the corresponding for-loop:
for (T index=seed; hasNext.test(index); index = next.apply(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.
S
- the type of the operand, predicate input and seed, a subtype of TT
- the type of stream elementsseed
- 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 elementStream
public static <T> Stream<T> generate(Supplier<? extends T> s)
Stream
where each
element is generated by the provided Supplier
. This is
suitable for generating constant streams, streams of random elements,
etc.T
- the type of stream elementss
- the Supplier
of generated elementsStream
public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b)
Stream
whose elements are all the
elements of the first stream followed by all the elements of the
second stream. The resulting stream is ordered if both
of the input streams are ordered, and parallel if either of the input
streams is parallel. When the resulting stream is closed, the close
handlers for both input streams are invoked.
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:
Stream<T> concat = RefStreams.of(s1, s2, s3, s4).flatMap(s -> s);
T
- The type of stream elementsa
- the first streamb
- the second streamCopyright © 2021. All rights reserved.