我正在练习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方法返回正确的值.我在这做错了什么?
因为age > 0 and identity == 0那时Integer.min(identity,age)总是返回0.
public static Integer getMinAge(List<Person> peopleList) {
return peopleList.stream().mapToInt(Person::getAge)
.reduce(Integer::min).orElse(0);
}
Run Code Online (Sandbox Code Playgroud)
public static Integer getMinAge(List<Person> peopleList) {
return peopleList.stream().mapToInt(Person::getAge)
.min().orElse(0);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
226 次 |
| 最近记录: |