Java 8计算最小值

use*_*540 2 java java-8

我正在练习Java 8.我不明白为什么这个方法总是返回0,或者更好的身份值:

public static Integer getMinAge(List<Person> peopleList) {
    return peopleList.stream().mapToInt(Person::getAge).reduce(0, Integer::min);
}
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是,Integer :: max方法返回正确的值.我在这做错了什么?

hol*_*ava 5

因为age > 0 and identity == 0那时Integer.min(identity,age)总是返回0.

使用IntStream.reduce(IntBinaryOperator)

public static Integer getMinAge(List<Person> peopleList) {
  return peopleList.stream().mapToInt(Person::getAge)
            .reduce(Integer::min).orElse(0);
}
Run Code Online (Sandbox Code Playgroud)

使用IntStream.min()

public static Integer getMinAge(List<Person> peopleList) {
  return peopleList.stream().mapToInt(Person::getAge)
           .min().orElse(0);
}
Run Code Online (Sandbox Code Playgroud)