Class Streams.FailableStream<T>
 Type Parameters:
T
 The streams element type.
 Enclosing class:
 Streams
Stream
with failable method signatures.
Constructor Summary
ConstructorDescriptionFailableStream
(Stream<T> stream) Constructs a new instance with the givenstream
. 
Method Summary
Modifier and TypeMethodDescriptionboolean
allMatch
(FailablePredicate<T, ?> predicate) Returns whether all elements of this stream match the provided predicate.boolean
anyMatch
(FailablePredicate<T, ?> predicate) Returns whether any elements of this stream match the provided predicate.protected void
Throws IllegalStateException if this stream is already terminated.<A,
R> R collect
(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) Performs a mutable reduction operation on the elements of this FailableStream.<A,
R> R Performs a mutable reduction operation on the elements of this stream using aCollector
.filter
(FailablePredicate<T, ?> predicate) Returns a FailableStream consisting of the elements of this stream that match the given FailablePredicate.void
forEach
(FailableConsumer<T, ?> action) Performs an action for each element of this stream.protected void
Marks this stream as terminated.<R> Streams.FailableStream<R>
map
(FailableFunction<T, R, ?> mapper) Returns a stream consisting of the results of applying the given function to the elements of this stream.reduce
(T identity, BinaryOperator<T> 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()
Converts the FailableStream into an equivalent stream.

Constructor Details

FailableStream
Constructs a new instance with the givenstream
. Parameters:
stream
 The stream.


Method Details

allMatch
Returns whether all elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty thentrue
is returned and the predicate is not evaluated.This is a shortcircuiting terminal operation.
Note 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 alwaystrue
(regardless of P(x)). Parameters:
predicate
 A noninterfering, stateless predicate to apply to elements of this stream Returns:
true
If either all elements of the stream match the provided predicate or the stream is empty, otherwisefalse
.

anyMatch
Returns whether any elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty thenfalse
is returned and the predicate is not evaluated.This is a shortcircuiting terminal operation.
Note This method evaluates the existential quantification of the predicate over the elements of the stream (for some x P(x)). Parameters:
predicate
 A noninterfering, stateless predicate to apply to elements of this stream Returns:
true
if any elements of the stream match the provided predicate, otherwisefalse

assertNotTerminated
Throws IllegalStateException if this stream is already terminated. Throws:
IllegalStateException
 if this stream is already terminated.

collect
Performs a mutable reduction operation on the elements of this stream using aCollector
. ACollector
encapsulates the functions used as arguments tocollect(Supplier, BiConsumer, BiConsumer)
, allowing for reuse of collection strategies and composition of collect operations such as multiplelevel 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 (seeCollector
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 nonthreadsafe data structures (such as
Note The following will accumulate strings into an ArrayList:ArrayList
), no additional synchronization is needed for a parallel reduction.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 twoCollector
s together:Map<String, Map<String, List<Person>>> peopleByStateAndCity = personStream .collect(Collectors.groupingBy(Person::getState, Collectors.groupingBy(Person::getCity)));

collect
public <A,R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) Performs a mutable reduction operation on the elements of this FailableStream. A mutable reduction is one in which the reduced value is a mutable result container, such as anArrayList
, 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.
Note There are many existing classes in the JDK whose signatures are wellsuited for use with method references as arguments to
collect()
. For example, the following will accumulate strings into anArrayList
: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();
 Type Parameters:
R
 type of the resultA
 Type of the accumulator. Parameters:
supplier
 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, noninterfering, stateless function for incorporating an additional element into a resultcombiner
 An associative, noninterfering, stateless function for combining two values, which must be compatible with the accumulator function Returns:
 The result of the reduction

filter
Returns a FailableStream consisting of the elements of this stream that match the given FailablePredicate.This is an intermediate operation.
 Parameters:
predicate
 a noninterfering, stateless predicate to apply to each element to determine if it should be included. Returns:
 the new stream

forEach
Performs an action for each element of this 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.
 Parameters:
action
 a noninterfering action to perform on the elements

makeTerminated
Marks this stream as terminated. Throws:
IllegalStateException
 if this stream is already terminated.

map
Returns a stream consisting of the results of applying the given function to the elements of this stream.This is an intermediate operation.
 Type Parameters:
R
 The element type of the new stream Parameters:
mapper
 A noninterfering, stateless function to apply to each element Returns:
 the new stream

reduce
Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value. This is equivalent to:T result = identity; for (T element : this stream) result = accumulator.apply(result, element) return result;
The
identity
value must be an identity for the accumulator function. This means that for allt
,accumulator.apply(identity, t)
is equal tot
. Theaccumulator
function must be an associative function.This is a terminal operation.
Note 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);
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.
 Parameters:
identity
 the identity value for the accumulating functionaccumulator
 an associative, noninterfering, stateless function for combining two values Returns:
 the result of the reduction

stream
Converts the FailableStream into an equivalent stream. Returns:
 A stream, which will return the same elements, which this FailableStream would return.
