Modifier and Type | Method and Description |
---|---|
static <T> Stream.Builder<T> |
add(Stream.Builder<T> builder,
T t)
Adds an element to the
Stream being built represented by the
Stream.Builder argument. |
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<T> s)
Returns an infinite sequential unordered
Stream where each
element is generated by the provided Supplier . |
static <T> Stream<T> |
iterate(T seed,
UnaryOperator<T> 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> Stream<T> iterate(T seed, UnaryOperator<T> 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
.
T
- 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> Stream<T> generate(Supplier<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
.
T
- The type of stream elementsa
- the first streamb
- the second streampublic static <T> Stream.Builder<T> add(Stream.Builder<T> builder, T t)
Stream
being built represented by the
Stream.Builder
argument.
Implementation Requirements:
The default implementation behaves as if:
builder.accept(t)
return builder;
T
- the type of stream elementsbuilder
- the Stream.Builder
to uset
- the element to addIllegalStateException
- if the builder has already transitioned to
the built stateCopyright © 2015. All rights reserved.