How it works...

As we mentioned in the introduction of this recipe, the collect() method allows you to do a mutable reduction of the elements of a Stream. We call it a mutable reduction because the final result of the stream will be a mutable data structure, such as Map or List. The Stream class of the Java Concurrency API provides two versions of the collect() method.

The first one receives only one parameter that is an implementation of the Collector interface. This interface has seven methods, so you normally won't implement your own collectors. Instead of this, you will use the utility class Collectors, which has a lot of methods that return ready-to-use Collector objects for your reduce operations. In our example, we have used the following ...

Get Java 9 Concurrency Cookbook - 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.