JDK中Predicate接口源码解析和使用详解

本文详细介绍了Java中的Predicate接口,包括逻辑与(and)、逻辑或(or)、逻辑非(negate)等方法的使用,展示了如何组合和操作这些逻辑操作符以满足特定条件的测试。
摘要由CSDN通过智能技术生成
  • 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.

  • 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类型参数进行逻辑与操作

  • 返回一个新的Predicate

  • Predicate newPredicate = (t) -> this.test(t) && other.test(t);

  • 如果传入的Predicate为空, 会抛出空指针异常

*/

default Predicate 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取反操作之后的Predicate

  • Predicate newPredicate = (t) -> !test(t);

*/

default Predicate 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.

  • 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类型参数进行逻辑或操作

  • 返回一个新的Predicate

  • Predicate newPredicate = (t) -> this.test(t) || other.test(t);

  • 如果传入的Predicate为空, 会抛出空指针异常

*/

default Predicate 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 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)}

  • 接收一个Object targetRef, 返回一个Predicate类型

  • 返回的Predicate的test方法是用来判断传入的参数是否等于targetRef

  • 如Predicate predicate = Predicate.isEqual(“demo”);

  • 等同于Predicate predicate = t -> “demo”.equals(t);

*/

static Predicate isEqual(Object targetRef) {

return (null == targetRef)

? Objects::isNull
object -> targetRef.equals(object);

}

}

3.Predicate接口各方法使用举例

====================

public class PredicateTest {

// 辅助打印方法

private static void print(Object obj) {

System.out.println(obj);

}

public static void main(String[] args) {

String demeStr=“中国”;

Predicate predicate = item -> demeStr.equals(item);

// 1. test 方法测试

print("1—> " + predicate.test(demeStr));

List list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

// 2. Predicate 返回一个List中的偶数

// list.stream(), 表示将List作为流进行处理, filter()方法接收一个Predicate, toArray是将流转换成数组

Object[] result = list.stream().filter(t -> t % 2 == 0).toArray();

print("2—> " + Arrays.toString(result));

// 3. 测试Predicate的and方法, 打印list中大于3, 小于6的数字

Predicate predicate1 = t -> t > 3;

predicate1 = predicate1.and(t -> t < 6);

result = list.stream().filter(predicate1).toArray();

print("3—> " + Arrays.toString(result));

// 4. 测试Predicate的or方法, 打印list中小于3或大于5的数字

predicate1 = t -> t < 3;

predicate1 = predicate1.or(t -> t > 5);

result = list.stream().filter(predicate1).toArray();

print("4—> " + Arrays.toString(result));

// 5. 测试Predicate的negate方法, 返回list中大于等于3,小于等于5的数字, 即对场景4取反

result = list.stream().filter(predicate1.negate()).toArray();

print("5—> " + Arrays.toString(result));

// 6. 测试静态方法isEqual方法, 个人感觉这个方法没啥用处

predicate = Predicate.isEqual(demeStr);

print("6—> " + predicate.test(demeStr));

print("6—> " + predicate.test(demeStr+“1”));

}

}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

小编在这里分享些我自己平时的学习资料,由于篇幅限制,pdf文档的详解资料太全面,细节内容实在太多啦,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!

程序员代码面试指南 IT名企算法与数据结构题目最优解

这是” 本程序员面试宝典!书中对IT名企代码面试各类题目的最优解进行了总结,并提供了相关代码实现。针对当前程序员面试缺乏权威题目汇总这一-痛点, 本书选取将近200道真实出现过的经典代码面试题,帮助广“大程序员的面试准备做到万无一失。 “刷”完本书后,你就是“题王”!

image.png

《TCP-IP协议组(第4版)》

本书是介绍TCP/IP协议族的经典图书的最新版本。本书自第1版出版以来,就广受读者欢迎。

本书最新版进行」护元,以体境计算机网络技不的最新发展,全书古有七大部分共30草和7个附录:第一部分介绍一些基本概念和基础底层技术:第二部分介绍网络层协议:第三部分介绍运输层协议;第四部分介绍应用层协议:第五部分介绍下一代协议,即IPv6协议:第六部分介绍网络安全问题:第七部分给出了7个附录。

image.png

Java开发手册(嵩山版)

这个不用多说了,阿里的开发手册,每次更新我都会看,这是8月初最新更新的**(嵩山版)**

image.png

MySQL 8从入门到精通

本书主要内容包括MySQL的安装与配置、数据库的创建、数据表的创建、数据类型和运算符、MySQL 函数、查询数据、数据表的操作(插入、更新与删除数据)、索引、存储过程和函数、视图、触发器、用户管理、数据备份与还原、MySQL 日志、性能优化、MySQL Repl ication、MySQL Workbench、 MySQL Utilities、 MySQL Proxy、PHP操作MySQL数据库和PDO数据库抽象类库等。最后通过3个综合案例的数据库设计,进步讲述 MySQL在实际工作中的应用。

image.png

Spring5高级编程(第5版)

本书涵盖Spring 5的所有内容,如果想要充分利用这一领先的企业级 Java应用程序开发框架的强大功能,本书是最全面的Spring参考和实用指南。

本书第5版涵盖核心的Spring及其与其他领先的Java技术(比如Hibemate JPA 2.Tls、Thymeleaf和WebSocket)的集成。本书的重点是介绍如何使用Java配置类、lambda 表达式、Spring Boot以及反应式编程。同时,将与企业级应用程序开发人员分享一些见解和实际经验,包括远程处理、事务、Web 和表示层,等等。

image.png

JAVA核心知识点+1000道 互联网Java工程师面试题

image.png

image.png

企业IT架构转型之道 阿里巴巴中台战略思想与架构实战

本书讲述了阿里巴巴的技术发展史,同时也是-部互联网技 术架构的实践与发展史。

image.png
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
*

[外链图片转存中…(img-qvyLloee-1712640233997)]

[外链图片转存中…(img-fWaKln5G-1712640233997)]

企业IT架构转型之道 阿里巴巴中台战略思想与架构实战

本书讲述了阿里巴巴的技术发展史,同时也是-部互联网技 术架构的实践与发展史。

[外链图片转存中…(img-y5De5lzM-1712640233997)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 26
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值