目录html
函数式接口,首先是一个接口,而后就是在这个接口里面只能有一个抽象方法,可是能够有多个非抽象方法的接口。java
Java 8为函数式接口引入了一个新注解@FunctionalInterface,主要用于编译级错误检查,加上该注解,当你写的接口不符合函数式接口定义的时候,编译器会报错。git
函数式接口能够被隐式转换为 lambda 表达式。github
Java 8的库帮你在java.util.function
包中引入了几个新的函数式接口。咱们接下来介绍 Predicate、Consumer和Function 三种函数式接口。segmentfault
public interface Predicate<T> { boolean test(T t); } public interface Comparator<T> { int compare(T o1, T o2); } @FunctionalInterface public interface Runnable { public abstract void run(); }
java.util.function
中定义了几组类型的函数式接口以及针对基本数据类型的子接口。app
boolean test(T t)
void accept(T t)
R apply(T t)
T get()
接口最终有肯定的类实现, 而类的最终父类是Object。 所以函数式接口能够定义Object的public方法。
如如下的接口依然是函数式接口:ide
@FunctionalInterface public interface ObjectMethodFunctionalInterface { void count(int i); String toString(); //same to Object.toString int hashCode(); //same to Object.hashCode boolean equals(Object obj); //same to Object.equals }
为何限定public
类型的方法呢?由于接口中定义的方法都是public
类型的。 举个例子,下面的接口就不是函数式接口:函数
interface WrongObjectMethodFunctionalInterface { void count(int i); Object clone(); //Object.clone is protected }
由于Object.clone
方法是protected
类型。学习
函数式接口的抽象方法能够声明 可检查异常
(checked exception)。 在调用目标对象的这个方法时必须catch这个异常。ui
public class FunctionalInterfaceWithException { public static void main(String[] args) { InterfaceWithException target = i -> {}; try { target.apply(10); } catch (Exception e) { e.printStackTrace(); } } } @FunctionalInterface interface InterfaceWithException { void apply(int i) throws Exception; }
这和之前的接口/方法调用同样。
可是,若是在Lambda表达式中抛出异常, 而目标接口中的抽象函数没有声明这个可检查, 则此接口不能做为此lambda表达式的目标类型。
public class FunctionalInterfaceWithException { public static void main(String[] args) { InterfaceWithException target = i -> {throw new Exception();}; } } @FunctionalInterface interface InterfaceWithException { void apply(int i); }
上面的例子中不能编译, 由于lambda表达式要求的目标类型和InterfaceWithException
不一样。 InterfaceWithException
的函数没有声明异常。
函数式接口中除了那个抽象方法外还能够包含静态方法。
Java 8之前的规范中接口中不容许定义静态方法。 静态方法只能在类中定义。 Java 8中能够定义静态方法。
一个或者多个静态方法不会影响SAM接口成为函数式接口。
下面的例子中FunctionalInterfaceWithStaticMethod
包含一个SAM: apply
,还有一个静态方法sum
。 它依然是函数式接口。
@FunctionalInterface interface FunctionalInterfaceWithStaticMethod { static int sum(int[] array) { return Arrays.stream(array).reduce((a, b) -> a+b).getAsInt(); } void apply(); } public class StaticMethodFunctionalInterface { public static void main(String[] args) { int sum = FunctionalInterfaceWithStaticMethod.sum(new int[]{1,2,3,4,5}); FunctionalInterfaceWithStaticMethod f = () -> {}; } }
Java 8中容许接口实现方法, 而不是简单的声明, 这些方法叫作默认方法,使用特殊的关键字default
。
由于默认方法不是抽象方法,因此不影响咱们判断一个接口是不是函数式接口。
@FunctionalInterface interface InterfaceWithDefaultMethod { void apply(Object obj); default void say(String name) { System.out.println("hello " + name); } } class FunctionalInterfaceWithDefaultMethod { public static void main(String[] args) { InterfaceWithDefaultMethod i = (o) -> {}; i.apply(null); i.say("default method"); } }
InterfaceWithDefaultMethod
仍然是一个函数式接口。
接口能够继承接口。 若是父接口是一个函数接口, 那么子接口也多是一个函数式接口。 判断标准依据下面的条件:
对于接口
I
, 假定M
是接口成员里的全部抽象方法的继承(包括继承于父接口的方法), 除去具备和Object的public的实例方法签名的方法, 那么咱们能够依据下面的条件判断一个接口是不是函数式接口, 这样能够更精确的定义函数式接口。
若是存在一个一个方法m, 知足:
- m的签名(subsignature)是M中每个方法签名的子签名(signature)
- m的返回值类型是M中的每个方法的返回值类型的替代类型(return-type-substitutable)
那么I就是一个函数式接口。
具体看参考中加粗的文章。
Java 不会强制要求你使用@FunctionalInterface注解来标记你的接口是函数式接口, 然而,做为API做者, 你可能倾向使用@FunctionalInterface指明特定的接口为函数式接口, 这只是一个设计上的考虑, 可让用户很明显的知道一个接口是函数式接口。
@FunctionalInterface public interface SimpleFuncInterface { public void doWork(); }
若是你在一个不是函数式的接口使用@FunctionalInterface标记的话,会出现什么状况?编译时出错。
error: Unexpected @FunctionalInterface annotation @FunctionalInterface ^ I is not a functional interface multiple non-overriding abstract methods found in interface I
@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; } }
其中实现了2个默认方法,分别compose,andThen,对应的函数表达为:
compose对应,体现嵌套关系;
andThen对应,转换了嵌套的顺序;
identity对应了一个传递自身的函数调用对应
从这里看出来,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)); } }
output:
f(x)=x+1,when x=2, f(x)=3 f(x)=x+1,g(x)=2x, when x=2, f(g(x))=5 f(x)=x+1,g(x)=2x, when x=2, g(f(x))=6 compose vs andThen:f(g(x))=5,5
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));
其输出结果将是:f(z)=x*y, when x=3,y=5, then f(z)=15
。
二元函数没有compose能力,只是默认实现了andThen。
有了一元和二元函数,那么能够经过组合扩展出更多的函数可能。
Function接口相关的接口包括:
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> { public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) { Objects.requireNonNull(comparator); return (a, b) -> comparator.compare(a, b) <= 0 ? a : b; } 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接口:(不解释了)
predicate是一个谓词函数,主要做为一个谓词演算推导真假值存在,其意义在于帮助开发一些返回bool值的Function。本质上也是一个单元函数接口,其抽象方法test接受一个泛型参数T,返回一个boolean值。等价于一个Function的boolean型返回值的子集。
@FunctionalInterface public interface Predicate<T> { boolean test(T t); default Predicate<T> and(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) && other.test(t); } default Predicate<T> negate() { return (t) -> !test(t); } default Predicate<T> or(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) || other.test(t); } static <T> Predicate<T> isEqual(Object targetRef) { return (null == targetRef) ? Objects::isNull : object -> targetRef.equals(object); } }
其默认方法也封装了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."); } } }
Output:
10 value should < 8 at least. value < 8 : 6 true
Predicate在Stream中有应用,Stream的filter方法就是接受Predicate做为入参的。这个具体在后面使用Stream的时候再分析深刻。
其余Predicate接口:
看名字就能够想到,这像谓词函数接口同样,也是一个Function接口的特殊表达——接受一个泛型参数,不须要返回值的函数接口。
@FunctionalInterface public interface Consumer<T> { void accept(T t); default Consumer<T> andThen(Consumer<? super T> after) { Objects.requireNonNull(after); return (T t) -> { accept(t); after.accept(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接口:
最后说的是一个叫作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接口:
整个函数式接口的大概总结以下:
名称 | 一元接口 | 说明 | 二元接口 | 说明 |
---|---|---|---|---|
通常函数 | Function | 一元函数,抽象apply方法 | BiFunction | 二元函数,抽象apply方法 |
算子函数(输入输出同类型) | UnaryOperator | 一元算子,抽象apply方法 | BinaryOperator | 二元算子,抽象apply方法 |
谓词函数(输出boolean) | Predicate | 一元谓词,抽象test方法 | BiPredicate | 二元谓词,抽象test方法 |
消费者(无返回值) | Consumer | 一元消费者函数,抽象accept方法 | BiConsumer | 二元消费者函数,抽象accept方法 |
供应者(无参数,只有返回值) | Supplier | 供应者函数,抽象get方法 | - | - |
Java 8函数式接口functional interface的秘密