Java 8函数式接口functional interface的秘密

2014年10月29日 17:52:55 西瓜可乐520 阅读数:3729
 

目录 [−]

函数式接口(Functional Interface)是Java 8对一类特殊类型的接口的称呼。 这类接口只定义了唯一的抽象方法的接口(除了隐含的Object对象的公共方法), 因此最开始也就做SAM类型的接口(Single Abstract Method)。

为什么会单单从接口中定义出此类接口呢? 原因是在Java Lambda的实现中, 开发组不想再为Lambda表达式单独定义一种特殊的Structural函数类型,称之为箭头类型(arrow type), 依然想采用Java既有的类型系统(class, interface, method等), 但是增加一个结构化的函数类型会增加函数类型的复杂性,破坏既有的Java类型,并对成千上万的Java类库造成严重的影响。 权衡利弊, 最终还是利用SAM 接口作为 Lambda表达式的目标类型。

JDK中已有的一些接口本身就是函数式接口,如Runnable。 JDK 8中又增加了java.util.function包, 提供了常用的函数式接口。

函数式接口代表的一种契约, 一种对某个特定函数类型的契约。 在它出现的地方,实际期望一个符合契约要求的函数。 Lambda表达式不能脱离上下文而存在,它必须要有一个明确的目标类型,而这个目标类型就是某个函数式接口。

当然, Java 8发布快一年了, 你对以上的概念也应该有所了解了,这篇文章也不会介绍这些基础的东西, 而是想深入的探讨函数式接口的概念和应用。

JDK 8之前已有的函数式接口

  • java.lang.Runnable
  • java.util.concurrent.Callable
  • java.security.PrivilegedAction
  • java.util.Comparator
  • java.io.FileFilter
  • java.nio.file.PathMatcher
  • java.lang.reflect.InvocationHandler
  • java.beans.PropertyChangeListener
  • java.awt.event.ActionListener
  • javax.swing.event.ChangeListener

新定义的函数式接口

java.util.function中定义了几组类型的函数式接口以及针对基本数据类型的子类。

  • Predicate — 传入一个参数,返回一个bool结果, 方法为boolean test(T t)
  • Consumer — 传入一个参数,无返回值,纯消费。 方法为void accept(T t)
  • Function — 传入一个参数,返回一个结果,方法为R apply(T t)
  • Supplier — 无参数传入,返回一个结果,方法为T get()
  • UnaryOperator — 一元操作符, 继承Function,传入参数的类型和返回类型相同。
  • BinaryOperator — 二元操作符, 传入的两个参数的类型和返回类型相同, 继承BiFunction

Java API的对函数式接口都已经标明了, 如

1
2
3
4
5
6
7
8
9
10
11
java.lang
Interface Runnable
 
All Known Subinterfaces:
RunnableFuture<V>, RunnableScheduledFuture<V>
 
All Known Implementing Classes:
AsyncBoxView.ChildState, ForkJoinWorkerThread, FutureTask, RenderableImageProducer, SwingWorker, Thread, TimerTask
 
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

函数式接口中可以额外定义多个抽象方法,但这些抽象方法签名必须和Object的public方法一样

接口最终有确定的类实现, 而类的最终父类是Object。 因此, 函数式接口可以定义Object的public方法。
如以下的接口依然是函数式接口:

1
2
3
4
5
6
7
8
@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类型的。 举个例子,下面的接口就不是函数式接口:

1
2
3
4
5
interface WrongObjectMethodFunctionalInterface {
void count(int i);
 
Object clone(); //Object.clone is protected
}

因为Object.clone方法是protected类型。

声明异常

函数式接口的抽象方法可以声明 可检查异常(checked exception)。 在调用目标对象的这个方法时必须catch这个异常。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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表达式的目标类型。

1
2
3
4
5
6
7
8
9
10
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中可以定义静态方法。

1个或者多个静态方法不会影响SAM接口成为函数式接口。
下面的例子中FunctionalInterfaceWithStaticMethod包含一个SAM: apply,还有一个静态方法sum。 它依然是函数式接口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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
因为默认方法是不是抽象方法,所以不影响我们判断一个接口是否是函数式接口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@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就是一个函数式接口。

看几个例子。
1)

1
2
3
interface X { int m(Iterable<String> arg); }
interface Y { int m(Iterable<String> arg); }
interface Z extends X, Y {}

