Collectors: Collection Operations.

This lesson discusses the immutable reduction operations tusing the collect() method.

In the earlier lesson, we discussed some immutable reduction methods. In this lesson, we will discuss mutable reduction methods.

Mutable reductions

The mutable reductions collect the desired results into a mutable container object, such as a java.util.Collection or an array.

The mutable reduction is achieved through the collect() method. It is one of the Java 8 Stream API’s terminal methods.

There are two overloaded versions of the collect() method:

  1. collect(Collector<? super T,A,R> collector)

  2. <R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner)

This lesson focuses on the collect() method which takes an instance of Collector as input.

We have two options:

  1. We can create our own Collector implementation.

  2. We can use the predefined implementations provided by the Collectors class.

Before discussing the collect() method further, we will first discuss the Collectors class in detail and look at how its methods are used with ...