探索Java功能接口包的Predicate

本文将介绍五种Predicate接口方法:test(), or(), and(), negate()和 isEqual()。

微信搜索关注《Java学研大本营》,加入读者群,分享更多精彩

Java8带来了其中的 java.util.function 包,其中包含通常与 Lambda 表达式一起使用的已定义功能接口。

阅读这篇文章,需要您已经具有Lambda表达式的基本知识。

Predicate

根据 JDK 1.8 文档的Predicate“表示一个参数的Predicate(布尔值函数)”;这基本上指的是,当您需要检查需要返回布尔值的某些条件时,您可以使用 Predicate。

Predicate Interface 有五个方法,如一个抽象方法;三个默认方法和一个静态方法。

Predicate接口中的方法

test()方法

test() 方法也称为功能接口 Predicate 的功能方法。test() 方法接受一个参数,根据给定的参数评估这个谓词并返回一个布尔值。

示例代码

// predicate interface type assigned to a field
Predicate<Integer> p = n -> (n>20);
// calling the functional method test on the predicate field p
System.out.println(p.test(40)); // output = true

上面创建了一个 Predicate 类型的字段 p。分配了一个 lambda 表达式来检查传递的参数是否大于 20 给字段 p。当函数方法 test() 在任何方法上被调用时,传入的参数被推断并用于代替 lambda 表达式中的 n,在这种情况下(即示例),答案是正确的,因为 40 大于 20。

and() 方法

Predicate接口的三个默认方法之一是 and() 方法。它接受一个 Predicate 类型的参数,而另一个使用点运算符与 and() 方法链接,就像这样。

// syntax to use and() method
P1.and(p2);

and() 方法“返回一个组合predicate,表示predicate和另一个的短路逻辑与”。这简单的意思是 and() 方法通过使用 AND 逻辑表达式将它们的结果组合在一起来评估两个predicate以获得输出。

在评估任何predicate是否为假时,则不评估另一个predicate,因为两者都必须为真;因此在这种特殊情况下输出为false。

示例代码

// array of number
int[] numArray = {2, 5, 10, 20, 30, 40, 45, 47, 50, 56, 60, 63, 70};
// to check if a number is even
Predicate<Integer> p1= n -> (n%2==0);
// to check id number is greater than 55
Predicate<Integer> p2= n -> (n>55);
// looping through number array
for (int num : numArray) {
// using and() method to check for two conditions and calling the functional method test()
if(p1.and(p2).test(num)) {
// print numbers that are even and greater than 55
System.out.println(num);
}
}

or() 方法

or() 方法在许多方面类似于 and() 方法。它接收一个 Predicate 类型的参数,而另一个使用点运算符与它组合,就像这样。

// syntax to use or() method
p1.or(p2);

根据 Java 8 文档,“它返回一个组合predicate,表示这个predicate和另一个predicate的短路逻辑 OR。这仅仅意味着 or() 方法通过组合它们的结果来评估predicate以给出输出。在使用 or 方法评估predicate时,如果第一个给出真值,则不评估另一个,因为 or() 只需要其中一个逻辑为真。

示例代码

// array of numbers
int[] numArray = {2, 5, 10, 20, 30, 40, 45, 47, 50, 56, 60, 63, 70};
// to check if a number is even
Predicate<Integer> p1= n -> (n%2==0);
// to check id number is greater than 55
Predicate<Integer> p2= n -> (n>55);
// looping through array
for (int num : numArray) {
// using or() method to check for two conditions and calling the functional method test()
if(p1.or(p2).test(num)) {
// print numbers that are either even or greater than 55
System.out.println(num);
}
}

negate()方法

negate() 方法返回与 Predicate 的结果相反的结果,negate() 很简单。根据文档“返回一个表示该predicate的逻辑否定的predicate”

示例代码

// array of numbers
int[] numArray = {2, 5, 10, 20, 30, 40, 45, 47, 50, 56, 60, 63, 70};
// to check if a number is even
Predicate<Integer> p1= n -> (n%2==0);
// to check id number is greater than 55
Predicate<Integer> p2= n -> (n>55);
// looping through array
for (int num : numArray) {
// using negate() method get negation of two conditions and calling the functional method test()
if(p1.negate(p2).test(num)) {
// print numbers that are neither even nor greater than 55
System.out.println(num);
}
}

isEqual() 方法

此方法的功能类似于 Objects.equals(Object, Object) 方法来比较两个对象并检查它们是否相等。就像除了功能方法测试之外的其他方法一样。isEqual() 方法返回一个谓词,表示两个对象是否相等。

示例代码

// predicate type field p1 using the isEqual() method
Predicate<String> p1 = Predicate.isEqual("Prince");
// invocating the isEqual() using the functional method test()
System.out.println(p1.test("Prince"));

概括

Predicate 接口有五个方法,包括函数方法(即 test() 方法),它是单个抽象方法,其功能是调用 Predicate 接口中使用的任何方法。其他方法如 or() 执行 OR 逻辑,and() 方法执行 AND 逻辑,negate() 方法执行 NOT 逻辑,以及 isEqual() 方法比较两个 Predicate 对象并给出反映对象相等性的布尔输出。

今天,你学会了吗?

参考文章:

  • Java 8 文档

  • GeeksforGeeks

推荐书单

《项目驱动零起点学Java》

《项目驱动零起点学Java》共分 13 章,围绕 6 个项目和 258 个代码示例,分别介绍了走进Java 的世界、变量与数据类型、运算符、流程控制、方法、数组、面向对象、异常、常用类、集合、I/O流、多线程、网络编程相关内容。《项目驱动零起点学Java》总结了马士兵老师从事Java培训十余年来经受了市场检验的教研成果,通过6 个项目以及每章的示例和习题,可以帮助读者快速掌握Java 编程的语法以及算法实现。扫描每章提供的二维码可观看相应章节内容的视频讲解。

《项目驱动零起点学Java》贯穿6个完整项目,经过作者多年教学经验提炼而得,项目从小到大、从短到长,可以让读者在练习项目的过程中,快速掌握一系列知识点。

马士兵,马士兵教育创始人,毕业于清华大学,著名IT讲师,所讲课程广受欢迎,学生遍布全球大厂,擅长用简单的语言讲授复杂的问题,擅长项目驱动知识的综合学习。马士兵教育获得在线教育“名课堂”奖、“最受欢迎机构”奖。

赵珊珊,从事多年一线开发,曾为国税、地税税务系统工作。拥有7年一线教学经验,多年线上、线下教育的积累沉淀,培养学员数万名,讲解细致,脉络清晰。

购买链接:https://u.jd.com/XwJWF2r

精彩回顾

想要代码干净又整洁?这里有十大原则

通过自学成为开发者的 9 种方法

怎么做一个有产品意识的软件工程师?

微信搜索关注《Java学研大本营》

访问【IT今日热榜】,发现每日技术热点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值