接口Z继承了X,Y接口的m方法,由于这两个方法的签名相同,返回值也一样,所以Z有唯一的一个抽象方法int m(Iterable<String> arg);,可以作为函数式接口。

2)

1
2
3
interface X { Iterable m(Iterable<String> arg); }
interface Y { Iterable<String> m(Iterable arg); }
interface Z extends X, Y {}

方法签名Y.m 既满足签名是X.m,并且返回值也满足return-type-substitutable。所以Z是函数式接口,函数类型为Iterable<String> m(Iterable arg)

3)

1
2
3
interface X { int m(Iterable<String> arg); }
interface Y { int m(Iterable<Integer> arg); }
interface Z extends X, Y {}

编译出错, 没有一个方法的签名是所有方法的子签名:

4)

1
2
3
interface X { int m(Iterable<String> arg, Class c); }
interface Y { int m(Iterable arg, Class<?> c); }
interface Z extends X, Y {}

Compiler error: No method has a subsignature of all abstract methods
编译出错, 没有一个方法的签名是所有方法的子签名

5)

1
2
3
interface X { long m(); }
interface Y { int m(); }
interface Z extends X, Y {}

Compiler error: no method is return type substitutable
编译出错, 返回值类型不同。

6)

1
2
3
interface Foo<T> { void m(T arg); }
interface Bar<T> { void m(T arg); }
interface FooBar<X, Y> extends Foo<X>, Bar<Y> {}

Compiler error: different signatures, same erasure
编译出错

7)

1
2
3
interface Foo { void m(String arg); }
interface Bar<T> { void m(T arg); }
interface FooBar<T> extends Foo, Bar<T> {}

不是一个函数式接口, 两个方法的类型参数不一样。

8)

1
2
3
4
5
interface X { void m() throws IOException; }
interface Y { void m() throws EOFException; }
interface Z { void m() throws ClassNotFoundException; }
interface XY extends X, Y {}
interface XYZ extends X, Y, Z {}

X.m,Y.m,Z.m方法签名相同,返回值类型都是void,只是异常列表不同。 EOFExceptionIOException的子类。
在这种情况下XYXYZ都是函数式接口,但是函数类型不同。
// XY has function type ()->void throws EOFException
// XYZ has function type ()->void (throws nothing)

9)

1
2
3
4
5
6
7
8
9
10
11
interface A {
List<String> foo(List<String> arg) throws IOException, SQLTransientException;
}
interface B {
List foo(List<String> arg) throws EOFException, SQLException, TimeoutException;
}
interface C {
List foo(List arg) throws Exception;
}
interface D extends A, B {}
interface E extends A, B, C {}

// D has function type (List)->List throws EOFException, SQLTransientException
// E has function type (List)->List throws EOFException, SQLTransientException

10)

1
2
3
4
5
6
7
interface G1 {
<E extends Exception> Object m() throws E;
}
interface G2 {
<F extends Exception> String m() throws Exception;
}
interface G extends G1, G2 {}

// G has function type ()->String throws F

函数式接口的交集

1)

1
2
3
4
5
6
7
8
9
10
11
12
public class Z {
public static void main(String[] args) {
Object o = (I & J) () -> {};
}
}
interface I {
void foo();
}
 
interface J {
void foo();
}

I和J方法的交集依然符合函数式接口的定义。 上述代码可以用JDK中的javac编译通过但是Eclipse报错,这是Eclipse的一个bug。

2)

1
2
3
4
5
6
7
8
9
10
11
12
public class Z {
public static void main(String[] args) {
Object o = (I & J) () -> {};
}
}
interface I {
void foo();
}
 
interface J {
void foo();
}

上述代码Eclipse不会报错但是javac无法编译,javac认为 (I & J)不是一个函数式接口。 看起来javac工作正常, Eclipse处理这样的case还有问题。

@FunctionalInterface

Java 不会强制要求你使用@FunctionalInterface注解来标记你的接口是函数式接口, 然而,作为API作者, 你可能倾向使用@FunctionalInterface指明特定的接口为函数式接口, 这只是一个设计上的考虑, 可以让用户很明显的知道一个接口是函数式接口。

1
2
3
4
@FunctionalInterface
public interface SimpleFuncInterface {
public void doWork();
}

如果你在一个不是函数式的接口使用@FunctionalInterface标记的话,会出现什么情况?编译时出错。

1
2
3
4
5
error: Unexpected @FunctionalInterface annotation
@FunctionalInterface
^
I is not a functional interface
multiple non-overriding abstract methods found in interface I

