一、Java8 Lambda表达式的基本语法
基本语法:
(parameters) -> expression或(parameters) ->{ statements; }
二、使用示例
/** * Java8 Lambda表达式测试 * * @author Kevin * @date 2017-01-24 */public class LambadaTest { public static void main(String[] args) { LambadaTest test = new LambadaTest(); MathOperation addition = (int a, int b) -> (a + b); MathOperation subtraction = (int a, int b) -> { return a - b; }; System.out.println("7 + 3 = " + test.operate(7, 3, addition)); System.out.println("7 - 3 = " + test.operate(7, 3, subtraction)); GreetingService greetService = (message) -> { System.out.println("Hello " + message); }; greetService.sayMessage("Kevin"); } interface MathOperation { int operation(int a, int b); } interface GreetingService { void sayMessage(String message); } private int operate(int a, int b, MathOperation mathOperation) { return mathOperation.operation(a, b); }}
三、Lambda表达式的应用场景
1.主要用于定义内联执行的功能的接口,即只有一个单一的方法接口。2.消除匿名类的需求,并给出了一个非常简单但功能强大的函数式编程能力。
四、参考文章
1.