Java 谓词
Java Predicate 是一个函数式接口,作为 java.util.function 包的一部分,充当通用谓词,指定为 lambda 表达式的目标,用于引用任何方法或使用 true 或布尔值评估任何基于布尔值的函数false 被指定为任何 lambda 表达式或任何方法引用的目标;因此,它需要基于这些谓词的分配的适当的评估条件,这有助于他们根据由对象组成的用例和实现的评估条件规则来评估特定实现的任何条件。
广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法
谓词的语法流程如所示。如果你遍历一下语法,那么它基本上就是一个函数式接口,用作方法引用的谓词,用于验证程序主体的测试条件。
package java.util.function; import java.util.Objects; @FunctionalInterface public interface Predicate<T> { boolean test(T t); // Body of the Program }
说明:遍历语法流程时,可以看出它基本上是一个函数式接口,用作方法引用的谓词,用于验证程序主体的测试条件。
- package java.util.function:这表示用于支持与谓词函数相关的方法和函数的函数的包。
- import java.util.Objects: 这个导入的包负责支持根据功能接口创建的对象。
- @FunctionalInterface:这表示与 Spring Boot 应用程序相关的注解,应用程序或其支持的接口使用的是谓词,即函数式接口。
-
public interface Predicate
: 这表示支持当前类或方法的接口类型是谓词接口。 - 布尔测试(T t):此方法是谓词接口的一部分。它是一个抽象方法,用于定义和评估 lambda 表达式的重要性或用于分配谓词类型目标的方法引用。该方法将使用布尔值作为返回类型。此外,T 是传递的参数,它是谓词作为接口的输入类型。总的来说,这个测试方法将根据评估条件的满足程度返回 true 或 false。
谓词在 Java 中如何工作?
java中的谓词是程序员的救世主,可以让他们以更干净和可读的格式编写和创建代码。它有助于更好地格式化测试用例并增强测试用例。一般来说,谓词只是一个布尔格式的语句,有助于评估带有约束的条件。 java 中的谓词基本上用于通过将谓词指定为从 java.util.function.package 获取的值来实现函数式接口。它使包方法成为传递谓词包的对象的目标,以获取整个方法或代码库的函数返回的布尔值(true 或 false)。它由一个测试方法组成,用于评估整个方法以及参考文献和各自的功能。在Java中,没有独立函数的概念;因此,它所做的就是从这些接口定义和创建对象。 Iterable.filter() 方法可以与方法的对象配合使用。带谓词的 Lambda 表达式也发挥了很好的作用,并且可以轻松地与谓词配合使用。
Java 谓词中的方法
有许多方法使用 Java 谓词方法,如下所示:
- boolean test(T t ): This method is used for evaluating the present predicate based on the passed argument t.
-
default Predicate
and(Predicate super T> other): The return type of this method is the composed predicate based on the AND logical operator of short-circuiting that will predicate and then the value for the other predicate if throws the value as false then there will be no evaluation with any argument or other logical operators. -
default Predicate
negate(): Any negated value, i.e. a negative value, will get returned using the default Predicatenegate method whenever defined within the package. -
default Predicate
or(Predicate super T> other): This behavior is also the same as and default predicate with a mere difference that the short-circuit will predicate and follow the value for the other predicate and is any of the value gets out to be true then only the other value will become false. -
static
Predicate This method returns a predicate value which will be used for testing whether the two objects have similar values or both have some transformed values.isEqual(Object targetRef):
Thus, using these methods with the predicate helps in evaluating any of the conditions defined with the predicate’s method types.
Examples to Implement Java Predicate
Below are examples mentioned:
Example #1
This program demonstrates the creation of a simple Predicate and then calling that Predicate method later point of time for evaluation condition with a test method as shown.
Code:
import java.util.function.Predicate; public class Frst_Java_Predicate_Ex { public static void main(String[] args) { Predicate<Integer> prdc = vl -> (vl > 20); System.out.println(prdc.test(80)); } }
Output:
Example #2
This program demonstrates the predicate value with the boolean constraint evaluating the marks of the student by using a predicate where a constraint says that if the student secures marks more than 35, then that student passes; otherwise, the student will fail if it returns the value false.
Code:
import java.util.function.Predicate; public class Predict_Java_ex_2 { static Boolean checkMarks(int marks){ if(marks>35) return true; else return false; } public static void main(String[] args){ Predicate<Integer> pred = Predict_Java_ex_2::checkMarks; boolean rslt = pred.test(15); System.out.println("If marks is more than 35 then only the student will get pass otherwise fail." + rslt); } }
Output:
Conclusion
Java Predicate is one of the new packages and utility being introduced in java 8, which is very flexible with the lambda expressions and helps the programmers create a neat and clean enhanced code for reference and understanding. A predicate with the values helps in the evaluation of the conditions with the return types and values.
以上是Java 谓词的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

Java是热门编程语言,适合初学者和经验丰富的开发者学习。本教程从基础概念出发,逐步深入讲解高级主题。安装Java开发工具包后,可通过创建简单的“Hello,World!”程序实践编程。理解代码后,使用命令提示符编译并运行程序,控制台上将输出“Hello,World!”。学习Java开启了编程之旅,随着掌握程度加深,可创建更复杂的应用程序。
