The stream.collect()
method is used to perform a mutable reduction operation on the elements of a stream. It returns a new mutable object containing the results of the reduction operation.
This method can be used to perform several different types of reduction operations, such as:
String
by concatenating the contents of a stream.List
or Set
.The syntax of the stream.collect()
method is as follows:
<R, A> R stream.collect(Collector<? super T,A,R> collector)
R
: This is the type of the result object.T
: This is the type of the source element. A
: This is the type of mutable object used for accumulating results (which is also the type of the result object). collector
: This is the type of collector that performs the reduction operation.This method returns the result of the reduction operation.
The following example shows us how to use the Stream.collect()
method:
import java.util.*;import java.util.stream.*;import static java.util.stream.Collectors.*;public class CollectExample {public static void main(String[] args) {Integer[] intArray = {1, 2, 3, 4, 5};// Creating a List from an array of elements// using Arrays.asList() methodList<Integer> list = Arrays.asList(intArray);// Collecting all elements of the list into a new// list using collect() methodList<Integer> evenNumbersList = list.stream().filter(i -> i%2 == 0).collect(toList());System.out.println(evenNumbersList);// finding the sum of all the values// in the streamInteger sum = list.stream().collect(summingInt(i -> i));System.out.println(sum);// finding the maximum of all the values// in the streamInteger max = list.stream().collect(maxBy(Integer::compare)).get();System.out.println(max);// finding the minimum of all the values// in the streamInteger min = list.stream().collect(minBy(Integer::compare)).get();System.out.println(min);// counting the values in the streamLong count = list.stream().collect(counting());System.out.println(count);}}
intArray
.Arrays.asList()
method to get a List
from an array of elements.stream()
method to get a stream from the list. We filter the even elements and collect them into a new list using the collect()
method.System.out.println()
method to print the filtered even numbers.collect()
method summingInt(ToIntFunction)
as an argument. The summingInt()
method returns a collector that sums the integer values extracted from the stream elements by applying an int producing mapping function to each element.System.out.println()
method to print the sum of elements in the stream.collect()
method with maxBy(Comparator)
as an argument. The maxBy()
accepts a Comparator and returns a collector that extracts the maximum element from the stream according to the given Comparator.System.out.println()
method to print the maximum value in the stream.collect()
method with minBy(Comparator)
as an argument. The minBy()
accepts a Comparator and returns a collector that extracts the minimum element from the stream according to the given Comparator.collect()
method with counting()
as an argument, which returns a collector that counts the number of input elements.System.out.println()
method to print the number of elements in the stream.