Creating an Optional The Optional<T> class models the absence of a value by a special singleton returned by the Optional.empty()…
Querying an Optional The presence of a value in an Optional can be determined by the isPresent() method, and the…
16.7 Terminal Stream Operations A stream pipeline does not execute until a terminal operation is invoked on it; that is,…
Implementing Functional Reduction: The reduce() Method A functional reduction combines all elements in a stream to produce a single immutable…
Counting Elements The count() operation performs a functional reduction on the elements of a stream, as each element contributes to…
Implementing Mutable Reduction: The collect() Method The collect(Collector) method accepts a collector that encapsulates the functions required to perform a…
Finding the First or Any Element The findFirst() method can be used to find the first element that is available…
Parallel Mutable Reduction Figure 16.14 shows the stream pipeline from Figure 16.13, where the sequential stream (2a) has been replaced…
Grouping Classifying elements into groups based on some criteria is a very common operation. An example is classifying CDs into…
Grouping to a ConcurrentMap If the collector returned by the Collectors.groupingBy() method is used in a parallel stream, the partial…