There's more...

The Collectors class has many more methods that return Collector objects that can be used in the collect() method. The following are the most interesting:

  • toList(): This method returns Collector that groups all the elements of Stream into List.
  • toCollection(): This method returns Collector that groups all the elements of Stream into Collection. This method returns as parameter an expression that creates Collection, which will be used internally by Collector and returned at the end of its execution.
  • averagingInt(), averagingLong(), and averagingDouble(): These methods return Collector that calculates the average of int, long, and double values, respectively. They receive as parameters an expression to convert an element of ...

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.