Java

What is the stream.collect() method in Java

The Stream API is introduced in Java 8 and used for handling huge collections of objects. Furthermore, it is a series of objects that can handle several operations and be parallelized to create the desired outcome. For the corresponding purpose, the “stream.collect()” Java method is invoked to insert the various components into the collection.

This tutorial will examine:

What is the stream.collect() method in Java?

The Java “stream.collect()” method is often used to add the components of a stream to a collection, that is the last action. In the scenario of a parallel stream, it holds synchronization. Furthermore, the Collectors class offers multiple Collector implementations.

How to Use the stream.collect() method in Java?

To utilize the “stream.collect()” method in Java, follow the below-stated syntax:

Syntax

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

In this syntax:

  • T” is a type of source element.
  • A” is the type of object which is mutable. It is utilized for accumulating the results.
  • R” indicates the resultant object.
  • collector” is used to perform the reduction or minimization operation.

Example 1: Concatenating List of Strings

To concatenate the list of strings, make a list of an integer with a particular name and insert the values inside the list with the “List.of()” method:

List<Integer> numbers = List.of(10, 11, 12, 13, 14, 15, 6);

Then, make another list and utilize the “stream()” method to get a stream from the list. Then, it will filter out the even elements from a specified list and add them to a new list with the help of the “collect()” method.

List<Integer> evenNumbers = numbers.stream().filter(x -> x % 2 == 0).collect(Collectors.toList());

Now, use the “println()” method and pass the defined list as the argument of the method to print it on the console:

System.out.println(even numbers);

Example 2: Map Odd Number

You can use the stream.collect() method to map the numbers. In this example, the odd numbers will be mapped from a particular list. To do so, make a list of integer data types and add the elements in the list with the help of the “List.of()” method:

List<Integer> numbers = List.of(11, 22, 55, 44, 77);

Now, utilize the “Collectors.toMap()” function that can be used for collecting the stream objects/elements to a Map. This stated method always accepts only two arguments for the mapping value and the corresponding key in the Map:

Map<Integer, String> mapOddNumbers = numbers.parallelStream().filter(y-> y% 2 != 0).collect(Collectors.toMap(Function.identity(), y-> String.valueOf(y)));

Invoke “println()” and pass the “mapOddNumbers” as the argument to print the result on the console:

System.out.println(mapOddNumbers);

That’s all about the usage of the stream.collect() method in Java.

Conclusion

The “stream.collect()” method in Java is utilized for inserting the components of a stream into a collection. You can perform various operations on the stream, including getting even elements in the stream, and odd elements, and also map the numbers in the list. This post stated the method stream.collect() in Java.

About the author

Hafsa Javed