O
- The streams element type.public static class Streams.FailableStream<O> extends Object
Stream
with
failable method signatures.Constructor and Description |
---|
FailableStream(Stream<O> stream)
Constructs a new instance with the given
stream . |
Modifier and Type | Method and Description |
---|---|
boolean |
allMatch(Functions.FailablePredicate<O,?> predicate)
Returns whether all elements of this stream match the provided predicate.
|
boolean |
anyMatch(Functions.FailablePredicate<O,?> predicate)
Returns whether any elements of this stream match the provided
predicate.
|
protected void |
assertNotTerminated() |
<A,R> R |
collect(Collector<? super O,A,R> collector)
Performs a mutable reduction operation on the elements of this stream using a
Collector . |
<A,R> R |
collect(Supplier<R> pupplier,
BiConsumer<R,? super O> accumulator,
BiConsumer<R,R> combiner)
Performs a mutable reduction operation on the elements of this FailableStream.
|
Streams.FailableStream<O> |
filter(Functions.FailablePredicate<O,?> predicate)
Returns a FailableStream consisting of the elements of this stream that match
the given FailablePredicate.
|
void |
forEach(Functions.FailableConsumer<O,?> action)
Performs an action for each element of this stream.
|
protected void |
makeTerminated() |
<R> Streams.FailableStream<R> |
map(Functions.FailableFunction<O,R,?> mapper)
Returns a stream consisting of the results of applying the given
function to the elements of this stream.
|
O |
reduce(O identity,
BinaryOperator<O> 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.
|
Stream<O> |
stream()
Converts the FailableStream into an equivalent stream.
|
public FailableStream(Stream<O> stream)
stream
.stream
- The stream.protected void assertNotTerminated()
protected void makeTerminated()
public Streams.FailableStream<O> filter(Functions.FailablePredicate<O,?> predicate)
This is an intermediate operation.
predicate
- a non-interfering, stateless predicate to apply to each
element to determine if it should be included.public void forEach(Functions.FailableConsumer<O,?> action)
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.
action
- a non-interfering action to perform on the elementspublic <A,R> R collect(Collector<? super O,A,R> collector)
Collector
. A Collector
encapsulates the functions used as arguments to
collect(Supplier, BiConsumer, BiConsumer)
, allowing for reuse of
collection strategies and composition of collect operations such as
multiple-level grouping or partitioning.
If the underlying 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.
\@apiNote
The following will accumulate strings into an ArrayList:
List<String> asList = stringStream.collect(Collectors.toList());
The following will classify Person
objects by city:
Map<String, List<Person>> peopleByCity
= personStream.collect(Collectors.groupingBy(Person::getCity));
The following will classify Person
objects by state and city,
cascading two Collector
s together:
Map<String, Map<String, List<Person>>> peopleByStateAndCity
= personStream.collect(Collectors.groupingBy(Person::getState,
Collectors.groupingBy(Person::getCity)));
R
- the type of the resultA
- the intermediate accumulation type of the Collector
collector
- the Collector
describing the reductioncollect(Supplier, BiConsumer, BiConsumer)
,
Collectors
public <A,R> R collect(Supplier<R> pupplier, BiConsumer<R,? super O> accumulator, BiConsumer<R,R> combiner)
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 reduce(Object, BinaryOperator)
, collect
operations
can be parallelized without requiring additional synchronization.
This is a terminal operation.
\@apiNote There are many existing classes in the JDK whose signatures are
well-suited for use with method references as arguments to collect()
.
For example, the following will accumulate strings into an ArrayList
:
List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add,
ArrayList::addAll);
The following will take a stream of strings and concatenates them into a single string:
String concat = stringStream.collect(StringBuilder::new, StringBuilder::append,
StringBuilder::append)
.toString();
R
- type of the resultA
- Type of the accumulator.pupplier
- 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 O reduce(O identity, BinaryOperator<O> accumulator)
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. \@apiNote Sum, min, max, average, and string concatenation are all special cases of reduction. Summing a stream of numbers can be expressed as:
Integer sum = integers.reduce(0, (a, b) -> a+b);
or:
Integer sum = integers.reduce(0, Integer::sum);
While this may seem a more roundabout way to perform an aggregation compared to simply mutating a running total in a loop, reduction operations parallelize more gracefully, without needing additional synchronization and with greatly reduced risk of data races.
identity
- the identity value for the accumulating functionaccumulator
- an associative, non-interfering, stateless
function for combining two valuespublic <R> Streams.FailableStream<R> map(Functions.FailableFunction<O,R,?> mapper)
This is an intermediate operation.
R
- The element type of the new streammapper
- A non-interfering, stateless function to apply to each elementpublic Stream<O> stream()
public boolean allMatch(Functions.FailablePredicate<O,?> predicate)
true
is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
\@apiNote
This method evaluates the universal quantification of the
predicate over the elements of the stream (for all x P(x)). If the
stream is empty, the quantification is said to be vacuously
satisfied and is always true
(regardless of P(x)).
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 boolean anyMatch(Functions.FailablePredicate<O,?> predicate)
false
is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation. \@apiNote This method evaluates the existential quantification of the predicate over the elements of the stream (for some x P(x)).
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
Copyright © 2001–2020 The Apache Software Foundation. All rights reserved.