文章目录
  1. 1. 背景
  2. 2. @FunctionalInterface
  3. 3. java.util.function
  4. 4. Lambda 表达式
  5. 5. 方法引用(Method Reference)
  6. 6. 扩展阅读
  7. 7. 参考文档

背景

在很多其他语言中,函数是一等公民。例如 JavaScript 中,函数(Function)和字符串(String)、数字(Number)、对象(Object)等一样是一种数据类型。可以这样定义函数:

1
2
3
var myFunction = function () {
doSomething();
};

也可以将函数作为参数:

1
2
3
setTimeout(function() { 
doSomething();
}, 1000);

在 Java 中,函数不是一等公民。如果想要像其他语言一样定义一个函数,只能通过定义一个接口来实现,例如 Runnable

在 Java 8 之前,可以通过匿名类的方式来创建 Runnable

1
2
3
4
5
6
Thread thread = new Thread(new Runnable() {
public void run() {
doSomethong();
}
});
thread.start();

Java 8 中可以通过 lambda 表达式来创建:

1
2
Thread thread = new Thread(() -> doSomethong());
thread.start();

也就是:

1
2
3
4
5
Runnable runnable = new Runnable() {
public void run() {
doSomethong();
}
};

简化成了:

1
Runnable runnable = () -> doSomethong();

是不是看起来像 JavaScript 的函数定义:

1
2
3
var myFunction = function () {
doSomething();
};

@FunctionalInterface

An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification. Conceptually, a functional interface has exactly one abstract method.

@FunctionalInterface 注解用于表明一个接口是函数式接口(functional interface)。函数式接口必须有且只有一个抽象方法。

例如 java.lang.Runnable 就是一个函数式接口,有且仅有一个抽象方法 runRunnable 源码中就有加上注解 @FunctionalInterface

1
2
3
4
@FunctionalInterface
public interface Runnable {
public abstract void run();
}

Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.

函数式接口实例可以通过 lambda 表达式、方法引用(method reference)、构造方法引用(constructor reference)的方式来创建。

However, the compiler will treat any interface meeting the definition of a functional interface as a functional interface regardless of whether or not a FunctionalInterface annotation is present on the interface declaration.

编译器会把满足函数式接口定义(有且只有一个抽象方法)的任何接口视为函数式接口 ,无论有没有 @FunctionalInterface 注解。

以上两条总结一下:当一个接口符合函数式接口定义(有且只有一个抽象方法),那么就可以通过 lambda 表达式、方法引用的方式来创建,无论该接口有没有加上 @FunctionalInterface 注解。

下面列出一些 Java 中的函数式接口:

  • java.lang.Runnable
  • java.util.concurrent.Callable
  • java.util.Comparator
  • java.util.function 包下的函数式接口,例如 PredicateConsumerFunctionSupplier

java.util.function

java.util.function 包下,定义了大量的函数式接口,每个接口都有且只有一个抽象方法,这些接口的区别在于其中的抽象方法的参数和返回值不同。

类型 参数个数 参数类型 返回值类型
Function<T,R> 1 T R
IntFunction<R> 1 int R
LongFunction<R> 1 long R
DoubleFunction<R> 1 double R
ToIntFunction<T> 1 T int
ToLongFunction<T> 1 T long
ToDoubleFunction<T> 1 T double
IntToLongFunction 1 int long
IntToDoubleFunction 1 int double
LongToIntFunction 1 long int
LongToDoubleFunction 1 long double
DoubleToIntFunction 1 double int
DoubleToLongFunction 1 double long
BiFunction<T,U,R> 2 T,U R
ToIntBiFunction<T,U> 2 T,U int
ToLongBiFunction<T,U> 2 T,U long
ToDoubleBiFunction<T,U> 2 T,U double
UnaryOperator<T> 1 T T
IntUnaryOperator 1 int int
LongUnaryOperator 1 long long
DoubleUnaryOperator 1 double double
BinaryOperator<T> 2 T,T T
IntBinaryOperator 2 int,int int
LongBinaryOperator 2 long,long long
DoubleBinaryOperator 2 double,double double
Consumer<T> 1 T void
IntConsumer 1 int void
LongConsumer 1 long void
DoubleConsumer 1 double void
BiConsumer<T,U> 2 T,U void
ObjIntConsumer<T> 2 T,int void
ObjLongConsumer<T> 2 T,long void
ObjDoubleConsumer<T> 2 T,double void
Supplier<T> 0 - T
BooleanSupplier 0 - boolean
IntSupplier 0 - int
LongSupplier 0 - long
DoubleSupplier 0 - double
Predicate<T> 1 T boolean
IntPredicate 1 int boolean
LongPredicate 1 long boolean
DoublePredicate 1 double boolean
BiPredicate<T,U> 2 T,U boolean

