当前位置:   article > 正文

java使用Stream流找出集合对象中最小值_stream获取最小值

stream获取最小值

一、问题

有如下四个人,如何使用Lamdba找到年龄最小的并打印出来

  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. @EqualsAndHashCode
  5. public class Person {
  6. private String name;
  7. private Integer age;
  8. }

  1. List<Person> persons = Stream.of(new Person("老王", 35)
  2. , new Person("老张", 23)
  3. , new Person("老周", 56)
  4. , new Person("老李", 48))
  5. .collect(Collectors.toList());

 二、使用sorted+limit

  1. persons.stream()
  2. .sorted((p1, p2) -> p2.getAge() - p1.getAge())
  3. .limit(1).forEach((person) -> {
  4. System.out.println("sorted+limit:" + JSON.toJSONString(person));
  5. // sorted+limit:{"age":56,"name":"老周"}
  6. });

三、使用 sorted+skip(不推荐)

  1. persons.stream()
  2. .sorted((p1, p2) -> p1.getAge() - p2.getAge())
  3. .skip(persons.size() - 1).forEach((person) -> {
  4. System.out.println("sorted+skip:" + JSON.toJSONString(person));
  5. // sorted+skip:{"age":56,"name":"老周"}
  6. });

 四、使用max

  1. Optional<Person> max = persons.stream()
  2. .max((p1, p2) -> p1.getAge() - p2.getAge());
  3. max.ifPresent((person -> {
  4. System.out.println("max:" + JSON.toJSONString(person));
  5. // max:{"age":56,"name":"老周"}
  6. }));

五、使用min(不推荐)

  1. Optional<Person> min = persons.stream()
  2. .min((p1, p2) -> p2.getAge() - p1.getAge());
  3. min.ifPresent((person -> {
  4. System.out.println("min:" + JSON.toJSONString(person));
  5. // min:{"age":56,"name":"老周"}
  6. }));

六、使用reduce(推荐)

  1. Optional<Person> cacl2 = persons.stream()
  2. .reduce((p1, p2) -> p1.getAge() > p2.getAge() ? p1 : p2);
  3. cacl2.ifPresent(person -> {
  4. System.out.println("reduce" + JSON.toJSONString(person));
  5. // reduce{"age":56,"name":"老周"}
  6. });

七、完整代码及结果

  1. private static void test01() {
  2. List<Person> persons = Stream.of(new Person("老王", 35)
  3. , new Person("老张", 23)
  4. , new Person("老周", 56)
  5. , new Person("老李", 48))
  6. .collect(Collectors.toList());
  7. System.out.println(JSON.toJSONString(persons));
  8. //[{"age":35,"name":"老王"},{"age":23,"name":"老张"},{"age":56,"name":"老周"},{"age":48,"name":"老例"}]
  9. // 找出年龄最大的人,并输入姓名和年龄
  10. persons.stream()
  11. .sorted((p1, p2) -> p2.getAge() - p1.getAge())
  12. .limit(1).forEach((person) -> {
  13. System.out.println("sorted+limit:" + JSON.toJSONString(person));
  14. // sorted+limit:{"age":56,"name":"老周"}
  15. });
  16. persons.stream()
  17. .sorted((p1, p2) -> p1.getAge() - p2.getAge())
  18. .skip(persons.size() - 1).forEach((person) -> {
  19. System.out.println("sorted+skip:" + JSON.toJSONString(person));
  20. // sorted+skip:{"age":56,"name":"老周"}
  21. });
  22. Optional<Person> max = persons.stream()
  23. .max((p1, p2) -> p1.getAge() - p2.getAge());
  24. max.ifPresent((person -> {
  25. System.out.println("max:" + JSON.toJSONString(person));
  26. // max:{"age":56,"name":"老周"}
  27. }));
  28. Optional<Person> min = persons.stream()
  29. .min((p1, p2) -> p2.getAge() - p1.getAge());
  30. min.ifPresent((person -> {
  31. System.out.println("min:" + JSON.toJSONString(person));
  32. // min:{"age":56,"name":"老周"}
  33. }));
  34. Optional<Person> cacl2 = persons.stream()
  35. .reduce((p1, p2) -> p1.getAge() > p2.getAge() ? p1 : p2);
  36. cacl2.ifPresent(person -> {
  37. System.out.println("reduce" + JSON.toJSONString(person));
  38. // reduce{"age":56,"name":"老周"}
  39. });
  40. }
  1. [{"age":35,"name":"老王"},{"age":23,"name":"老张"},{"age":56,"name":"老周"},{"age":48,"name":"老李"}]
  2. sorted+limit:{"age":56,"name":"老周"}
  3. sorted+skip:{"age":56,"name":"老周"}
  4. max:{"age":56,"name":"老周"}
  5. min:{"age":56,"name":"老周"}
  6. reduce{"age":56,"name":"老周"}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/479474
推荐阅读
相关标签
  

闽ICP备14008679号