标签(空格分隔): Java
目前由于系统已经全面切换为 JDK8,所以有必要系统的了解一下 Java8 的一些新特性,以便后续在日常工作中可以使用一些高级特性来提高编程效率。
因为 Java8 引入了函数式接口,在 java.util.function 包含了几大类函数式接口声明。这里第一篇主要研究一下 Function 相关的接口。
Java8 的新引入,包含函数式的设计,接口都有
的注解。就像这个注解的注释说明一样,它注解在接口层面,且注解的接口要有且仅有一个抽象方法。具体就是说,注解在 Inteface 上,且 interface 里只能有一个抽象方法,可以有 default 方法。因为从语义上来讲,一个函数式接口需要通过一个 *** 逻辑上的 *** 方法表达一个单一函数。那理解这个单一就很重要了,单一不是说限制你一个 interface 里只有一个抽象方法,单是多个方法的其他方法需要是继承自 Object 的 public 方法,或者你要想绕过,就自己实现 default。函数式接口自己本身一定是只有一个抽象方法。同时,如果是 Object 类的 public 方法,也是不允许的。官方的说明翻译如下:
- @FunctionalInterface
怎么理解,看几个例子。
比如:你声明一个接口:
- @FunctionalInterface
- public interface Func {
- }
这会编译错,编译器会告诉你 * no target method*。而如果加一个方法:
- @FunctionalInterface
- public interface Func {
- void run();
- }
这就 OK 了,一个函数式接口声明好了。再加一个呢?
- @FunctionalInterface
- public interface Func {
- void run();
- void foo();
- }
不 ok,明确说了只有一个抽象方法嘛。但是如果换一种函数签名:
- @FunctionalInterface
- public interface Func {
- boolean equals(Object obj);
- }
错误依旧,因为这个方法签名是 Object 类的 public 方法。而再改一下:
- @FunctionalInterface
- public interface Func {
- boolean equals(Object obj);
- void run();
- }
这就 OK 了。一个抽象方法,一个 Object 的 public 方法,相安无事。Object 还有其他方法,clone 方法试试会怎么样?
- @FunctionalInterface
- public interface Func {
- Object clone();
- void run();
- }
这又不行了,因为前面明确说了,要是 Object 的 public 方法,而 clone 是 protected 的。
所以总结一句话就是:
因为 Java 本身支持多接口实现,你定义一个 Class 可以 implements 多个 interface。所以这个限制也没什么影响,如果想约定一个函数式接口来统一,也可以做一些默认的实现来达到一个接口多个抽象方法的目的,比如下面这种做法:
一个普通接口 NonFunc:
- public interface NonFunc {
- void foo();
- void voo();
- }
函数式接口 Func:
- @FunctionalInterface
- public interface Func extends NonFunc {
- void run();
- default void foo() {
- // do something
- }
- default void voo() {
- // do something
- }
- }
实现的测试类:
- public class TestJ8FunctionalInterface implements Func {
- public static void main(String[] args) {
- Func func = new TestJ8FunctionalInterface();
- func.run();
- func.foo();
- func.voo();
- }
- @Override
- public void run() {
- System.out.println("run");
- }
- @Override
- public void foo() {
- System.out.println("foo");
- }
- @Override
- public void voo() {
- System.out.println("voo");
- }
- }
函数式接口的一大特性就是可以被 lambda 表达式和函数引用表达式代替。也就是说声明这样的接口,是可以灵活的以方法来传参。看个例子:
- public class TestJ8FunctionalInterface2 {
- public static void main(String[] args) {
- TestJ8FunctionalInterface2 testJ8FunctionalInterface2 = new TestJ8FunctionalInterface2();
- // lambda
- testJ8FunctionalInterface2.test(10, () -> System.out.println("A customed Func."));
- // method reference
- testJ8FunctionalInterface2.test(100, testJ8FunctionalInterface2::customedFunc);
- }
- public void customedFunc() {
- System.out.println("A customed method reference.");
- }
- public void test(int x, Func func) {
- System.out.println(x);
- func.run();
- }
- }
上面例子列举了一个 lambda 模式和一个方法引用模式,这样就可以利用函数式编程强大的能力,将方法作为参数了。
另一个大的话题是针对上文的 *** 逻辑上的方法 ***。所谓逻辑上,就是说当你出现函数式接口多重继承其他接口时,如果继承的多个接口有相同的方法签名,那么也是 OK 的。而这种相同签名的方法,也包括了泛型的情况,以下的声明中的 Z 接口,都是函数式接口。
- interface X {
- int m(Iterable < String > arg);
- }
- interface Y {
- int m(Iterable < String > arg);
- }
- interface Z extends X,
- Y {}
- interface X {
- int m(Iterable < String > arg);
- }
- interface Y {
- int m(Iterable < String > arg);
- }
- interface Z extends X,
- Y {}
但是要注意的是,这种泛型的支持,是因为函数式接口的官方声明规范里要求类型可替换和子签名,不是因为泛型擦除。
比如下面的例子就不是函数式接口:
- interface X {
- int m(Iterable < String > arg);
- }
- interface Y {
- int m(Iterable < Integer > arg);
- }
- interface Z extends X,
- Y {}
- interface X {
- int m(Iterable < String > arg, Class c);
- }
- interface Y {
- int m(Iterable arg, Class < ?>c);
- }
- interface Z extends X,
- Y {}
- interface X < T > {
- void m(T arg);
- }
- interface Y < T > {
- void m(T arg);
- }
- interface Z < A,
- B > extends X < A > ,
- Y < B > {}
最后,Java8 里关于函数式接口的包是
,里面全部是函数式接口。主要包含几大类:Function、Predicate、Supplier、Consumer 和 * Operator(没有 Operator 接口,只有类似 BinaryOperator 这样的接口)。后面依次展开详细说明一下。
- java.util.function
关于 Function 接口,其接口声明是一个函数式接口,其抽象表达函数为
- @FunctionalInterface
- public interface Function<T, R> {
- R apply(T t);
- ...
- }
函数意为将参数 T 传递给一个函数,返回 R。即 $R=Function(T)$
其默认实现了 3 个 default 方法,分别是 compose、andThen 和 identity,对应的函数表达为:compose 对应 $V=Function(ParamFunction(T))$,体现嵌套关系;andThen 对应 $V=ParamFunction(Function(T))$,转换了嵌套的顺序;还有 identity 对应了一个传递自身的函数调用对应 $Function(T)=T$。从这里看出来,compose 和 andThen 对于两个函数 f 和 g 来说,
等价于
- f.compose(g)
。看个例子:
- g.andThen(f)
- public class TestFunction {
- public static void main(String[] args) {
- Function < Integer,
- Integer > incr1 = x - >x + 1;
- Function < Integer,
- Integer > multiply = x - >x * 2;
- int x = 2;
- System.out.println("f(x)=x+1,when x=" + x + ", f(x)=" + incr1.apply(x));
- System.out.println("f(x)=x+1,g(x)=2x, when x=" + x + ", f(g(x))=" + incr1.compose(multiply).apply(x));
- System.out.println("f(x)=x+1,g(x)=2x, when x=" + x + ", g(f(x))=" + incr1.andThen(multiply).apply(x));
- System.out.println("compose vs andThen:f(g(x))=" + incr1.compose(multiply).apply(x) + "," + multiply.andThen(incr1).apply(x));
- }
- }
只是普通的 lambda 表达式,其能力有限。我们会希望引入更强大的函数能力——高阶函数,可以定义任意同类计算的函数。
- Function < Integer,
- Function < Integer,
- Integer >> makeAdder = z - >y - >z + y;
比如这个函数定义,参数是 z,返回值是一个 Function,这个 Function 本身又接受另一个参数 y,返回 z+y。于是我们可以根据这个函数,定义任意加法函数:
- //high order function
- Function < Integer,
- Function < Integer,
- Integer >> makeAdder = z - >y - >z + y;
- x = 2;
- //define add1
- Function < Integer,
- Integer > add1 = makeAdder.apply(1);
- System.out.println("f(x)=x+1,when x=" + x + ", f(x)=" + add1.apply(x));
- //define add5
- Function < Integer,
- Integer > add5 = makeAdder.apply(5);
- System.out.println("f(x)=x+5,when x=" + x + ", f(x)=" + add5.apply(x));
由于高阶函数接受一个函数作为参数,结果返回另一个函数,所以是典型的函数到函数的映射。
BiFunction 提供了二元函数的一个接口声明,举例来说:
- //binary function
- BiFunction < Integer,
- Integer,
- Integer > multiply = (a, b) - >a * b;
- System.out.println("f(z)=x*y, when x=3,y=5, then f(z)=" + multiply.apply(3, 5));
其输出结果将是:
。 二元函数没有 compose 能力,只是默认实现了 andThen。
- f(z)=x*y, when x=3,y=5, then f(z)=15
有了一元和二元函数,那么可以通过组合扩展出更多的函数可能。
Function 接口相关的接口包括:
接受两个参数,返回一个值,代表一个二元函数;
- R apply(T t, U u);
只处理 double 类型的一元函数;
- R apply(double value);
只处理 int 参数的一元函数;
- R apply(int value);
只处理 long 参数的一元函数;
- R apply(long value);
返回 double 的一元函数;
- double applyAsDouble(T value);
返回 double 的二元函数;
- double applyAsDouble(T t, U u);
返回 int 的一元函数;
- int applyAsInt(T value);
返回 int 的二元函数;
- int applyAsInt(T t, U u);
返回 long 的一元函数;
- long applyAsLong(T value);
返回 long 的二元函数;
- long applyAsLong(T t, U u);
接受 double 返回 int 的一元函数;
- int applyAsInt(double value);
接受 double 返回 long 的一元函数;
- long applyAsLong(double value);
接受 int 返回 double 的一元函数;
- double applyAsDouble(int value);
接受 int 返回 long 的一元函数;
- long applyAsLong(int value);
接受 long 返回 double 的一元函数;
- double applyAsDouble(long value);
接受 long 返回 int 的一元函数;
- int applyAsInt(long value);
Operator 其实就是 Function,函数有时候也叫作算子。算子在 Java8 中接口描述更像是函数的补充,和上面的很多类型映射型函数类似。
算子 Operator 包括:UnaryOperator 和 BinaryOperator。分别对应单元算子和二元算子。
算子的接口声明如下:
- @FunctionalInterface
- public interface UnaryOperator<T> extends Function<T, T> {
- static <T> UnaryOperator<T> identity() {
- return t -> t;
- }
二元算子的声明:
- @FunctionalInterface
- public interface BinaryOperator<T> extends BiFunction<T,T,T> {
- /**
- * Returns a {@link BinaryOperator} which returns the lesser of two elements
- * according to the specified {@code Comparator}.
- *
- * @param <T> the type of the input arguments of the comparator
- * @param comparator a {@code Comparator} for comparing the two values
- * @return a {@code BinaryOperator} which returns the lesser of its operands,
- * according to the supplied {@code Comparator}
- * @throws NullPointerException if the argument is null
- */
- public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
- Objects.requireNonNull(comparator);
- return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
- }
- /**
- * Returns a {@link BinaryOperator} which returns the greater of two elements
- * according to the specified {@code Comparator}.
- *
- * @param <T> the type of the input arguments of the comparator
- * @param comparator a {@code Comparator} for comparing the two values
- * @return a {@code BinaryOperator} which returns the greater of its operands,
- * according to the supplied {@code Comparator}
- * @throws NullPointerException if the argument is null
- */
- public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
- Objects.requireNonNull(comparator);
- return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
- }
- }
很明显,算子就是一个针对同类型输入输出的一个映射。在此接口下,只需声明一个泛型参数 T 即可。对应上面的例子:
- public class TestOperator {
- public static void main(String[] args) {
- UnaryOperator<Integer> add = x -> x + 1;
- System.out.println(add.apply(1));
- BinaryOperator<Integer> addxy = (x, y) -> x + y;
- System.out.println(addxy.apply(3, 5));
- BinaryOperator<Integer> min = BinaryOperator.minBy((o1, o2) -> o1 - o2);
- System.out.println(min.apply(100, 200));
- BinaryOperator<Integer> max = BinaryOperator.maxBy((o1, o2) -> o1 - o2);
- System.out.println(max.apply(100, 200));
- }
- }
例子里补充一点的是,BinaryOperator 提供了两个默认的 static 快捷实现,帮助实现二元函数 min(x,y) 和 max(x,y),使用时注意的是排序器可别传反了:)
其他的 Operator 接口:(不解释了)
- long applyAsLong(long operand);
- int applyAsInt(int operand);
- double applyAsDouble(double operand);
- double applyAsDouble(double left, double right);
- int applyAsInt(int left, int right);
- long applyAsLong(long left, long right);
predicate 是一个谓词函数,主要作为一个谓词演算推导真假值存在,其意义在于帮助开发一些返回 bool 值的 Function。本质上也是一个单元函数接口,其抽象方法 test 接受一个泛型参数 T,返回一个 boolean 值。等价于一个 Function 的 boolean 型返回值的子集。
- @FunctionalInterface
- public interface Predicate<T> {
- boolean test(T t);
- ...
- }
其默认方法也封装了 and、or 和 negate 逻辑。写个小例子看看:
- public class TestJ8Predicate {
- public static void main(String[] args) {
- TestJ8Predicate testJ8Predicate = new TestJ8Predicate();
- testJ8Predicate.printBigValue(10, val -> val > 5);
- testJ8Predicate.printBigValueAnd(10, val -> val > 5);
- testJ8Predicate.printBigValueAnd(6, val -> val > 5);
- //binary predicate
- BiPredicate<Integer, Long> biPredicate = (x, y) -> x > 9 && y < 100;
- System.out.println(biPredicate.test(100, 50L));
- }
- public void printBigValue(int value, Predicate<Integer> predicate) {
- if (predicate.test(value)) {
- System.out.println(value);
- }
- }
- public void printBigValueAnd(int value, Predicate<Integer> predicate) {
- if (predicate.and(v -> v < 8).test(value)) {
- System.out.println("value < 8 : " + value);
- } else {
- System.out.println("value should < 8 at least.");
- }
- }
- }
Predicate 在 Stream 中有应用,Stream 的 filter 方法就是接受 Predicate 作为入参的。这个具体在后面使用 Stream 的时候再分析深入。
其他 Predicate 接口:
接受两个参数的二元谓词
- boolean test(T t, U u);
入参为 double 的谓词函数
- boolean test(double value);
入参为 int 的谓词函数
- boolean test(int value);
入参为 long 的谓词函数
- boolean test(long value);
看名字就可以想到,这像谓词函数接口一样,也是一个 Function 接口的特殊表达——接受一个泛型参数,不需要返回值的函数接口。
- @FunctionalInterface
- public interface Consumer<T> {
- void accept(T t);
- ...
- }
这个接口声明太重要了,对于一些纯粹 consume 型的函数,没有 Consumer 的定义真无法被 Function 家族的函数接口表达。因为 Function 一定需要一个泛型参数作为返回值类型(当然不排除你使用 Function 来定义,但是一直返回一个无用的值)。比如下面的例子,如果没有 Consumer,类似的行为使用 Function 表达就一定需要一个返回值。
- public class TestJ8Consumer {
- public static void main(String[] args) {
- Consumer < Integer > consumer = System.out: :println;
- consumer.accept(100);
- //use function, you always need one return value.
- Function < Integer,
- Integer >
- function = x - >{
- System.out.println(x);
- return x;
- };
- function.apply(100);
- }
- }
其他 Consumer 接口:
接受两个参数
- void accept(T t, U u);
接受一个 double 参数
- void accept(double value);
接受一个 int 参数
- void accept(int value);
接受一个 long 参数
- void accept(long value);
接受一个泛型参数一个 double 参数
- void accept(T t, double value);
接受一个泛型参数一个 int 参数
- void accept(T t, int value);
接受一个泛型参数一个 long 参数
- void accept(T t, long value);
最后说的是一个叫做 Supplier 的函数接口,其声明如下:
- @FunctionalInterface
- public interface Supplier<T> {
- T get();
- }
其简洁的声明,会让人以为不是函数。这个抽象方法的声明,同 Consumer 相反,是一个只声明了返回值,不需要参数的函数(这还叫函数?)。也就是说 Supplier 其实表达的不是从一个参数空间到结果空间的映射能力,而是表达一种生成能力,因为我们常见的场景中不止是要 consume(Consumer)或者是简单的 map(Function),还包括了 new 这个动作。而 Supplier 就表达了这种能力。
比如你要是返回一个常量,那可以使用类似的做法:
- Supplier<Integer> supplier = () -> 1;
- System.out.println(supplier.get());
这保证 supplier 对象输出的一直是 1。
如果是要利用构造函数的能力呢?就可以这样:
- Supplier<TestJ8Supplier> anotherSupplier;
- for (int i = 0; i < 10; i++) {
- anotherSupplier = TestJ8Supplier::new;
- System.out.println(anotherSupplier.get());
- }
这样的输出可以看到,全部的对象都是 new 出来的。
这样的场景在 Stream 计算中会经常用到,具体在分析 Java 8 中 Stream 的时候再深入。
其他 Supplier 接口:
返回 boolean
- boolean getAsBoolean();
返回 double
- double getAsDouble();
返回 int
- int getAsInt();
返回 long
- long getAsLong();
整个函数式接口的大概总结如下:
名称 | 一元接口 | 说明 | 二元接口 | 说明 |
---|---|---|---|---|
一般函数 | Function | 一元函数,抽象 apply 方法 | BiFunction | 二元函数,抽象 apply 方法 |
算子函数(输入输出同类型) | UnaryOperator | 一元算子,抽象 apply 方法 | BinaryOperator | 二元算子,抽象 apply 方法 |
谓词函数(输出 boolean) | Predicate | 一元谓词,抽象 test 方法 | BiPredicate | 二元谓词,抽象 test 方法 |
消费者(无返回值) | Consumer | 一元消费者函数,抽象 accept 方法 | BiConsumer | 二元消费者函数,抽象 accept 方法 |
供应者(无参数,只有返回值) | Supplier | 供应者函数,抽象 get 方法 | - | - |
来源: