函数式接口概述
函数式接口: 有且仅有一个抽象方法的接口
Java中的函数式编程体现就是Lambda表达式,所以函数式接口可以使用与Lambda使用的接口
只有确保接口中有且仅有一个抽象方法,Java中的Lambda才能顺利进行推断
如何检测一个接口是不是函数式接口?
使用@functionalInterface注解
放在接口定义的上方,如果接口是函数式接口,编译通过,如果不是,编译失败
注意:
我们自己定义函数式接口的时候,@FunctionInterface是可选的,就算我不写这个注解,
只要满足函数式接口定义的条件,也照样是函数式接口,但是建议加上该注解
函数式接口作为方法的参数
如果方法的参数是一个函数式接口,我们可以使用Lambda表达式作为参数传递
startThread(Runable r)
stratThread(() -> System.out.println(Thread.currentThread().getName() + "线程启动了"))
函数式接口作为方法的返回值
如果方法的返回值是一个函数式接口,就可以使用Lambda表达式作为结果返回
private static Comparator<String> getComparator() {
return (s1, s2) -> s1.length() - s2.length();
}
常用函数式接口
Supplier
Supplier<T>: 包含一个无参的方法
T get(): 获得结果
该方法不需要参数,它会按照某种实现逻辑(由Lambda表达式实现)返回一个数据
Supplier<T> 接口也被成为生产型接口,如果我们指定了接口的泛型是什么类型,那么接口中的get方法就会生产什么类型的数据供我们使用
public static void main(String[] args) {
getStr(() -> "HelloWorld");
}
private static void getStr(Supplier<String> supplier) {
System.out.println(supplier.get());
}
Consumer接口
Consumer<T>: 包含俩个方法
void accept(T t): 对给定的参数执行操作
default Consumer<T> andThen(Consumer after): 返回一个组合的Constumer, 依次执行此操作,然后执行after操作
Consumer<T>接口也被成为消费型接口,它消费的数据的数据类型由泛型指定
public static void main(String[] args) {
operatorString("林青霞", (s) -> System.out.println(s));
operatorString("林青霞", (s) -> System.out.println(new StringBuilder(s).reverse().toString()));
System.out.println("--------");
operatorString2("林青霞", (s) -> System.out.println(new StringBuilder(s).reverse().toString()), (s) -> System.out.println(s));
}
private static void operatorString(String s, Consumer<String> consumer) {
consumer.accept(s);
}
private static void operatorString2(String s, Consumer<String> consumer1, Consumer<String> consumer2) {
consumer1.andThen(consumer2).accept(s);
}
Predicate接口
Predicate<T>: 常用的四个方法
boolean test(T t): 对给定的参数进行判断(判断逻辑由Lambda表达式实现), 返回一个布尔值
default Predicate<T> negate(): 返回一个逻辑的否定,对应逻辑非
default Predicate<T> and(Predicate other): 返回一个组合判断,对应短路与
default Predicate<T> or(Predicate other): 返回一个组合判断,对应短路或
Predicate<T>接口通常用于判断参数是否满足指定的条件
public static void main(String[] args) {
boolean b1 = checkString("Hello", s -> s.length() > 8);
boolean b2 = checkString("HelloWorld", s -> s.length() > 8);
System.out.println(b1);
System.out.println(b2);
boolean b3 = checkString2("HelloWorld", s -> s.length() > 5, s -> s.length() < 11);
System.out.println(b3);
boolean b4 = checkString3("HelloWorld", s -> s.length() > 5, s -> s.length() < 3);
System.out.println(b4);
}
private static boolean checkString(String s, Predicate<String> predicate) {
// test方法
// return predicate.test(s);
// !方法
return predicate.negate().test(s);
}
// &方法
private static boolean checkString2(String s,
Predicate<String> predicate1,
Predicate<String> predicate2) {
return predicate1.and(predicate2).test(s);
}
// or方法
private static boolean checkString3(String s,
Predicate<String> predicate1,
Predicate<String> predicate2) {
return predicate1.or(predicate2).test(s);
}
Function函数
Function<T, R>: 常用的俩个方法
R apply(T t): 将此函数应用于给定的参数
default<V> Function andThen(Function after):
返回一个组合函数,首先将该函数应用于输入,然后将after函数应用于结果
Function<T,R>接口通常用于对参数进行处理,转换(处理逻辑由Lambda表达式实现),然后返回一个新的值
public static void main(String[] args) {
convert("100", s -> Integer.parseInt(s));
convert(100, i -> String.valueOf(i + 566));
convert("100", s -> Integer.parseInt(s), integer -> String.valueOf(integer + 566));
convert("林青霞,30",
s -> s.split(",")[1],
s -> Integer.parseInt(s),
integer -> String.valueOf(integer + 7));
}
private static void convert(String s, Function<String, Integer> function) {
Integer apply = function.apply(s);
System.out.println(apply);
}
private static void convert(Integer i, Function<Integer, String> function) {
String apply = function.apply(i);
System.out.println(apply);
}
private static void convert(String s,
Function<String, Integer> function1,
Function<Integer, String> function2) {
String apply = function1.andThen(function2).apply(s);
System.out.println(apply);
}
private static void convert(String s,
Function<String, String> function1,
Function<String, Integer> function2,
Function<Integer, String> function3) {
String apply = function1.andThen(function2).andThen(function3).apply(s);
System.out.println(apply);
}