「Java」匿名クラスでPredicateインターフェースを使用するサンプル
環境
Spring Tool Suite 4
JavaSE17
関数
インタフェースPredicate<T>
boolean test(T t)
指定された引数でこの述語を評価します。
パラメータ:
t – 入力引数
戻り値:
入力引数が述語に一致する場合はtrue、それ以外の場合はfalse
使用例
package com.arkgame.study;
import java.util.function.Predicate;
public class PredicatDemo {
private static boolean flg = false;
public static void main(String[] args) {
String tt = "arkgame";
// PredicateのTはメソッドの引数
boolean res = new Predicate<String>() {
@Override
public boolean test(String tt) {// testメソッドは、引数があり booleanの戻り値
if (tt.equals("arkgame")) {
flg = true;
}
return flg;
}
}.test(tt); // testメソッドを呼び出す
System.out.println("結果:" + res);
}
}
package com.arkgame.study;
import java.util.function.Predicate;
public class PredicatDemo {
private static boolean flg = false;
public static void main(String[] args) {
String tt = "arkgame";
// PredicateのTはメソッドの引数
boolean res = new Predicate<String>() {
@Override
public boolean test(String tt) {// testメソッドは、引数があり booleanの戻り値
if (tt.equals("arkgame")) {
flg = true;
}
return flg;
}
}.test(tt); // testメソッドを呼び出す
System.out.println("結果:" + res);
}
}
package com.arkgame.study; import java.util.function.Predicate; public class PredicatDemo { private static boolean flg = false; public static void main(String[] args) { String tt = "arkgame"; // PredicateのTはメソッドの引数 boolean res = new Predicate<String>() { @Override public boolean test(String tt) {// testメソッドは、引数があり booleanの戻り値 if (tt.equals("arkgame")) { flg = true; } return flg; } }.test(tt); // testメソッドを呼び出す System.out.println("結果:" + res); } }
実行結果
結果:true