Using Stream.reduce() for Data Aggregation in Java


The reduce() method is a terminal operation that aggregates stream elements into a single result based on a provided identity and accumulator function.

Source Code

List numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().reduce(0, Integer::sum);
System.out.println("Sum: " + sum); // Outputs: 15
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments