Section 17.4.2 Sorting a Stream and Collecting the Results

Stream method sorted (p. 745) sorts a stream’s elements into ascending order by default.

• To create a collection containing a stream pipeline’s results, you can use Stream method collect (a terminal operation). As the stream pipeline is processed, method collect performs a mutable reduction operation that places the results into an object, such as a List, Map or Set.

• Method collect with one argument receives an object that implements interface Collector (package java.util.stream), which specifies how to perform the mutable reduction.

• Class Collectors (package java.util.stream) provides static methods that return predefined Collector implementations.

Collectors method toList ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.