一、问题
有如下四个人,如何使用Lamdba找到年龄最小的并打印出来
@Data @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode public class Person { private String name; private Integer age; }
List<Person> persons = Stream.of(new Person("老王", 35) , new Person("老张", 23) , new Person("老周", 56) , new Person("老李", 48)) .collect(Collectors.toList());
二、使用sorted+limit
persons.stream() .sorted((p1, p2) -> p2.getAge() - p1.getAge()) .limit(1).forEach((person) -> { System.out.println("sorted+limit:" + JSON.toJSONString(person)); // sorted+limit:{"age":56,"name":"老周"} });
三、使用 sorted+skip(不推荐)
persons.stream() .sorted((p1, p2) -> p1.getAge() - p2.getAge()) .skip(persons.size() - 1).forEach((person) -> { System.out.println("sorted+skip:" + JSON.toJSONString(person)); // sorted+skip:{"age":56,"name":"老周"} });
四、使用max
Optional<Person> max = persons.stream() .max((p1, p2) -> p1.getAge() - p2.getAge()); max.ifPresent((person -> { System.out.println("max:" + JSON.toJSONString(person)); // max:{"age":56,"name":"老周"} }));
五、使用min(不推荐)
Optional<Person> min = persons.stream() .min((p1, p2) -> p2.getAge() - p1.getAge()); min.ifPresent((person -> { System.out.println("min:" + JSON.toJSONString(person)); // min:{"age":56,"name":"老周"} }));
六、使用reduce(推荐)
Optional<Person> cacl2 = persons.stream() .reduce((p1, p2) -> p1.getAge() > p2.getAge() ? p1 : p2); cacl2.ifPresent(person -> { System.out.println("reduce" + JSON.toJSONString(person)); // reduce{"age":56,"name":"老周"} });
七、完整代码及结果
private static void test01() { List<Person> persons = Stream.of(new Person("老王", 35) , new Person("老张", 23) , new Person("老周", 56) , new Person("老李", 48)) .collect(Collectors.toList()); System.out.println(JSON.toJSONString(persons)); //[{"age":35,"name":"老王"},{"age":23,"name":"老张"},{"age":56,"name":"老周"},{"age":48,"name":"老例"}] // 找出年龄最大的人,并输入姓名和年龄 persons.stream() .sorted((p1, p2) -> p2.getAge() - p1.getAge()) .limit(1).forEach((person) -> { System.out.println("sorted+limit:" + JSON.toJSONString(person)); // sorted+limit:{"age":56,"name":"老周"} }); persons.stream() .sorted((p1, p2) -> p1.getAge() - p2.getAge()) .skip(persons.size() - 1).forEach((person) -> { System.out.println("sorted+skip:" + JSON.toJSONString(person)); // sorted+skip:{"age":56,"name":"老周"} }); Optional<Person> max = persons.stream() .max((p1, p2) -> p1.getAge() - p2.getAge()); max.ifPresent((person -> { System.out.println("max:" + JSON.toJSONString(person)); // max:{"age":56,"name":"老周"} })); Optional<Person> min = persons.stream() .min((p1, p2) -> p2.getAge() - p1.getAge()); min.ifPresent((person -> { System.out.println("min:" + JSON.toJSONString(person)); // min:{"age":56,"name":"老周"} })); Optional<Person> cacl2 = persons.stream() .reduce((p1, p2) -> p1.getAge() > p2.getAge() ? p1 : p2); cacl2.ifPresent(person -> { System.out.println("reduce" + JSON.toJSONString(person)); // reduce{"age":56,"name":"老周"} }); }
[{"age":35,"name":"老王"},{"age":23,"name":"老张"},{"age":56,"name":"老周"},{"age":48,"name":"老李"}] sorted+limit:{"age":56,"name":"老周"} sorted+skip:{"age":56,"name":"老周"} max:{"age":56,"name":"老周"} min:{"age":56,"name":"老周"} reduce{"age":56,"name":"老周"}