The collect() method

The collect() method allows you to transform and group the elements of the stream generating a new data structure with the final results of the stream. You can use up to three different data types: an input data type, the data type of the input elements that come from the stream, an intermediate data type used to store the elements while the collect() method is running, and an output data type returned by the collect() method.

There are two different versions of the collect() method. The first version accepts the following three functional parameters:

  • Supplier: This is a function that creates an object of the intermediate data type. If you use a sequential stream, this method will be called once. If you use a parallel ...

Get Mastering Concurrency Programming with Java 9 - Second 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.