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);
}
}
}

Java Predicate的更多相关文章

  1. J2SE 8的Lambda --- functions

    functions //1. Runnable 输入参数:无 返回类型void new Thread(() -> System.out.println("In Java8!" ...

  2. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  3. lambda -- Java 8 find first element by predicate

        Java 8 find first element by predicate up vote6down votefavorite I've just started playing with ...

  4. Java 8 基础教程 - Predicate

    在Java 8中,Predicate是一个函数式接口,可以被应用于lambda表达式和方法引用.其抽象方法非常简单: /** * Evaluates this predicate on the giv ...

  5. Java 8 新特性:4-断言(Predicate)接口

    (原) 这个接口主要用于判断,先看看它的实现,说明,再给个例子. /* * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All ri ...

  6. 一点一点看JDK源码(五)java.util.ArrayList 后篇之removeIf与Predicate

    一点一点看JDK源码(五)java.util.ArrayList 后篇之removeIf与Predicate liuyuhang原创,未经允许禁止转载 本文举例使用的是JDK8的API 目录:一点一点 ...

  7. java代码之美(13)--- Predicate详解

    java代码之美(13)--- Predicate详解 遇到Predicate是自己在自定义Mybatis拦截器的时候,在拦截器中我们是通过反射机制获取对象的所有属性,再查看这些属性上是否有我们自定义 ...

  8. Java常用函数式接口--Predicate接口使用案例

    Java常用函数式接口--Predicate接口使用案例 该方法可以使用and来优化: 调用:

  9. java 8中 predicate chain的使用

    目录 简介 基本使用 使用多个Filter 使用复合Predicate 组合Predicate Predicate的集合操作 总结 java 8中 predicate chain的使用 简介 Pred ...

随机推荐

  1. 手写hashmap算法

    /** * 01.自定义一个hashmap * 02.实现put增加键值对,实现key重复时替换key的值 * 03.重写toString方法,方便查看map中的键值对信息 * 04.实现get方法, ...

  2. [BZOJ] 地精部落

    问题描述 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为 N 的山脉 H 可分 为从左到右的 N 段,每段有一个独一无二的高度 Hi,其中 Hi ...

  3. hdu 6050: Funny Function (2017 多校第二场 1006) 【找规律】

    题目链接 暴力打个表找下规律就好了,比赛时看出规律来了倒是,然而看这道题看得太晚了,而且高中的那些数列相关的技巧生疏了好多,然后推公式就比较慢..其实还是自身菜啊.. 公式是 #include< ...

  4. 自定义日志注解 + AOP实现记录操作日志

      需求:系统中经常需要记录员工的操作日志和用户的活动日志,简单的做法在每个需要的方法中进行日志保存操作, 但这样对业务代码入侵性太大,下面就结合AOP和自定义日志注解实现更方便的日志记录   首先看 ...

  5. 小陈现有2个任务A,B要完成,每个任务分别有若干步骤如下 一道网上没啥题解的难题(至少我是这么觉得的)

    小陈现有2个任务A,B要完成,每个任务分别有若干步骤如下:A=a1->a2->a3,B=b1->b2->b3->b4->b5.在任何时候,小陈只能专心做某个任务的一 ...

  6. 【CF1243C】 Tile Painting【思维】

    题意:给定长度为n的方块,要求染色,需要满足:当|j-i|>1且n%|j-i|==0时,两格颜色相同,求做多可以染多少种颜色 题解:求出n的所有质因子 1.若只有一种质因子,则答案为该质因子 2 ...

  7. Oulipo (poj3461

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29759   Accepted: 11986 Descript ...

  8. vue路由vue-router的安装和使用

    1.安装,如果你没有在创建项目时候选择的情况下  cnpm install vue-router 2.使用    假设App为根组件,两个自定义组件home及list main.js里操作 impor ...

  9. 数据挖掘与CRM

    数据挖掘与CRM 现在的数据挖掘项目多数都是游击战,这边挖一挖那边挖一挖,挖到最后还是一场空,还落了个"忽悠"绰号:回想数据挖掘的一个标准流程,那只是一个数据挖掘类项目的标杆而已, ...

  10. quick bi dashboard 控件样式控制。

    控件样式控制 1 想要的效果图 2 查询样式里面进行设置