当前位置: 代码迷 >> 综合 >> JDK8 新特性Function接口
  详细解决方案

JDK8 新特性Function接口

热度:12   发布时间:2023-12-01 20:43:58.0

源码(删除了源码注释):

package java.util.function;import java.util.Objects;@FunctionalInterface
public interface Function<T, R> {R apply(T t);default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {Objects.requireNonNull(before);return (V v) -> apply(before.apply(v));}default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {Objects.requireNonNull(after);return (T t) -> after.apply(apply(t));}static <T> Function<T, T> identity() {return t -> t;}
}

标注为 @FunctionalInterface 的接口被称为函数式接口,该接口只能有一个抽象方法。如果一个接口只有一个抽象方法,则编译器会认为这就是一个函数式接口。是否是一个函数式接口,需要注意的有以下几点:

  • 该注解只能标记在”有且仅有一个抽象方法”的接口上。
  • JDK8 接口中的静态方法和默认方法,都不是抽象方法。
  • 接口默认继承 java.lang.Object,所以如果接口显示声明覆盖了 Object 中的方法,那么也不算抽象方法。
  • 该注解不是必须的,如果一个接口符合”函数式接口”定义,那么加不加该注解都没有影响。加上该注解能够更好地让编译器进行检查。如果编写的不是函数式接口(比如有多个抽象方法),但是加上了@FunctionInterface,那么编译器会报错。
    报错如下图:
    在这里插入图片描述

报错代码如下:

package test;import java.util.function.Function;@FunctionalInterface
public interface myFunction extends Function<String , Integer> {Integer testFunctionalInterface();
}

因为 myFunction 接口继承了 Function 接口,Function 接口里面本来就定义了一个 apply() 抽象方法,因为 myFunction 接口又定义了一个抽象方法,所以不符合”函数式接口”定义了,加上 @FunctionalInterface,就会报错。

使用 Function 接口:

  1. 使用 lambda 表达式直接赋值
Function<String , Integer> get_length = (String s) -> {return s.length();
};
Function<String , String> append = (String s) -> s.concat(" kaven!");
  1. 匿名类实现
Function<Integer , Integer> add = new Function<Integer, Integer>() {@Overridepublic Integer apply(Integer integer) {return integer+100;}
};

生成的 class 代码如下图:
在这里插入图片描述
我觉得两种方式都是给 Function 接口的 apply() 抽象方法进行定义。

测试代码如下:

package test;import java.util.function.Function;public class testFunction {public static void main(String[] args){Function<String , Integer> get_length = (String s) -> {return s.length();};Function<String , String> append = (String s) -> s.concat(" kaven!");System.out.println(append.apply("Welcome"));System.out.println(get_length.compose(append).apply("Welcome"));System.out.println(append.andThen(get_length).apply("Welcome"));Function<Integer , Integer> add = new Function<Integer, Integer>() {@Overridepublic Integer apply(Integer integer) {return integer+100;}};System.out.println(add.apply(100));System.out.println(Function.identity().apply("Kaven"));}
}

输出数据如下:

Welcome kaven!
14
14
200
Kaven
System.out.println(get_length.compose(append).apply("Welcome"));
System.out.println(append.andThen(get_length).apply("Welcome"));

这两行代码的效果是一样的,先调用 append 的 apply() 方法,后调用get_length 的 apply() 方法。
源码也可以看出来:

default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {Objects.requireNonNull(before);return (V v) -> apply(before.apply(v));//先调用 before 的 apply() 方法,后调用 this 的 apply() 方法。
}default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {Objects.requireNonNull(after);return (T t) -> after.apply(apply(t));//先调用 this 的 apply() 方法,后调用 after 的 apply() 方法。
}
static <T> Function<T, T> identity() {return t -> t;
}

Function 接口的静态方法,返回一个 Function<T, T> 实例,并且给这个实例的 apply() 抽象方法赋值了一个 lambda 表达式 t -> t。所以调用 apply(“Kaven”),就会输出 Kaven。

Function 接口可以搭配 BiFunction 接口一起使用(看源码就很清楚了,也删除了源码注释):

package java.util.function;import java.util.Objects;@FunctionalInterface
public interface BiFunction<T, U, R> {R apply(T t, U u);default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {Objects.requireNonNull(after);return (T t, U u) -> after.apply(apply(t, u));}
}

参考博客 : JDK8新特性-java.util.function-Function接口

  相关解决方案