Lambda Expression - Predicate

原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/12807044.html

 

Predicate

Predicate是一个谓词函数,主要作为一个谓词演算推导真假值存在,返回布尔值。

 

Demo

PredicateTest.java

 1 package org.fool.java8;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Arrays;
 5 import java.util.List;
 6 import java.util.function.BiPredicate;
 7 import java.util.function.IntPredicate;
 8 import java.util.function.Predicate;
 9 
10 public class PredicateTest {
11 
12     private static List<Person> filter(List<Person> persons, Predicate<Person> predicate) {
13         List<Person> result = new ArrayList<>();
14 
15         for (Person person : persons) {
16             if (predicate.test(person)) {
17                 result.add(person);
18             }
19         }
20 
21         return result;
22     }
23 
24     private static List<Person> filterByAge(List<Person> persons, IntPredicate predicate) {
25         List<Person> result = new ArrayList<>();
26 
27         for (Person person : persons) {
28             if (predicate.test(person.getAge())) {
29                 result.add(person);
30             }
31         }
32 
33         return result;
34     }
35 
36     private static List<Person> filterByNameAndAge(List<Person> persons, BiPredicate<String, Integer> predicate) {
37         List<Person> result = new ArrayList<>();
38 
39         for (Person person : persons) {
40             if (predicate.test(person.getName(), person.getAge())) {
41                 result.add(person);
42             }
43         }
44 
45         return result;
46     }
47 
48     private static class Person {
49         private String name;
50         private Integer age;
51 
52         public Person(String name, Integer age) {
53             this.name = name;
54             this.age = age;
55         }
56 
57         public String getName() {
58             return name;
59         }
60 
61         public void setName(String name) {
62             this.name = name;
63         }
64 
65         public Integer getAge() {
66             return age;
67         }
68 
69         public void setAge(Integer age) {
70             this.age = age;
71         }
72 
73         @Override
74         public String toString() {
75             return "Person{" +
76                     "name='" + name + '\'' +
77                     ", age=" + age +
78                     '}';
79         }
80     }
81 
82     public static void main(String[] args) {
83         List<Person> list = Arrays.asList(
84                 new Person("zhangsan", 18),
85                 new Person("lisi", 28));
86 
87         filter(list, person -> person.getAge().equals(18)).forEach(System.out::println);
88 
89         filterByAge(list, age -> age > 18).forEach(System.out::println);
90 
91         filterByNameAndAge(list, (name, age) -> name.equals("zhangsan") && age < 28).forEach(System.out::println);
92     }
93 }

Console Output

Person{name='zhangsan', age=18}
Person{name='lisi', age=28}
Person{name='zhangsan', age=18}

 

posted @ 2020-04-30 10:00  李白与酒  阅读(345)  评论(0编辑  收藏  举报