1 /* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 4 */ 5 package java.util.function; 6 7 import java.util.Objects; 8 9 /** 10 * Represents a predicate (boolean-valued function) of one argument. 11 * 12 *This is a functional interface
13 * whose functional method is {@link #test(Object)}. 14 * 15 * @paramthe type of the input to the predicate 16 * 17 * @since 1.8 18 */ 19 @FunctionalInterface 20 public interface Predicate{ 21 22 /** 23 * Evaluates this predicate on the given argument. 24 * 25 * @param t the input argument 26 * @return {@code true} if the input argument matches the predicate, 27 * otherwise {@code false} 28 */ 29 boolean test(T t); 30 31 /** 32 * Returns a composed predicate that represents a short-circuiting logical 33 * AND of this predicate and another. When evaluating the composed 34 * predicate, if this predicate is {@code false}, then the {@code other} 35 * predicate is not evaluated. 36 * 37 * Any exceptions thrown during evaluation of either predicate are relayed
38 * to the caller; if evaluation of this predicate throws an exception, the 39 * {@code other} predicate will not be evaluated. 40 * 41 * @param other a predicate that will be logically-ANDed with this 42 * predicate 43 * @return a composed predicate that represents the short-circuiting logical 44 * AND of this predicate and the {@code other} predicate 45 * @throws NullPointerException if other is null 46 */ 47 default Predicateand(Predicate super T> other) { 48 Objects.requireNonNull(other); 49 return (t) -> test(t) && other.test(t); 50 } 51 52 /** 53 * Returns a predicate that represents the logical negation of this 54 * predicate. 55 * 56 * @return a predicate that represents the logical negation of this 57 * predicate 58 */ 59 default Predicate negate() { 60 return (t) -> !test(t); 61 } 62 63 /** 64 * Returns a composed predicate that represents a short-circuiting logical 65 * OR of this predicate and another. When evaluating the composed 66 * predicate, if this predicate is {@code true}, then the {@code other} 67 * predicate is not evaluated. 68 * 69 * Any exceptions thrown during evaluation of either predicate are relayed
70 * to the caller; if evaluation of this predicate throws an exception, the 71 * {@code other} predicate will not be evaluated. 72 * 73 * @param other a predicate that will be logically-ORed with this 74 * predicate 75 * @return a composed predicate that represents the short-circuiting logical 76 * OR of this predicate and the {@code other} predicate 77 * @throws NullPointerException if other is null 78 */ 79 default Predicateor(Predicate super T> other) { 80 Objects.requireNonNull(other); 81 return (t) -> test(t) || other.test(t); 82 } 83 84 /** 85 * Returns a predicate that tests if two arguments are equal according 86 * to {@link Objects#equals(Object, Object)}. 87 * 88 * @param the type of arguments to the predicate 89 * @param targetRef the object reference with which to compare for equality, 90 * which may be {@code null} 91 * @return a predicate that tests if two arguments are equal according 92 * to {@link Objects#equals(Object, Object)} 93 */ 94 staticPredicate isEqual(Object targetRef) { 95 return (null == targetRef) 96 ? Objects::isNull 97 : object -> targetRef.equals(object); 98 } 99 }
根据接口说明,Predicate 提供的为逻辑判断操作,即断言。
静态方法isEqual:判断是否相等,并返回一个Predicate对象
调用:Predicate.isEqual(Object1).test(Object2)
含义:使用Object1的equals方法判断Object2是否与其相等。
默认方法and,or,negate:分别代表逻辑判断与、或、非并都返回一个Predicate对象
调用:Predicate1.and(Predicate2).test(Object)
含义:判断Object对象是否满足Predicate1 && Predicate2
1 package org.htsg; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Objects; 6 import java.util.function.Predicate; 7 8 /** 9 * @author HTSG 10 */ 11 public class PredicateTest { 12 public static void main(String[] args) { 13 // 添加十个学生 14 ListstudentList = 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 boolean filterAge1(Student student) { 43 return student.getAge() > 15; 44 } 45 46 public static boolean filterAge2(Student student) { 47 return student.getAge() <= 15; 48 } 49 50 public static boolean filterName(Student student) { 51 return "student7".equals(student.getName()); 52 } 53 54 public static List test(List students, Predicate pre) { 55 List result = new ArrayList<>(10); 56 for (Student student : students) { 57 if (pre.test(student)) { 58 result.add(student); 59 } 60 } 61 return result; 62 } 63 64 public static List and(List students, Predicate pre1, Predicate pre2) { 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 return result; 72 } 73 74 public static List negate(List students, Predicate pre) { 75 List result = new ArrayList<>(10); 76 for (Student student : students) { 77 if (pre.negate().test(student)) { 78 result.add(student); 79 } 80 } 81 return result; 82 } 83 84 public static List or(List students, Predicate pre1, Predicate pre2) { 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 return result; 92 } 93 94 public static List isEqual(List students, 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 return result; 102 } 103 104 // 创建静态内部类 105 public static class Student { 106 private String name; 107 private int age; 108 109 public Student() { 110 } 111 112 public Student(String name, int age) { 113 this.name = name; 114 this.age = age; 115 } 116 117 public String getName() { 118 return name; 119 } 120 121 public void setName(String name) { 122 this.name = name; 123 } 124 125 public int getAge() { 126 return age; 127 } 128 129 public void setAge(int age) { 130 this.age = age; 131 } 132 133 @Override 134 public String toString() { 135 return "Student{" + 136 "name='" + name + '\'' + 137 ", age=" + age + 138 '}'; 139 } 140 141 @Override 142 public boolean equals(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 }