Files
javaSE-0113/javaSE-day07/src/com/inmind/functional_interface02/Demo08.java

46 lines
1.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}