O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required