Java 8函数式接口functional interface的秘密的更多相关文章

  1. JAVA 8 函数式接口 - Functional Interface

    什么是函数式接口(Functional Interface) 其实之前在讲Lambda表达式的时候提到过,所谓的函数式接口,当然首先是一个接口,然后就是在这个接口里面只能有一个抽象方法. 这种类型的接 ...

  2. Java8 函数式接口-Functional Interface

    目录 函数式接口: JDK 8之前已有的函数式接口: 新定义的函数式接口: 函数式接口中可以额外定义多个Object的public方法一样抽象方法: 声明异常: 静态方法: 默认方法 泛型及继承关系 ...

  3. java8函数式接口(Functional Interface)

    介绍 函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口. 函数式接口可以被隐式转换为 lambda 表达式(箭头函数). 函数式接口代 ...

  4. Java中的函数式编程(二)函数式接口Functional Interface

    写在前面 前面说过,判断一门语言是否支持函数式编程,一个重要的判断标准就是:它是否将函数看做是"第一等公民(first-class citizens)".函数是"第一等公 ...

  5. Java 8 函数式接口

    函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口. 函数式接口可以被隐式转换为 lambda 表达式. Lambda 表达式和方法引用 ...

  6. Java 8 新特性-菜鸟教程 (3) -Java 8 函数式接口

    Java 8 函数式接口 函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口. 函数式接口可以被隐式转换为lambda表达式. 函数式接 ...

  7. Java之函数式接口@FunctionalInterface详解(附源码)

    Java之函数式接口@FunctionalInterface详解 函数式接口的定义 在java8中,满足下面任意一个条件的接口都是函数式接口: 1.被@FunctionalInterface注释的接口 ...

  8. Java基础-面向接口(interface)编程

    Java基础-面向接口(interface)编程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的“类 ...

  9. Java常用函数式接口--Consumer接口andThen()方法使用案例(二)

    Java常用函数式接口--Consumer接口使用案例

随机推荐

  1. Git(五)IDEA应用Git

    一.IDEA客户端git 1.提交代码到本地仓库 1. 关联Git,创建本地库 关联git 配置git环境变量 设置本地仓库目录,一般是IDEA工作空间,选择VCS->Import into V ...

  2. 字符串匹配的kmp算法 及 python实现

    一:背景 给定一个主串(以 S 代替)和模式串(以 P 代替),要求找出 P 在 S 中出现的位置,此即串的模式匹配问题. Knuth-Morris-Pratt 算法(简称 KMP)是解决这一问题的常 ...

  3. 【LOJ】#2186. 「SDOI2015」道路修建

    题解 就是线段树维护一下转移矩阵 分成两种情况,一种是前面有两个联通块,一种是前面有一个联通块 从一个联通块转移到一个联通块 也就是新加一列的三个边选其中两条即可 从一个联通块转移到两个联通块 不连竖 ...

  4. Spark优化之gc

    对于官方Programming Guides的GC优化一节做了阅读. 在这里记录一下我的理解,可能记录的比较混乱没有条理: 我理解其实GC优化的主要目的就是在你的任务执行中使用更少的内存,进行更少的g ...

  5. P1280 尼克的任务 线性DP

    题目描述 尼克每天上班之前都连接上英特网,接收他的上司发来的邮件,这些邮件包含了尼克主管的部门当天要完成的全部任务,每个任务由一个开始时刻与一个持续时间构成. 尼克的一个工作日为N分钟,从第一分钟开始 ...

  6. vue播放video插件vue-video-player实现hls, rtmp播放全过程

    1.安装插件 1 npm install vue-video-player -S 2.配置插件 在main.js里添加 1 import VideoPlayer from 'vue-video-pla ...

  7. maven打包时包含本地jar

    项目中需要使用maven的打包工具,生成zip压缩包,使用的插件是assembly-plugin.因为一些特殊的原因,需要使用一些本地的jar进行依赖,加载外部jar后编码过程中没有任何问题,但是打包 ...

  8. 005.MySQL双主-Master01可用配置

    [root@Master01 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_de ...

  9. canvas-圆弧形可拖动进度条

    一.效果如下: See the Pen XRmNRK by pangyongsheng (@pangyongsheng) on CodePen. 链接dome 二. 本文是实现可拖动滑块实现的基本思路 ...

  10. StringBuffer StringBuilder append

    StringBuilder is not thread safe. So, it performs better in situations where thread safety is not re ...