What is the reduce method actually doing here? I've read the Oracle docs but I still don't understand what the reduce method is doing here in this example
public static Coder findCoderWithWorstBMI(List<Coder> coders) {
return coders.stream().sorted(Comparator.comparing(BMICalculator::calculateBMI))
.reduce((first, second) -> second).orElse(null);
}
private static double calculateBMI(Coder coder) {
double height = coder.getHeight();
double weight = coder.getWeight();
if (height == 0.0)
throw new ArithmeticException();
double bmi = weight / (height * height);
return Math.round(bmi * 100) / 100.0;
}
reverseaComparatoras well and thenfindFirstof such an element. Which then forms a combination ofsort+findFirsteasily replaceable withminormaxAPIs.