P_IN
- type of elements in the upstream sourceP_OUT
- type of elements in produced by this stagepublic abstract class ReferencePipeline<P_IN,P_OUT> extends AbstractPipeline<P_IN,P_OUT,Stream<P_OUT>> implements Stream<P_OUT>
U
.Modifier and Type | Class and Description |
---|---|
static class |
ReferencePipeline.Head<E_IN,E_OUT>
Source stage of a ReferencePipeline.
|
static class |
ReferencePipeline.StatefulOp<E_IN,E_OUT>
Base class for a stateful intermediate stage of a Stream.
|
static class |
ReferencePipeline.StatelessOp<E_IN,E_OUT>
Base class for a stateless intermediate stage of a Stream.
|
Stream.Builder<T>
sourceOrOpFlags
Modifier and Type | Method and Description |
---|---|
boolean |
allMatch(Predicate<? super P_OUT> predicate)
Returns whether all elements of this stream match the provided predicate.
|
boolean |
anyMatch(Predicate<? super P_OUT> predicate)
Returns whether any elements of this stream match the provided
predicate.
|
<R,A> R |
collect(Collector<? super P_OUT,A,R> collector)
Performs a mutable
reduction operation on the elements of this stream using a
Collector . |
<R> R |
collect(Supplier<R> supplier,
BiConsumer<R,? super P_OUT> accumulator,
BiConsumer<R,R> combiner)
Performs a mutable
reduction operation on the elements of this stream.
|
long |
count()
Returns the count of elements in this stream.
|
Stream<P_OUT> |
distinct()
Returns a stream consisting of the distinct elements (according to
Object.equals(Object) ) of this stream. |
<P_IN> Node<P_OUT> |
evaluateToNode(PipelineHelper<P_OUT> helper,
Spliterator<P_IN> spliterator,
boolean flattenTree,
IntFunction<P_OUT[]> generator)
Collect elements output from a pipeline into a Node that holds elements
of this shape.
|
Stream<P_OUT> |
filter(Predicate<? super P_OUT> predicate)
Returns a stream consisting of the elements of this stream that match
the given predicate.
|
Optional<P_OUT> |
findAny()
Returns an
Optional describing some element of the stream, or an
empty Optional if the stream is empty. |
Optional<P_OUT> |
findFirst()
Returns an
Optional describing the first element of this stream,
or an empty Optional if the stream is empty. |
<R> Stream<R> |
flatMap(Function<? super P_OUT,? extends Stream<? extends R>> 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 |
flatMapToDouble(Function<? super P_OUT,? extends DoubleStream> mapper)
Returns an
DoubleStream 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. |
IntStream |
flatMapToInt(Function<? super P_OUT,? extends IntStream> mapper)
Returns an
IntStream 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. |
LongStream |
flatMapToLong(Function<? super P_OUT,? extends LongStream> mapper)
Returns an
LongStream 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(Consumer<? super P_OUT> action)
Performs an action for each element of this stream.
|
void |
forEachOrdered(Consumer<? super P_OUT> action)
Performs an action for each element of this stream, in the encounter
order of the stream if the stream has a defined encounter order.
|
void |
forEachWithCancel(Spliterator<P_OUT> spliterator,
Sink<P_OUT> sink)
Traverse the elements of a spliterator compatible with this stream shape,
pushing those elements into a sink.
|
StreamShape |
getOutputShape()
Get the output shape of the pipeline.
|
Iterator<P_OUT> |
iterator()
Returns an iterator for the elements of this stream.
|
Spliterator<P_OUT> |
lazySpliterator(Supplier<? extends Spliterator<P_OUT>> supplier)
Create a lazy spliterator that wraps and obtains the supplied the
spliterator when a method is invoked on the lazy spliterator.
|
Stream<P_OUT> |
limit(long maxSize)
Returns a stream consisting of the elements of this stream, truncated
to be no longer than
maxSize in length. |
Node.Builder<P_OUT> |
makeNodeBuilder(long exactSizeIfKnown,
IntFunction<P_OUT[]> generator)
Make a node builder compatible with this stream shape.
|
<R> Stream<R> |
map(Function<? super P_OUT,? extends R> mapper)
Returns a stream consisting of the results of applying the given
function to the elements of this stream.
|
DoubleStream |
mapToDouble(ToDoubleFunction<? super P_OUT> mapper)
Returns a
DoubleStream consisting of the results of applying the
given function to the elements of this stream. |
IntStream |
mapToInt(ToIntFunction<? super P_OUT> mapper)
Returns an
IntStream consisting of the results of applying the
given function to the elements of this stream. |
LongStream |
mapToLong(ToLongFunction<? super P_OUT> mapper)
Returns a
LongStream consisting of the results of applying the
given function to the elements of this stream. |
Optional<P_OUT> |
max(Comparator<? super P_OUT> comparator)
Returns the maximum element of this stream according to the provided
Comparator . |
Optional<P_OUT> |
min(Comparator<? super P_OUT> comparator)
Returns the minimum element of this stream according to the provided
Comparator . |
boolean |
noneMatch(Predicate<? super P_OUT> predicate)
Returns whether no elements of this stream match the provided predicate.
|
Stream<P_OUT> |
peek(Consumer<? super P_OUT> action)
Returns a stream consisting of the elements of this stream, additionally
performing the provided action on each element as elements are consumed
from the resulting stream.
|
Optional<P_OUT> |
reduce(BinaryOperator<P_OUT> accumulator)
Performs a reduction on the
elements of this stream, using an
associative accumulation
function, and returns an
Optional describing the reduced value,
if any. |
P_OUT |
reduce(P_OUT identity,
BinaryOperator<P_OUT> 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.
|
<R> R |
reduce(R identity,
BiFunction<R,? super P_OUT,R> accumulator,
BinaryOperator<R> combiner)
Performs a reduction on the
elements of this stream, using the provided identity, accumulation and
combining functions.
|
Stream<P_OUT> |
skip(long n)
Returns a stream consisting of the remaining elements of this stream
after discarding the first
n elements of the stream. |
Stream<P_OUT> |
sorted()
Returns a stream consisting of the elements of this stream, sorted
according to natural order.
|
Stream<P_OUT> |
sorted(Comparator<? super P_OUT> comparator)
Returns a stream consisting of the elements of this stream, sorted
according to the provided
Comparator . |
Object[] |
toArray()
Returns an array containing the elements of this stream.
|
<A> A[] |
toArray(IntFunction<A[]> generator)
Returns an array containing the elements of this stream, using the
provided
generator function to allocate the returned array, as
well as any additional arrays that might be required for a partitioned
execution or for resizing. |
Stream<P_OUT> |
unordered()
Returns an equivalent stream that is
unordered.
|
<P_IN> Spliterator<P_OUT> |
wrap(PipelineHelper<P_OUT> ph,
Supplier<Spliterator<P_IN>> supplier,
boolean isParallel)
Create a spliterator that wraps a source spliterator, compatible with
this stream shape, and operations associated with a
PipelineHelper . |
close, evaluate, evaluateToArrayNode, getStreamAndOpFlags, getStreamFlags, isParallel, onClose, opEvaluateParallel, opEvaluateParallelLazy, opIsStateful, opWrapSink, parallel, sequential, spliterator, wrapSink
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
builder, concat, empty, generate, iterate, of, of
close, isParallel, onClose, parallel, sequential, spliterator
public final StreamShape getOutputShape()
AbstractPipeline
getOutputShape
in class AbstractPipeline<P_IN,P_OUT,Stream<P_OUT>>
public final <P_IN> Node<P_OUT> evaluateToNode(PipelineHelper<P_OUT> helper, Spliterator<P_IN> spliterator, boolean flattenTree, IntFunction<P_OUT[]> generator)
AbstractPipeline
evaluateToNode
in class AbstractPipeline<P_IN,P_OUT,Stream<P_OUT>>
helper
- the pipeline helper describing the pipeline stagesspliterator
- the source spliteratorflattenTree
- true if the returned node should be flattenedgenerator
- the array generatorpublic final <P_IN> Spliterator<P_OUT> wrap(PipelineHelper<P_OUT> ph, Supplier<Spliterator<P_IN>> supplier, boolean isParallel)
AbstractPipeline
PipelineHelper
.public final Spliterator<P_OUT> lazySpliterator(Supplier<? extends Spliterator<P_OUT>> supplier)
AbstractPipeline
lazySpliterator
in class AbstractPipeline<P_IN,P_OUT,Stream<P_OUT>>
supplier
- the supplier of a spliteratorpublic final void forEachWithCancel(Spliterator<P_OUT> spliterator, Sink<P_OUT> sink)
AbstractPipeline
forEachWithCancel
in class AbstractPipeline<P_IN,P_OUT,Stream<P_OUT>>
spliterator
- the spliterator to pull elements fromsink
- the sink to push elements topublic final Node.Builder<P_OUT> makeNodeBuilder(long exactSizeIfKnown, IntFunction<P_OUT[]> generator)
AbstractPipeline
makeNodeBuilder
in class AbstractPipeline<P_IN,P_OUT,Stream<P_OUT>>
exactSizeIfKnown
- if >=0, then a node builder will be
created that has a fixed capacity of at most sizeIfKnown elements. If
< 0, then the node builder has an unfixed capacity. A fixed
capacity node builder will throw exceptions if an element is added after
builder has reached capacity, or is built before the builder has reached
capacity.generator
- the array generator to be used to create instances of a
T[] array. For implementations supporting primitive nodes, this parameter
may be ignored.public final Iterator<P_OUT> iterator()
BaseStream
This is a terminal operation.
iterator
in interface BaseStream<P_OUT,Stream<P_OUT>>
public Stream<P_OUT> unordered()
BaseStream
This is an intermediate operation.
unordered
in interface BaseStream<P_OUT,Stream<P_OUT>>
public final Stream<P_OUT> filter(Predicate<? super P_OUT> predicate)
Stream
This is an intermediate operation.
filter
in interface Stream<P_OUT>
predicate
- a non-interfering,
stateless
predicate to apply to each element to determine if it
should be includedpublic final <R> Stream<R> map(Function<? super P_OUT,? extends R> mapper)
Stream
This is an intermediate operation.
map
in interface Stream<P_OUT>
R
- The element type of the new streammapper
- a non-interfering,
stateless
function to apply to each elementpublic final IntStream mapToInt(ToIntFunction<? super P_OUT> mapper)
Stream
IntStream
consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToInt
in interface Stream<P_OUT>
mapper
- a non-interfering,
stateless
function to apply to each elementpublic final LongStream mapToLong(ToLongFunction<? super P_OUT> mapper)
Stream
LongStream
consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToLong
in interface Stream<P_OUT>
mapper
- a non-interfering,
stateless
function to apply to each elementpublic final DoubleStream mapToDouble(ToDoubleFunction<? super P_OUT> mapper)
Stream
DoubleStream
consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToDouble
in interface Stream<P_OUT>
mapper
- a non-interfering,
stateless
function to apply to each elementpublic final <R> Stream<R> flatMap(Function<? super P_OUT,? extends Stream<? extends R>> mapper)
Stream
closed
after its contents
have been placed into this stream. (If a mapped stream is null
an empty stream is used, instead.)
This is an intermediate operation.
flatMap
in interface Stream<P_OUT>
R
- The element type of the new streammapper
- a non-interfering,
stateless
function to apply to each element which produces a stream
of new valuespublic final IntStream flatMapToInt(Function<? super P_OUT,? extends IntStream> mapper)
Stream
IntStream
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. Each mapped
stream is closed
after its
contents have been placed into this stream. (If a mapped stream is
null
an empty stream is used, instead.)
This is an intermediate operation.
flatMapToInt
in interface Stream<P_OUT>
mapper
- a non-interfering,
stateless
function to apply to each element which produces a stream
of new valuesStream.flatMap(Function)
public final DoubleStream flatMapToDouble(Function<? super P_OUT,? extends DoubleStream> mapper)
Stream
DoubleStream
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. Each mapped
stream is closed
after its
contents have placed been into this stream. (If a mapped stream is
null
an empty stream is used, instead.)
This is an intermediate operation.
flatMapToDouble
in interface Stream<P_OUT>
mapper
- a non-interfering,
stateless
function to apply to each element which produces a stream
of new valuesStream.flatMap(Function)
public final LongStream flatMapToLong(Function<? super P_OUT,? extends LongStream> mapper)
Stream
LongStream
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. Each mapped
stream is closed
after its
contents have been placed into this stream. (If a mapped stream is
null
an empty stream is used, instead.)
This is an intermediate operation.
flatMapToLong
in interface Stream<P_OUT>
mapper
- a non-interfering,
stateless
function to apply to each element which produces a stream
of new valuesStream.flatMap(Function)
public final Stream<P_OUT> peek(Consumer<? super P_OUT> action)
Stream
This is an intermediate operation.
For parallel stream pipelines, the action may be called at whatever time and in whatever thread the element is made available by the upstream operation. If the action modifies shared state, it is responsible for providing the required synchronization.
peek
in interface Stream<P_OUT>
action
- a
non-interfering action to perform on the elements as
they are consumed from the streampublic final Stream<P_OUT> distinct()
Stream
Object.equals(Object)
) of this stream.
For ordered streams, the selection of distinct elements is stable (for duplicated elements, the element appearing first in the encounter order is preserved.) For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
public final Stream<P_OUT> sorted()
Stream
Comparable
, a java.lang.ClassCastException
may be thrown
when the terminal operation is executed.
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
public final Stream<P_OUT> sorted(Comparator<? super P_OUT> comparator)
Stream
Comparator
.
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
sorted
in interface Stream<P_OUT>
comparator
- a non-interfering,
stateless
Comparator
to be used to compare stream elementspublic final Stream<P_OUT> limit(long maxSize)
Stream
maxSize
in length.
public final Stream<P_OUT> skip(long n)
Stream
n
elements of the stream.
If this stream contains fewer than n
elements then an
empty stream will be returned.
This is a stateful intermediate operation.
public void forEach(Consumer<? super P_OUT> action)
Stream
This is a terminal operation.
The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism. For any given element, the action may be performed at whatever time and in whatever thread the library chooses. If the action accesses shared state, it is responsible for providing the required synchronization.
forEach
in interface Stream<P_OUT>
action
- a
non-interfering action to perform on the elementspublic void forEachOrdered(Consumer<? super P_OUT> action)
Stream
This is a terminal operation.
This operation processes the elements one at a time, in encounter order if one exists. Performing the action for one element happens-before performing the action for subsequent elements, but for any given element, the action may be performed in whatever thread the library chooses.
forEachOrdered
in interface Stream<P_OUT>
action
- a
non-interfering action to perform on the elementsStream.forEach(Consumer)
public final <A> A[] toArray(IntFunction<A[]> generator)
Stream
generator
function to allocate the returned array, as
well as any additional arrays that might be required for a partitioned
execution or for resizing.
This is a terminal operation.
public final Object[] toArray()
Stream
This is a terminal operation.
public final boolean anyMatch(Predicate<? super P_OUT> predicate)
Stream
false
is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
anyMatch
in interface Stream<P_OUT>
predicate
- a non-interfering,
stateless
predicate to apply to elements of this streamtrue
if any elements of the stream match the provided
predicate, otherwise false
public final boolean allMatch(Predicate<? super P_OUT> predicate)
Stream
true
is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
allMatch
in interface Stream<P_OUT>
predicate
- a non-interfering,
stateless
predicate to apply to elements of this streamtrue
if either all elements of the stream match the
provided predicate or the stream is empty, otherwise false
public final boolean noneMatch(Predicate<? super P_OUT> predicate)
Stream
true
is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
noneMatch
in interface Stream<P_OUT>
predicate
- a non-interfering,
stateless
predicate to apply to elements of this streamtrue
if either no elements of the stream match the
provided predicate or the stream is empty, otherwise false
public final Optional<P_OUT> findFirst()
Stream
Optional
describing the first element of this stream,
or an empty Optional
if the stream is empty. If the stream has
no encounter order, then any element may be returned.
This is a short-circuiting terminal operation.
public final Optional<P_OUT> findAny()
Stream
Optional
describing some element of the stream, or an
empty Optional
if the stream is empty.
This is a short-circuiting terminal operation.
The behavior of this operation is explicitly nondeterministic; it is
free to select any element in the stream. This is to allow for maximal
performance in parallel operations; the cost is that multiple invocations
on the same source may not return the same result. (If a stable result
is desired, use Stream.findFirst()
instead.)
findAny
in interface Stream<P_OUT>
Optional
describing some element of this stream, or an
empty Optional
if the stream is emptyStream.findFirst()
public final P_OUT reduce(P_OUT identity, BinaryOperator<P_OUT> accumulator)
Stream
T result = identity;
for (T element : this stream)
result = accumulator.apply(result, element)
return result;
but is not constrained to execute sequentially.
The identity
value must be an identity for the accumulator
function. This means that for all t
,
accumulator.apply(identity, t)
is equal to t
.
The accumulator
function must be an
associative function.
This is a terminal operation.
reduce
in interface Stream<P_OUT>
identity
- the identity value for the accumulating functionaccumulator
- an associative,
non-interfering,
stateless
function for combining two valuespublic final Optional<P_OUT> reduce(BinaryOperator<P_OUT> accumulator)
Stream
Optional
describing the reduced value,
if any. This is equivalent to:
boolean foundAny = false;
T result = null;
for (T element : this stream) {
if (!foundAny) {
foundAny = true;
result = element;
}
else
result = accumulator.apply(result, element);
}
return foundAny ? Optional.of(result) : Optional.empty();
but is not constrained to execute sequentially.
The accumulator
function must be an
associative function.
This is a terminal operation.
reduce
in interface Stream<P_OUT>
accumulator
- an associative,
non-interfering,
stateless
function for combining two valuesOptional
describing the result of the reductionStream.reduce(Object, BinaryOperator)
,
Stream.min(Comparator)
,
Stream.max(Comparator)
public final <R> R reduce(R identity, BiFunction<R,? super P_OUT,R> accumulator, BinaryOperator<R> combiner)
Stream
U result = identity;
for (T element : this stream)
result = accumulator.apply(result, element)
return result;
but is not constrained to execute sequentially.
The identity
value must be an identity for the combiner
function. This means that for all u
, combiner(identity, u)
is equal to u
. Additionally, the combiner
function
must be compatible with the accumulator
function; for all
u
and t
, the following must hold:
combiner.apply(u, accumulator.apply(identity, t)) == accumulator.apply(u, t)
This is a terminal operation.
reduce
in interface Stream<P_OUT>
R
- The type of the resultidentity
- the identity value for the combiner functionaccumulator
- an associative,
non-interfering,
stateless
function for incorporating an additional element into a resultcombiner
- an associative,
non-interfering,
stateless
function for combining two values, which must be
compatible with the accumulator functionStream.reduce(BinaryOperator)
,
Stream.reduce(Object, BinaryOperator)
public final <R,A> R collect(Collector<? super P_OUT,A,R> collector)
Stream
Collector
. A Collector
encapsulates the functions used as arguments to
Stream.collect(Supplier, BiConsumer, BiConsumer)
, allowing for reuse of
collection strategies and composition of collect operations such as
multiple-level grouping or partitioning.
If the stream is parallel, and the Collector
is concurrent
, and
either the stream is unordered or the collector is
unordered
,
then a concurrent reduction will be performed (see Collector
for
details on concurrent reduction.)
This is a terminal operation.
When executed in parallel, multiple intermediate results may be
instantiated, populated, and merged so as to maintain isolation of
mutable data structures. Therefore, even when executed in parallel
with non-thread-safe data structures (such as ArrayList
), no
additional synchronization is needed for a parallel reduction.
collect
in interface Stream<P_OUT>
R
- the type of the resultA
- the intermediate accumulation type of the Collector
collector
- the Collector
describing the reductionStream.collect(Supplier, BiConsumer, BiConsumer)
,
Collectors
public final <R> R collect(Supplier<R> supplier, BiConsumer<R,? super P_OUT> accumulator, BiConsumer<R,R> combiner)
Stream
ArrayList
, and elements are incorporated by updating
the state of the result rather than by replacing the result. This
produces a result equivalent to:
R result = supplier.get();
for (T element : this stream)
accumulator.accept(result, element);
return result;
Like Stream.reduce(Object, BinaryOperator)
, collect
operations
can be parallelized without requiring additional synchronization.
This is a terminal operation.
collect
in interface Stream<P_OUT>
R
- type of the resultsupplier
- a function that creates a new result container. For a
parallel execution, this function may be called
multiple times and must return a fresh value each time.accumulator
- an associative,
non-interfering,
stateless
function for incorporating an additional element into a resultcombiner
- an associative,
non-interfering,
stateless
function for combining two values, which must be
compatible with the accumulator functionpublic final Optional<P_OUT> max(Comparator<? super P_OUT> comparator)
Stream
Comparator
. This is a special case of a
reduction.
This is a terminal operation.
max
in interface Stream<P_OUT>
comparator
- a non-interfering,
stateless
Comparator
to compare elements of this streamOptional
describing the maximum element of this stream,
or an empty Optional
if the stream is emptypublic final Optional<P_OUT> min(Comparator<? super P_OUT> comparator)
Stream
Comparator
. This is a special case of a
reduction.
This is a terminal operation.
min
in interface Stream<P_OUT>
comparator
- a non-interfering,
stateless
Comparator
to compare elements of this streamOptional
describing the minimum element of this stream,
or an empty Optional
if the stream is emptypublic final long count()
Stream
return mapToLong(e -> 1L).sum();
This is a terminal operation.