搜尋

Java謂詞

Aug 30, 2024 pm 03:58 PM
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
}</t>

說明:遍歷語法流程時,可以看出它基本上是一個函數式接口,用作方法引用的謂詞,用於驗證程式主體的測試條件。

  • 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 Predicate negate 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 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));
}
}</integer>

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);
}
}</integer>

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是開發跨平台桌面應用程序的流行選擇?Apr 25, 2025 am 12:23 AM

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runany where”哲學。 1)itusesbytiesebyTecodeThatrunsonAnyJvm-備用Platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

討論可能需要在Java中編寫平台特定代碼的情況。討論可能需要在Java中編寫平台特定代碼的情況。Apr 25, 2025 am 12:22 AM

在Java中編寫平台特定代碼的原因包括訪問特定操作系統功能、與特定硬件交互和優化性能。 1)使用JNA或JNI訪問Windows註冊表;2)通過JNI與Linux特定硬件驅動程序交互;3)通過JNI使用Metal優化macOS上的遊戲性能。儘管如此,編寫平台特定代碼會影響代碼的可移植性、增加複雜性、可能帶來性能開銷和安全風險。

與平台獨立性相關的Java開發的未來趨勢是什麼?與平台獨立性相關的Java開發的未來趨勢是什麼?Apr 25, 2025 am 12:12 AM

Java將通過雲原生應用、多平台部署和跨語言互操作進一步提昇平台獨立性。 1)雲原生應用將使用GraalVM和Quarkus提升啟動速度。 2)Java將擴展到嵌入式設備、移動設備和量子計算機。 3)通過GraalVM,Java將與Python、JavaScript等語言無縫集成,增強跨語言互操作性。

Java的強鍵入如何有助於平台獨立性?Java的強鍵入如何有助於平台獨立性?Apr 25, 2025 am 12:11 AM

Java的強類型系統通過類型安全、統一的類型轉換和多態性確保了平台獨立性。 1)類型安全在編譯時進行類型檢查,避免運行時錯誤;2)統一的類型轉換規則在所有平台上一致;3)多態性和接口機制使代碼在不同平台上行為一致。

說明Java本機界面(JNI)如何損害平台獨立性。說明Java本機界面(JNI)如何損害平台獨立性。Apr 25, 2025 am 12:07 AM

JNI會破壞Java的平台獨立性。 1)JNI需要特定平台的本地庫,2)本地代碼需在目標平台編譯和鏈接,3)不同版本的操作系統或JVM可能需要不同的本地庫版本,4)本地代碼可能引入安全漏洞或導致程序崩潰。

是否有任何威脅或增強Java平台獨立性的新興技術?是否有任何威脅或增強Java平台獨立性的新興技術?Apr 24, 2025 am 12:11 AM

新興技術對Java的平台獨立性既有威脅也有增強。 1)雲計算和容器化技術如Docker增強了Java的平台獨立性,但需要優化以適應不同雲環境。 2)WebAssembly通過GraalVM編譯Java代碼,擴展了其平台獨立性,但需與其他語言競爭性能。

JVM的實現是什麼,它們都提供了相同的平台獨立性?JVM的實現是什麼,它們都提供了相同的平台獨立性?Apr 24, 2025 am 12:10 AM

不同JVM實現都能提供平台獨立性,但表現略有不同。 1.OracleHotSpot和OpenJDKJVM在平台獨立性上表現相似,但OpenJDK可能需額外配置。 2.IBMJ9JVM在特定操作系統上表現優化。 3.GraalVM支持多語言,需額外配置。 4.AzulZingJVM需特定平台調整。

平台獨立性如何降低發展成本和時間?平台獨立性如何降低發展成本和時間?Apr 24, 2025 am 12:08 AM

平台獨立性通過在多種操作系統上運行同一套代碼,降低開發成本和縮短開發時間。具體表現為:1.減少開發時間,只需維護一套代碼;2.降低維護成本,統一測試流程;3.快速迭代和團隊協作,簡化部署過程。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具