Streams: Collectors.reducing is the collector form of reduce

Collectors.reducing offers the same identity/accumulator or accumulator-only shapes as Stream.reduce, but as a Collector, so it can be combined inside groupingBy or another multi-level collector.

Code
List<Integer> prices = List.of(10, 20, 30, 40);
int total = prices.stream()
        .collect(Collectors.reducing(0, Integer::sum));
System.out.println("Total via reducing collector: " + total);
Optional<Integer> product = prices.stream()
        .collect(Collectors.reducing((a, b) -> a * b));
System.out.println("Product via no-identity reducing: " + product.get());
Output
Total via reducing collector: 100
Product via no-identity reducing: 240000
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms