From the course: Java SE 21 Developer (1Z0-830) Cert Prep

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Collection and reduction, part 1

Collection and reduction, part 1 - Java Tutorial

From the course: Java SE 21 Developer (1Z0-830) Cert Prep

Collection and reduction, part 1

- Okay, time to look at collection and reduction. First, let's look at reduction from a conceptual perspective. If we have a binary operator, it takes two data items of a given type and creates one item of the same type. The reduce operation applies this idea repeatedly to create a single result from an arbitrary number of items. This is the foundation of how the stream API produces a single result from many input items. Let's take a look at this on the whiteboard. Let's suppose we have a sequence of numbers, in this case one through 10, and we also have a binary operator, the plus operator. The plus operator takes two items of number type and produces one number type result. So one plus two will give us three. But now we can apply this again by taking the intermediate result, three, and the next data item, also three, and applying the plus to those two items. Well, that will give us six. From there, of course,…

Contents