Lambda 表达式

One issue with anonymous classes is that if the implementation of your anonymous class is very simple, such as an interface that contains only one method, then the syntax of anonymous classes may seem unwieldy and unclear. In these cases, you’re usually trying to pass functionality as an argument to another method, such as what action should be taken when someone clicks a button. Lambda expressions enable you to do this, to treat functionality as method argument, or code as data.

当一个接口中只有一个方法时(即满足函数式接口定义),此时通过匿名类的语法来编写代码显得比较笨重。使用 lambda 表达式可以将功能作为参数,将代码作为数据。

一个 Lambda 表达式分为以下三个部分:

  • Argument List : 参数列表
  • Arrow Token : 箭头符号,即 ->
  • Body : 包含一个表达式或者一整块代码

下面举几个例子:

  1. 定义一个函数式接口对象,用于求两个 int 之和,包含两个 int 类型参数 xy,返回 x + y 的值:

    1
    IntBinaryOperator sum = (x, y) -> x + y;
  2. 定义一个函数式接口对象,无参数,返回42:

    1
    IntSupplier intSupplier = () -> 42;
  3. 定义一个函数式接口对象,用于输出字符串,包含一个 String 类型的参数 s,无返回值:

    1
    2
    3
    Consumer<String> stringConsumer = s -> {
    System.out.println(s);
    };

方法引用(Method Reference)

You use lambda expressions to create anonymous methods. Sometimes, however, a lambda expression does nothing but call an existing method. In those cases, it’s often clearer to refer to the existing method by name. Method references enable you to do this; they are compact, easy-to-read lambda expressions for methods that already have a name.

如果 lambda 表达式只是调用一个已有的方法,那么可以直接使用方法引用。

例如输出 List 中的元素,用 lambda 表达式:

1
2
List<String> list = Arrays.asList("1", "22", "333");
list.forEach(s -> System.out.println(s));

改用方法引用更加简洁:

1
2
List<String> list = Arrays.asList("1", "22", "333");
list.forEach(System.out::println);

也就是:

1
Consumer<String> stringConsumer = s -> System.out.println(s);

简化成了:

1
Consumer<String> stringConsumer = System.out::println; // 将一个已有的方法赋值给一个函数式接口对象

方法引用有以下几种类型:

  1. 类名::静态方法名 : 静态方法引用

    例如定义一个 max 函数式接口对象,用于求两个 int 中的最大值:

    1
    IntBinaryOperator max = Math::max;

    IntBinaryOperator 表示有两个 int 参数且返回值为 int 的函数,Math.max() 静态方法符合要求。

  2. 对象名::非静态方法名 : 对象的方法引用

    例如定义一个 println 函数式接口对象,用于输出字符串:

    1
    Consumer<String> println = System.out::println;

    Consumer<String> 表示有一个 String 类型参数且无返回值的函数,System.out.println() 方法符合要求。

  3. 类名::new : 构造方法引用

    例如定义一个 createHashMap 函数式接口对象,用于创建一个 HashMap

    1
    Supplier<HashMap> createHashMap = HashMap::new;

    Supplier<HashMap> 表示有一个无参数且返回值为 HashMap 的函数,HashMap 的构造函数符合要求。

  4. 类名::非静态方法名 : 文档中解释为:Reference to an instance method of an arbitrary object of a particular type 。如果不理解的话,下面举个例子来说明一下。

    定义一个 concat 函数式接口对象,用于拼接两个字符串:

    1
    BinaryOperator<String> concat = String::concat;

    BinaryOperator<String> 表示有两个 String 类型参数且返回值为 String 的函数。注意 String 类的 concat 不是静态方法,且 String.concat(String str) 只有一个参数,看似不符合要求。实际上它相当于:

    1
    BinaryOperator<String> concat = (s1, s2) -> s1.concat(s2);

    即调用第一个参数 s1concat 方法,传入参数 s2

扩展阅读

Java 8 Stream 总结

参考文档

文章目录
  1. 1. 背景
  2. 2. @FunctionalInterface
  3. 3. java.util.function
  4. 4. Lambda 表达式
  5. 5. 方法引用(Method Reference)
  6. 6. 扩展阅读
  7. 7. 参考文档