O'Reilly logo

Java 11 Cookbook by Mohamed Sanaulla, Nick Samoylov

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

Getting ready

As we mentioned in the previous recipe, there are two overloaded versions of the collect() terminal operation, which allow us to create a collection of the stream elements:

  • R collect(Supplier<R> supplier, BiConsumer<R,T> accumulator, BiConsumer<R,R> combiner): Produces the R result using the passed-in functions applied to the stream elements of the T type
  • R collect(Collector<T, A, R> collector): Produces the R result using the passed-in Collector object applied to the stream elements of the T type

These operations can be also used to create a Map object, and in this recipe, we are going to demonstrate how to do so.

In support of the second of the preceding versions of the collect() operation, the Collectors class provides ...

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