From the course: Functional Programming with Java
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Reduce in Java - Java Tutorial
From the course: Functional Programming with Java
Reduce in Java
- [Instructor] The next important list processing operation that we're going to learn about is something called reducing. And to do it, we're going to use yet another of Java's built in functions called reduce. Now the purpose of reducing might not be quite as apparent as mapping or filtering, so let's take a look at how it works. What reducing allows us to do is take a list of data and well, reduce it down to a single piece of data. So we just reduce if we wanted to take a list of numbers and reduce it down to a sum or an average, for example. What reduce does essentially is it starts off with an initial value, say zero if we're working with integers, and for each element in the stream, it modifies this initial value in some way until after we've processed all our elements, we end up with the result. So in the case of finding the sum of a list of numbers, for example, we'd start off with zero and then we'd add each element to the initial value. Or if we were trying to find the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.