Java Reference
In-Depth Information
Figure 5.8. A reduce operation—calculating the maximum
Optional<Integer> max = numbers.stream().reduce(Integer::max);
To calculate the minimum, you need to pass Integer.min to the reduce operation instead of
Integer.max:
Optional<Integer> min = numbers.stream().reduce(Integer::min);
You could have equally well used the lambda (x,y)->x<y?x:y instead of Integer::min, but the
latter is easier to read.
To test your understanding of the reduce operation, have a go at Quiz 5.3 .
Quiz 5.3: Reducing
How would you count the number of dishes in a stream using the map and reduce methods?
Answer:
Search WWH ::




Custom Search