reduce
A terminal stream operation that combines all elements into one value, such as a sum, using a starting value and a combining operation.
In more detail
Built-in reductions such as sum and count cover common cases. A complicated custom reduction can hide business rules, so a named method or plain loop is often clearer.
Example
.reduce(0, Integer::sum)Related terms
- terminal operation — The final stream step, such as toList, collect, count or anyMatch, that starts the pipeline and produces a result.
- collector — A recipe passed to a stream's collect operation that gathers the elements into a result, such as a set or a map grouped by key.
Taught in
- The Streams API — Modern Java: Virtual Threads & High-Throughput Services
- Stream Pipelines, Primitive Streams And Optional — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Reduction, Grouping And Partitioning — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Lambdas And Streams — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Concurrent Collections And Parallel Streams — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Paths And Files With NIO.2 — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep