首页  >  文章  >  Java  >  Java 谓词

Java 谓词

王林
王林原创
2024-08-30 15:58:16999浏览

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 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 Predicate negate method whenever defined within the package.
  • default Predicate or(Predicate 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 isEqual(Object targetRef): 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.

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:

Java 谓词

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:

Java 谓词

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn