s-day07--常用的函数式接口_Predicate

This commit is contained in:
xx
2026-10-20 13:42:36 +08:00
parent 8cfbad5ae5
commit 7165e6cd08
@@ -0,0 +1,30 @@
package com.inmind.functional_interface02;
import java.util.function.Predicate;
/*
12.常用的函数式接口_Predicate
Interface Predicate<T>
boolean test(T t) 用来判断或者筛选数据
Predicate函数式接口的作用:用来进行数据筛选,判断指定的数据是否符合业务
什么时候要使用Predicat呢??
当我们要定义一个有一个参数,并且返回值类型为boolean的函数式接口时,直接使用Predicate
需求:使用Predicate接口判断指定的字符串长度是否>3(包含java)
*/
public class Demo06 {
public static void main(String[] args) {
//判断指定的字符串长度是否>3
String str = "hello java";
boolean result = testMethod(s -> s.length() > 3, str);
System.out.println(result);//true
}
//定义一个方法,返回值为boolean,参数是Predicate接口,String类型的参数
public static boolean testMethod(Predicate<String> predicate, String s) {
boolean result = predicate.test(s);
return result;
}
}