ホームページ  >  記事  >  Java  >  Java述語

Java述語

王林
王林オリジナル
2024-08-30 15:58:16999ブラウズ

Java Predicate は、java.util.function パッケージの一部としての関数インターフェースであり、メソッドを参照したり、ブール値 true またはブール値でブールベースの関数を評価したりするためのラムダ式のターゲットとして割り当てられる一般的な述語として機能します。 false は、ラムダ式またはメソッド参照のターゲットとして割り当てられます。したがって、オブジェクトと実装の評価条件ルールで構成されるユースケースに基づいて、プログラミングの観点から特定の実装の条件を評価するのに役立つ、これらの述語の割り当てに基づく適切な評価条件が必要です。

広告 このカテゴリーの人気コース JAVA マスタリー - スペシャライゼーション | 78 コース シリーズ | 15 回の模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文

述語の構文フローは次のとおりです。構文をたどってみると、基本的には、プログラム本体のテスト条件を検証するためのメソッド参照への述語として使用される関数インターフェイスであることがわかります。

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 アプリケーションに関する注釈を表します。
  • パブリック インターフェイス Predicate: これは、現在のクラスまたはメソッドをサポートするインターフェイス タイプが述語インターフェイスであることを表します。
  • boolean test(T t): このメソッドは述語インターフェイスの一部です。これは、述語型のターゲットを割り当てるために使用されるラムダ式またはメソッド参照の重要性を定義および評価するために使用される抽象メソッドです。このメソッドは戻り値の型としてブール値を持ちます。また、T は渡される引数であり、インターフェイスとしての述語の入力型です。全体として、このテスト メソッドは、評価条件の満足度に基づいて true または false を返します。

Java では述語はどのように機能しますか?

Java の述語は、プログラマーにとって、よりクリーンで読みやすい形式でコードを作成および作成するための救世主です。これは、テスト ケースの形式を改善し、テスト ケースを強化するのに役立ちます。一般的に、述語は、制約のある条件を評価するのに役立つブール形式のステートメントにすぎません。 Java の述語は基本的に、java.util.function.package から取得した値として述語を割り当てることにより、関数インターフェイスを実装するために使用されます。これにより、パッケージ メソッドが述語パッケージのオブジェクトを渡すターゲットになり、コードベースのメソッド全体または関数に対して true または false で返されるブール値が取得されます。これは、参照を使用してメソッド全体を評価するために使用されるテスト メソッドとそれぞれの関数で構成されます。 Java にはスタンドアロン関数の概念がありません。したがって、これらのインターフェイスからオブジェクトを定義および作成することが行われます。 Iterable.filter() メソッドは、メソッドのオブジェクトと連携して使用できます。述語を使用したラムダ式も良い役割を果たし、述語と簡単に連携できます。

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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:Java コルーチン次の記事:Java コルーチン