java predicate原理_Java Predicate

1 packageorg.htsg;2

3 importjava.util.ArrayList;4 importjava.util.List;5 importjava.util.Objects;6 importjava.util.function.Predicate;7

8 /**

9 *@authorHTSG10 */

11 public classPredicateTest {12 public static voidmain(String[] args) {13 //添加十个学生

14 List studentList = new ArrayList<>(10);15 for (int i = 0; i < 10; i++) {16 studentList.add(new Student("student" + i, 10 +i));17 }18 //获取年龄大于15的学生19 //[Student{name='student6', age=16}, Student{name='student7', age=17}, Student{name='student8', age=18}, Student{name='student9', age=19}]

20 List filteredStudents =test(studentList, PredicateTest::filterAge1);21 System.out.println(filteredStudents);22 //获取年龄大于15并且名字叫 "student7" 的学生23 //[Student{name='student7', age=17}]

24 filteredStudents =and(studentList, PredicateTest::filterAge1, PredicateTest::filterName);25 System.out.println(filteredStudents);26 //获取年龄不大于15的学生27 //[Student{name='student0', age=10}, Student{name='student1', age=11}, Student{name='student2', age=12}, Student{name='student3', age=13}, Student{name='student4', age=14}, Student{name='student5', age=15}]

28 filteredStudents =negate(studentList, PredicateTest::filterAge1);29 System.out.println(filteredStudents);30 //获取年龄不大于15或名字叫 "student7" 的学生31 //[Student{name='student0', age=10}, Student{name='student1', age=11}, Student{name='student2', age=12}, Student{name='student3', age=13}, Student{name='student4', age=14}, Student{name='student5', age=15}, Student{name='student7', age=17}]

32 filteredStudents =or(studentList, PredicateTest::filterAge2, PredicateTest::filterName);33 System.out.println(filteredStudents);34 //获取和目标学生属性值相同的学生列表35 //[Student{name='student1', age=11}]

36 filteredStudents = isEqual(studentList, new Student("student1", 11));37 System.out.println(filteredStudents);38

39 }40

41

42 public static booleanfilterAge1(Student student) {43 return student.getAge() > 15;44 }45

46 public static booleanfilterAge2(Student student) {47 return student.getAge() <= 15;48 }49

50 public static booleanfilterName(Student student) {51 return "student7".equals(student.getName());52 }53

54 public static List test(List students, Predicatepre) {55 List result = new ArrayList<>(10);56 for(Student student : students) {57 if(pre.test(student)) {58 result.add(student);59 }60 }61 returnresult;62 }63

64 public static List and(List students, Predicate pre1, Predicatepre2) {65 List result = new ArrayList<>(10);66 for(Student student : students) {67 if(pre1.and(pre2).test(student)) {68 result.add(student);69 }70 }71 returnresult;72 }73

74 public static List negate(List students, Predicatepre) {75 List result = new ArrayList<>(10);76 for(Student student : students) {77 if(pre.negate().test(student)) {78 result.add(student);79 }80 }81 returnresult;82 }83

84 public static List or(List students, Predicate pre1, Predicatepre2) {85 List result = new ArrayList<>(10);86 for(Student student : students) {87 if(pre1.or(pre2).test(student)) {88 result.add(student);89 }90 }91 returnresult;92 }93

94 public static List isEqual(Liststudents, Student student) {95 List result = new ArrayList<>(10);96 for(Student studentTemp : students) {97 if(Predicate.isEqual(student).test(studentTemp)) {98 result.add(studentTemp);99 }100 }101 returnresult;102 }103

104 //创建静态内部类

105 public static classStudent {106 privateString name;107 private intage;108

109 publicStudent() {110 }111

112 public Student(String name, intage) {113 this.name =name;114 this.age =age;115 }116

117 publicString getName() {118 returnname;119 }120

121 public voidsetName(String name) {122 this.name =name;123 }124

125 public intgetAge() {126 returnage;127 }128

129 public void setAge(intage) {130 this.age =age;131 }132

133 @Override134 publicString toString() {135 return "Student{" +

136 "name='" + name + '\'' +

137 ", age=" + age +

138 '}';139 }140

141 @Override142 public booleanequals(Object o) {143 if (this ==o) {144 return true;145 }146 if (o == null || getClass() !=o.getClass()) {147 return false;148 }149 Student student =(Student) o;150 return age == student.age &&

151 Objects.equals(name, student.name);152 }153 }154 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值