Collectors and reduction in java
In the Java programming language Streams API, the Collectors and Reduction methods are used to merge or combine elements of array, list, and set data types in Java and display the final result. Java users use the Collectors and Reduction-supported function methods to perform multiple Java stream operations.

Collectors Method with Java Streams.
To use the Collectors method in Java programs, Java provides a built-in utility class called Collectors, java.util.stream, which helps users collect stream data type elements and return results from lists, sets, maps, strings, and other collection data types in a Java program.
Import these required class package libraries.
import java.util.stream.Collectors;
collect(toList()) Streams function method with Java.
The collect(toList()) function method in a Java Streams program collects and displays the elements of the Collection Array data type in a list.
Example of the collect(toList()) Streams function.
List<Integer> integers =
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Integer> integers = Arrays.asList(1, 4, 7, 10, 17, 21, 29, 39, 5);
List<Integer> output = integers.stream()
.filter(integer -> integer > 10)
.collect(Collectors.toList());
System.out.println(output);
}
}
It’s output.
[17, 21, 29, 39]
collect(toSet()) Streams function method with Java.
The collect(toSet()) function in a Java Streams program removes any duplicate values from a collection array data element and creates a new list of Set data type elements.
Example of the collect(toSet()) Streams function.
List<Integer> integers =
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
var integers = Arrays.asList(1, 4, 1, 7, 9, 7, 10, 4, 21, 77);
Set<Integer> output = integers.stream()
.collect(Collectors.toSet());
System.out.println(output);
}
}
It’s output.
[1, 4, 21, 7, 9, 10, 77]
joining() Streams function method with Java.
The joining() function is used in Java Streams programs to group or combine collection array data element strings into a single string.
Example of the joining() Streams function.
List<String> city =
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> city = Arrays.asList(“Mumbai”, “Delhi”, “Bengaluru”, “Hyderabad”, “Ahmedabad”);
String output = city.stream()
.collect(Collectors.joining(“: “));
System.out.println(output);
}
}
It’s output.
Mumbai: Delhi: Bengaluru: Hyderabad: Ahmedabad
groupingBy() Streams function method with Java.
The groupingBy() function in a Java Streams program displays collection array data type elements grouped by a particular array property.
Example of the groupingBy() Streams function.
List<String> country =
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> country = Arrays.asList(“India”, “United States”, “China”, “Canada”, “Brazil”, “Australia”, “Argentina”);
Map<Integer, List<String>> output = country.stream()
.collect(Collectors.groupingBy(String::length));
System.out.println(output);
}
}
Its output.
{5=[India, China], 6=[Canada, Brazil], 9=[Australia, Argentina], 13=[United States]}
This function displays country names grouped by their length.
counting() Streams function method with Java.
The counting() function counts the elements of the Collection array data type in Java streams programs. It is mostly used with the groupingBy() function method in Java streams.
Example of the counting() Streams function.
Map<Integer, Long> output =
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> programming = Arrays.asList(“Rust”, “Pearl”, “Matlab”, “Mysql”, “Jquery”, “Html”);
Map<Integer, Long> output = programming.stream()
.collect(Collectors.groupingBy(
String::length,
Collectors.counting()
));
System.out.println(output);
}
}
Its output.
{4=2, 5=2, 6=2}
Reduction Method with Java Streams.
The concept of stream reduction in a Java program means grouping multiple available stream collection data type element values into a single result and displaying them.
Common reduction operations in a Java program include.
- sum() reduction function.
- count() reduction function.
- min() reduction function.
- max() reduction function.
- reduce() reduction function.
The reduce() Streams function method with Java.
The reduce() function method in a Java program displays the collection data type elements by adding them step by step.
Example of the reduce() Streams function.
List<Integer> integers =
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> integers = Arrays.asList(1, 3, 7, 11, 17, 24, 39, 12);
int sum = integers.stream()
.reduce(0, (p, q) -> p + q);
System.out.println(sum);
}
}
Its output.
114
Its calculation is as follows.
0 + 1 = 1
1 + 3 = 4
4 + 7 = 11
11 + 11 = 22
22 + 17 = 39
39 + 24 = 63
63 + 39 = 102
102 + 12 = 114
Finding Maximum Streams function method with Java.
In Java programs, the max() function method is used to find the largest maximum value element from a collection data type.
Example of the max() Streams function.
int max =
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> integers = Arrays.asList(1, 3, 9, 2, 7, 9, 5, 6, 11);
int max = integers.stream()
.reduce(Integer.MIN_VALUE, Integer::max);
System.out.println(max);
}
}
Its output.
11
Searching Products Streams function method with Java.
In Java programs, the product() function method is used to find the product or multiplication value of a collection data type element.
Example of the product Streams function.
int product =
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> integers = Arrays.asList(3, 3, 2, 7, 5);
int product = integers.stream()
.reduce(1, (p, q) -> p * q);
System.out.println(product);
}
}
Its output.
630
Its calculation is as follows.
[3, x 3, x 2, x 7, x 5] = 630
Main difference between Collectors vs Reduction in java stream
| # | Collectors stream java method | Reduction stream java method |
| 1. | Collectors’ java method used to collect/process array, list, set, map data elements into an output result | Reduction java method used to combine array, list, set, map data elements into one final output result |
| 2. | Generally, we used it with collect() function method | Normally we can usually uses reduce() function method for it |
| 3. | With collectors’ data type we can create list, set, map, string, data type element collection | With reduce function usually we can produce a single value output as result |
| 4. | Collectors used to display a list of collection tolist() data element | We can calculate a number of sums using reduce() function with given integer values |
| 5. | Collectors used to display a set of collection toset() data element | We can find a number of products using reduce() function with given integer values |
| 6. | Used to display a group of collection groupingby() data element | We can use it to find a number of only 1 single maximum value using reduce() function among given integer values |
| 7. | Used to display or join a group of collection joining() data element | We can use it to find a number of minimum values using reduce() function among given integer values |
Conclusion of Collector vs. Reduction method in Java streams.
Collector vs. Here’s an easy way to remember the Reduction method:
- Collector stream method → This user-defined Java program “collects the results” of a collection data type.
- Reduction stream method → This user-defined Java program “groups the results” of a collection data type into a single result.
Collector vs. Reduction stream method concept example.
integers.stream()
.collect(Collectors.toList());
This means collecting the elements of the collection data into a list.
Whereas.
integers.stream()
.reduce(0, Integer::sum);
This means combining the elements of the collection data into a single value in a list.

