首頁  >  文章  >  Java  >  Java謂詞

Java謂詞

王林
王林原創
2024-08-30 15:58:16998瀏覽

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
上一篇:Java 協程下一篇:Java 協程