Collectors: Collection Operations.

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 the collect() method to reduce streams.

Collectors

Collectors is a final class that extends the Object class. It provides the most common mutable reduction operations that could be required by application developers as individual static methods.

Some of the important reduction operations already implemented in the Collectors class are listed below:

Get hands-on with 1200+ tech skills courses.