这里来说解一下Java8 新特性中的函数式接口, 以及和Lambda 表达式的关系。看到过不少很多介绍Java8特性的文章,都会介绍到函数式接口和lambda表达式,可是都是分别介绍,没有将二者的关系说明清楚,在这里,把本身的理解整理以下:java
1、函数式接口:app
函数式接口其实本质上仍是一个接口,可是它是一种特殊的接口:SAM类型的接口(Single Abstract Method)。定义了这种类型的接口,使得以其为参数的方法,能够在调用时,使用一个lambda表达式做为参数。从另外一个方面说,一旦咱们调用某方法,能够传入lambda表达式做为参数,则这个方法的参数类型,一定是一个函数式的接口,这个类型一定会使用@FunctionalInterface进行修饰。函数
从SAM原则上讲,这个接口中,只能有一个函数须要被实现,可是也能够有以下例外:spa
1. 默认方法与静态方法并不影响函数式接口的契约,能够任意使用,即设计
函数式接口中能够有静态方法,一个或者多个静态方法不会影响SAM接口成为函数式接口,而且静态方法能够提供方法实现code
能够由 default 修饰的默认方法方法,这个关键字是Java8中新增的,为的目的就是使得某一些接口,原则上只有一个方法被实现,可是因为历史缘由,不得不加入一些方法来兼容整个JDK中的API,因此就须要使用default关键字来定义这样的方法blog
2. 能够有 Object 中覆盖的方法,也就是 equals,toString,hashcode等方法。接口
JDK中之前全部的函数式接口都已经使用 @FunctionalInterface 定义,能够经过查看JDK源码来确认,如下附JDK 8以前已有的函数式接口:get
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
如:
@FunctionalInterface public interface Runnable { /** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see java.lang.Thread#run() */ public abstract void run(); }
如下为一个自定义函数式接口的示例:
定义:
@FunctionalInterface interface Converter<F, T> { T convert(F from); }
使用:
Converter<String, Integer> converter = (from) -> Integer.valueOf(from);
Integer converted = converter.convert("123");
注:方法和构造函数引用在Java8中能够经过 :: 操做符调用
自行设计的方法中, 若是能够接收 lambda 表达式, 则可使用 Function 做为参数, 以下为一些已经实现的函数式接口:
// Function<T, R> -T做为输入,返回的R做为输出 Function<String,String> function = (x) -> {System.out.print(x+": ");return "Function";}; System.out.println(function.apply("hello world")); //Predicate<T> -T做为输入,返回的boolean值做为输出 Predicate<String> pre = (x) ->{System.out.print(x);return false;}; System.out.println(": "+pre.test("hello World")); //Consumer<T> - T做为输入,执行某种动做但没有返回值 Consumer<String> con = (x) -> {System.out.println(x);}; con.accept("hello world"); //Supplier<T> - 没有任何输入,返回T Supplier<String> supp = () -> {return "Supplier";}; System.out.println(supp.get()); //BinaryOperator<T> -两个T做为输入,返回一个T做为输出,对于“reduce”操做颇有用 BinaryOperator<String> bina = (x,y) ->{System.out.print(x+" "+y);return "BinaryOperator";}; System.out.println(" "+bina.apply("hello ","world"));
2、Lambda表达式(这里只是简单提一下)
书写方法: e -> System.out.println( e )
1. 三部分构成
参数列表
符号 ->
函数体 : 有多个语句,能够用{} 包括, 若是须要返回值且只有一个语句,能够省略 return
2. 访问控制:
能够访问类的成员变量和局部变量(非final会自动隐含转为final)
以上主要是对于函数式接口的一些简单介绍,若有疏漏,欢迎指正