进阶day06-常用的函数式接口_Predicate(数据筛选)
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
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 Demo08 {
|
||||
public static void main(String[] args) {
|
||||
/*boolean result = method(new Predicate<String>() {
|
||||
@Override
|
||||
public boolean test(String s) {
|
||||
*//*if (s.length() > 3) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
} *//*
|
||||
return s.length() > 3;
|
||||
}
|
||||
}, "woaijava");*/
|
||||
|
||||
//--------------------使用函数式编程思想优化代码,重点关注做什么,而不是怎么做---------------------
|
||||
// boolean result = method((String s)->{return s.length()>3;},"woaijava");
|
||||
boolean result = method(s-> s.length()>3,"woaijava");
|
||||
|
||||
boolean result1 = method((String s)->{return s.contains("java");},"woaijaa");
|
||||
|
||||
System.out.println("是否满足长度大于3条件:"+result);
|
||||
System.out.println("是否满足包含java:"+result1);
|
||||
}
|
||||
|
||||
//定义出一个返回值为boolean,参数是Predicate接口,String的数据的方法
|
||||
public static boolean method(Predicate<String> predicate, String s) {
|
||||
boolean result = predicate.test(s);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user