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> |
dropWhile(Stream<? extends T> stream,
Predicate<? super T> predicate)
Returns, if the passed stream is ordered, a stream consisting of the remaining
elements of the passed stream after dropping the longest prefix of elements
that match the given predicate.
|
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<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 . |
static <T> Stream<T> |
takeWhile(Stream<? extends T> stream,
Predicate<? super T> predicate)
Returns, if the passed stream is ordered, a stream consisting of the longest
prefix of elements taken from the passed stream that match the given predicate.
|
public static <T> Stream<T> takeWhile(Stream<? extends T> stream, Predicate<? super T> predicate)
If the passed stream is ordered then the longest prefix is a contiguous sequence of elements of the passed stream that match the given predicate. The first element of the sequence is the first element of the passed stream, and the element immediately following the last element of the sequence does not match the given predicate.
If the passed stream is unordered, and some (but not all) elements of the passed stream match the given predicate, then the behavior of this operation is nondeterministic; it is free to take any subset of matching elements (which includes the empty set).
Independent of whether the passed stream is ordered or unordered if all elements of the passed stream match the given predicate then this operation takes all elements (the result is the same as the input), or if no elements of the passed stream match the given predicate then no elements are taken (the result is an empty stream).
This is a short-circuiting stateful intermediate operation.
Implementation Requirements:
The default implementation obtains the spliterator
of the passed stream, wraps that spliterator so as to support the
semantics of this operation on traversal, and returns a new stream
associated with the wrapped spliterator. The returned stream preserves
the execution characteristics of the passed stream (namely parallel or
sequential execution as per isParallel()
)
but the wrapped spliterator may choose to not support splitting.
When the returned stream is closed, the close handlers for both
the returned and the passed stream are invoked.
API Note:
While takeWhile()
is generally a cheap operation on sequential
stream pipelines, it can be quite expensive on ordered parallel
pipelines, since the operation is constrained to return not just any
valid prefix, but the longest prefix of elements in the encounter order.
Using an unordered stream source (such as generate(Supplier)
)
or removing the ordering constraint with unordered()
may result in significant speedups of takeWhile()
in parallel
pipelines, if the semantics of your situation permit. If consistency
with encounter order is required, and you are experiencing poor
performance or memory utilization with takeWhile()
in parallel
pipelines, switching to sequential execution with
sequential()
may improve performance.
A use-case for stream cancellation is executing a stream pipeline for a certain duration. The following example will calculate as many probable primes as is possible, in parallel, during 5 seconds:
long t = System.currentTimeMillis();
List<BigInteger> pps = RefStreams
.generate(() -> BigInteger.probablePrime(1024, ThreadLocalRandom.current()))
.parallel()
.takeWhile(e -> (System.currentTimeMillis() - t) < TimeUnit.SECONDS.toMillis(5))
.collect(toList());
T
- the type of the stream elementsstream
- the stream to wrap for the takeWhile()
operationpredicate
- a non-interfering,
predicate to apply elements to determine the longest
prefix of elements.public static <T> Stream<T> dropWhile(Stream<? extends T> stream, Predicate<? super T> predicate)
If the passed stream is ordered then the longest prefix is a contiguous sequence of elements of the passed stream that match the given predicate. The first element of the sequence is the first element of the passed stream, and the element immediately following the last element of the sequence does not match the given predicate.
If the passed stream is unordered, and some (but not all) elements of the passed stream match the given predicate, then the behavior of this operation is nondeterministic; it is free to drop any subset of matching elements (which includes the empty set).
Independent of whether the passed stream is ordered or unordered if all elements of the passed stream match the given predicate then this operation drops all elements (the result is an empty stream), or if no elements of the passed stream match the given predicate then no elements are dropped (the result is the same as the input).
This is a stateful intermediate operation.
Implementation Requirements:
The default implementation obtains the spliterator
of the passed stream, wraps that spliterator so as to support the
semantics of this operation on traversal, and returns a new stream
associated with the wrapped spliterator. The returned stream preserves
the execution characteristics of the passed stream (namely parallel or
sequential execution as per isParallel()
)
but the wrapped spliterator may choose to not support splitting.
When the returned stream is closed, the close handlers for both
the returned and the passed stream are invoked.
API Note:
While dropWhile()
is generally a cheap operation on sequential
stream pipelines, it can be quite expensive on ordered parallel
pipelines, since the operation is constrained to return not just any
valid prefix, but the longest prefix of elements in the encounter order.
Using an unordered stream source (such as generate(Supplier)
)
or removing the ordering constraint with unordered()
may result in significant speedups of dropWhile()
in parallel
pipelines, if the semantics of your situation permit. If consistency
with encounter order is required, and you are experiencing poor
performance or memory utilization with dropWhile()
in parallel
pipelines, switching to sequential execution with
sequential()
may improve performance.
T
- the type of the stream elementsstream
- the stream to wrap for the dropWhile()
operationpredicate
- a non-interfering,
stateless
predicate to apply elements to determine the longest
prefix of elements.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<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.
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.
T
- The type of stream elementsa
- the first streamb
- the second streamCopyright © 2016. All rights reserved.