1. Predicate 接口方法
有时候我们需要对某种类型的数据进行判断,从而得到一个boolean值结果。这时可以使用 java.util.function.Predicate 接口
package ListStream;
import java.util.function.Predicate;
/**
* @Description
* @auther 宁宁小可爱
* @create 2020-05-21 9:30
*/
public class PredicateDemo {
public static void main(String[] args) {
// 条件判断的标准是传入的Lambda表达式逻辑,只要字符串长度大于5则认为很长。
testMethod(s -> s.length() > 5);
}
public static void testMethod(Predicate<String> predicate){
boolean result = predicate.test("HelloWorld");
System.out.println("字符串很长么? " + result);
}
}
1.1 Predicate的与或非条件判断
package ListStream;
import java.util.function.Predicate;
/**
* @Description
* @auther 宁宁小可爱
* @create 2020-05-21 9:30
*/
public class PredicateDemo {
public static void main(String[] args) {
// 条件判断的标准是传入的Lambda表达式逻辑 , s包含大写H 与 W
andMethod(s -> s.contains("H") ,s ->s.contains("W"));
// 条件判断的标准是传入的Lambda表达式逻辑 , s包含大写H 或 W
orMethod(s -> s.contains("H"), s -> s.contains("W"));
// 条件判断的标准是传入的Lambda表达式逻辑 , s长度大于5
negateMethod(s -> s.length() > 5);
}
// 与方法 判断一个字符串既要包含大写“H”,又要包含大写“W”,
public static void andMethod(Predicate<String> one,Predicate<String> two){
boolean result = one.and(two).test("HelloWorld");
System.out.println("是否包含大写W与H? " + result);
}
// 或方法 判断一个字符串包含大写“H” 或 包含大写“W”,
public static void orMethod(Predicate<String> one,Predicate<String> two){
boolean result = one.or(two).test("HelloWorld");
System.out.println("是否包含大写W或H? " + result);
}
// 非方法 , 对结果取反
public static void negateMethod(Predicate<String> predicate){
boolean result = predicate.negate().test("HelloWorld");
System.out.println("字符串很长么? " + result);
}
}
2. Function接口
java.util.function.Function<T,R> 接口用来根据一个类型的数据得到另一个类型的数据,前者称为前置条件, 后者称为后置条件。有进有出,所以称为“函数Function”
抽象方法apply
Function 接口中最主要的抽象方法为: R apply(T t) ,根据类型T的参数获取类型R的结果。使用的场景例如: 将 String 类型转换为 Integer 类型
package ListStream;
import java.util.function.Function;
/**
* @Description
* @auther 宁宁小可爱
* @create 2020-05-21 10:11
*/
public class FunctionDemo {
public static void main(String[] args) {
// 最好是通过方法引用的写法
applyMethod(Integer::parseInt);
// 第一个操作是字符串解析成为int数字,第二个操作是乘以10两个操作通过 andThen 按照前后顺序组合到了一 起。
andThenMethod(Integer::parseInt, i -> i *= 10);
}
// apply方法
private static void applyMethod(Function<String,Integer> function){
Integer num = function.apply("10");
System.out.println("apply方法计算结果 :"+num+20);
}
// andThen方法
private static void andThenMethod(Function<String,Integer> one,Function<Integer,Integer> two){
int num = one.andThen(two).apply("10");
System.out.println("andThen方法计算结果 :" + num+20);
}
}
自定义函数模型拼接
- 将字符串截取数字年龄部分,得到字符串
- 将上一步的字符串转换成为int类型的数字
- 将上一步的int数字累加100,得到结果int数字
package ListStream;
import java.util.function.Function;
/**
* @Description
* @auther 宁宁小可爱
* @create 2020-05-21 10:11
*/
public class FunctionDemo {
public static void main(String[] args) {
String str = "宁宁,20";
int age = getAgeNum(str, s -> s.split(",")[1], Integer::parseInt, n -> n += 100);
System.out.println(age);
}
private static int getAgeNum(String str, Function<String, String> one,
Function<String, Integer> two,
Function<Integer, Integer> three) {
return one.andThen(two).andThen(three).apply(str);
}
}
3 总结: 延迟方法与终结方法
在上述学习到的多个常用函数式接口当中,方法可以分成两种:
- 延迟方法:只是在拼接Lambda函数模型的方法,并不立即执行得到结果
- 终结方法:根据拼好的Lambda函数模型,立即执行得到结果值的方法
通常情况下,这些常用的函数式接口中唯一的抽象方法为终结方法,而默认方法为延迟方法。但这并不是绝对的。 下面的表格中进行了方法分类的整理:
接口名称 | 方法名称 | 抽象/默认 | 延迟/终结 |
---|---|---|---|
Supplier | get | 抽象 | 终结 |
Consumer | accept | 抽象 | 终结 |
andThen | 默认 | 延迟 | |
Predicate | test | 抽象 | 终结 |
and | 默认 | 延迟 | |
or | 默认 | 延迟 | |
negate | 默认 | 延迟 | |
Function | apply | 抽象 | 终结 |
andThen | 默认 | 延迟 |