前言:
Lambda表达式需依托于函数是接口。(函数式接口定义为只具备 一个抽象方法 的接口)如下:
@FunctionalInterface public interface AppleFilter { boolean accept(Apple apple); }
=》详细解释及jdk自带的函数式接口可参见:https://www.cnblogs.com/shenlanzhizun/p/6023140.html
(1)Runnable noArguments = () -> System.out.println("Hello World");
此表达式重写了Runnable的run(),方法内容为“System.out.println("Hello World");”
(2)BinaryOperator<Long> addExplicit = (Long x, Long y) -> x + y;
此表达式重写了BinaryOperator的唯一抽象方法,内容为“x + y; ”
记住一点很重要,Lambda 表达式都可以扩写为原始的“匿名类”形式。所以当你觉得这个 Lambda 表达式很复杂不容易理解的时候,不妨把它扩写为“匿名类”形式来看。
Thread th = new Thread(new Runnable() { @Override public void run() { System.out.println("Do"); } });
可改写为:
Thread th = new Thread(() -> System.out.println("Do"));