s-day07- 常用的函数式接口_Predicate
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package com.inmind.funcitonal_predicate05;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/*
|
||||
JDK为了避免各个程序员定义一些重复的无效的函数式接口,所以,它就提供了一些常用的函数式接口
|
||||
常用的函数式接口_Predicate
|
||||
Interface Predicate<T>
|
||||
|
||||
boolean test(T t) 用来判断或者筛选数据
|
||||
|
||||
Predicate函数式接口,用来进行数据筛选,用来判断指定的数据是否符合业务
|
||||
|
||||
什么时候要使用Predicate??
|
||||
当我们开发人员想自定义一个函数式接口,并且该接口中要定义一个接收一个参数,并返回boolean型返回值的抽象方法时,直接使用Predicate
|
||||
|
||||
|
||||
使用Predicate接口判断指定的字符串长度是否>3(包含java)
|
||||
*/
|
||||
public class Demo09 {
|
||||
public static void main(String[] args) {
|
||||
//字符串长度是否>3
|
||||
// method(s->{return s.length()>3;},"woaijava");
|
||||
method(s-> s.length()>10,"woaijava");
|
||||
|
||||
//字符串是否包含java 1个参数,有返回值
|
||||
method(s-> s.contains("java"),"woaijava");
|
||||
}
|
||||
|
||||
public static void method(Predicate<String> predicate, String s) {
|
||||
boolean result = predicate.test(s);
|
||||
System.out.println("判断的结果为:"+result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user