Predicate 接口说明

 /*
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.util.function; import java.util.Objects; /**
* Represents a predicate (boolean-valued function) of one argument.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #test(Object)}.
*
* @param <T> the type of the input to the predicate
*
* @since 1.8
*/
@FunctionalInterface
public interface Predicate<T> { /**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t); /**
* Returns a composed predicate that represents a short-circuiting logical
* AND of this predicate and another. When evaluating the composed
* predicate, if this predicate is {@code false}, then the {@code other}
* predicate is not evaluated.
*
* <p>Any exceptions thrown during evaluation of either predicate are relayed
* to the caller; if evaluation of this predicate throws an exception, the
* {@code other} predicate will not be evaluated.
*
* @param other a predicate that will be logically-ANDed with this
* predicate
* @return a composed predicate that represents the short-circuiting logical
* AND of this predicate and the {@code other} predicate
* @throws NullPointerException if other is null
*/
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
} /**
* Returns a predicate that represents the logical negation of this
* predicate.
*
* @return a predicate that represents the logical negation of this
* predicate
*/
default Predicate<T> negate() {
return (t) -> !test(t);
} /**
* Returns a composed predicate that represents a short-circuiting logical
* OR of this predicate and another. When evaluating the composed
* predicate, if this predicate is {@code true}, then the {@code other}
* predicate is not evaluated.
*
* <p>Any exceptions thrown during evaluation of either predicate are relayed
* to the caller; if evaluation of this predicate throws an exception, the
* {@code other} predicate will not be evaluated.
*
* @param other a predicate that will be logically-ORed with this
* predicate
* @return a composed predicate that represents the short-circuiting logical
* OR of this predicate and the {@code other} predicate
* @throws NullPointerException if other is null
*/
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
} /**
* Returns a predicate that tests if two arguments are equal according
* to {@link Objects#equals(Object, Object)}.
*
* @param <T> the type of arguments to the predicate
* @param targetRef the object reference with which to compare for equality,
* which may be {@code null}
* @return a predicate that tests if two arguments are equal according
* to {@link Objects#equals(Object, Object)}
*/
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
}

根据接口说明,Predicate 提供的为逻辑判断操作,即断言。

静态方法isEqual:判断是否相等,并返回一个Predicate对象

调用:Predicate.isEqual(Object1).test(Object2)

含义:使用Object1的equals方法判断Object2是否与其相等。

默认方法and,or,negate:分别代表逻辑判断与、或、非并都返回一个Predicate对象

调用:Predicate1.and(Predicate2).test(Object)

含义:判断Object对象是否满足Predicate1 && Predicate2

方法test:按照给定的Predicate条件进行逻辑判断。

 package org.htsg;

 import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate; /**
* @author HTSG
*/
public class PredicateTest {
public static void main(String[] args) {
// 添加十个学生
List<Student> studentList = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
studentList.add(new Student("student" + i, 10 + i));
}
// 获取年龄大于15的学生
// [Student{name='student6', age=16}, Student{name='student7', age=17}, Student{name='student8', age=18}, Student{name='student9', age=19}]
List<Student> filteredStudents = test(studentList, PredicateTest::filterAge1);
System.out.println(filteredStudents);
// 获取年龄大于15并且名字叫 "student7" 的学生
// [Student{name='student7', age=17}]
filteredStudents = and(studentList, PredicateTest::filterAge1, PredicateTest::filterName);
System.out.println(filteredStudents);
// 获取年龄不大于15的学生
// [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}]
filteredStudents = negate(studentList, PredicateTest::filterAge1);
System.out.println(filteredStudents);
// 获取年龄不大于15或名字叫 "student7" 的学生
// [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}]
filteredStudents = or(studentList, PredicateTest::filterAge2, PredicateTest::filterName);
System.out.println(filteredStudents);
// 获取和目标学生属性值相同的学生列表
// [Student{name='student1', age=11}]
filteredStudents = isEqual(studentList, new Student("student1", 11));
System.out.println(filteredStudents); } public static boolean filterAge1(Student student) {
return student.getAge() > 15;
} public static boolean filterAge2(Student student) {
return student.getAge() <= 15;
} public static boolean filterName(Student student) {
return "student7".equals(student.getName());
} public static List<Student> test(List<Student> students, Predicate<Student> pre) {
List<Student> result = new ArrayList<>(10);
for (Student student : students) {
if (pre.test(student)) {
result.add(student);
}
}
return result;
} public static List<Student> and(List<Student> students, Predicate<Student> pre1, Predicate<Student> pre2) {
List<Student> result = new ArrayList<>(10);
for (Student student : students) {
if (pre1.and(pre2).test(student)) {
result.add(student);
}
}
return result;
} public static List<Student> negate(List<Student> students, Predicate<Student> pre) {
List<Student> result = new ArrayList<>(10);
for (Student student : students) {
if (pre.negate().test(student)) {
result.add(student);
}
}
return result;
} public static List<Student> or(List<Student> students, Predicate<Student> pre1, Predicate<Student> pre2) {
List<Student> result = new ArrayList<>(10);
for (Student student : students) {
if (pre1.or(pre2).test(student)) {
result.add(student);
}
}
return result;
} public static List<Student> isEqual(List<Student> students, Student student) {
List<Student> result = new ArrayList<>(10);
for (Student studentTemp : students) {
if (Predicate.isEqual(student).test(studentTemp)) {
result.add(studentTemp);
}
}
return result;
} // 创建静态内部类
public static class Student {
private String name;
private int age; public Student() {
} public Student(String name, int age) {
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
} @Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
}
}

最新文章

  1. Docker安装CentOS
  2. Hibernate之lazy延迟加载
  3. C和指针 第十二章 使用结构和指针 双链表和语句提炼
  4. 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
  5. atitit.RESTful服务的概览and框架选型
  6. IOSView显示特性设置
  7. 【转载】VGA时序与原理
  8. 由问题引出的fsck命令
  9. Ubuntu apt-get 错误 -11 -system error
  10. NOIP2005-普及组复赛-第一题-陶陶摘苹果
  11. JSSDK微信自定义分享
  12. Java之反射--练习
  13. Spark SQL笔记——技术点汇总
  14. LeetCode(50)-Word Pattern
  15. 中间件(3)NoSQL
  16. 网络通信中tcp多客户端连接
  17. python3下获取主流浏览器和python的安装路径
  18. ABAP 中JSON格式的转换与解析
  19. (next_permutation) 排列2 hdu 1716
  20. Spark SQL DataFrame新增一列的四种方法

热门文章

  1. 牛客ACM赛 B [小a的旅行计划 ]
  2. 获取不到最新的url地址展示图片可以盖时间戳
  3. Python---字符串拼接和严格字符串
  4. @ControllerAdvice全局数据绑定
  5. Listary安装+破解
  6. 使用tinymce编辑器从word保持原格式复制粘贴的办法
  7. Shiro学习资料
  8. CSS页面乱码 GB2312、UTF-8格式问题解决方案
  9. 移动端rem布局屏幕适配插件(放js中便可使用)
  10. GoldenGate—日常管理