示例

首先定义一个接口,使用该接口来演示Lambda是如何使用的。

  1. package cn.unuuc.java.service;
  2. public interface OrderService {
  3. void get();
  4. }

匿名内部类实现接口

  1. @Test
  2. public void test02(){
  3. OrderService orderService = new OrderService() {
  4. @Override
  5. public void get() {
  6. System.out.println("get.......");
  7. }
  8. };
  9. orderService.get();
  10. }

也可以省略掉示例名 名称

  1. public void test03(){
  2. new OrderService() {
  3. @Override
  4. public void get() {
  5. System.out.println("get.......");
  6. }
  7. }.get();
  8. }

image.png
可以看到 new OrderService() 代码为灰色的,并且鼠标移动在上会提示一些操作,点击将这段代码自动转换为Lambda表达式。

  1. @Test
  2. public void test03(){
  3. ((OrderService) () -> System.out.println("get.......")).get();
  4. }

可能idea转换的一开始看不太懂。看一下下面的代码

  1. // 首先将 () -> System.out.println("get.......") 看作是一个整体,
  2. // 将其转换为 OrderService 类型,(OrderService)() -> System.out.println("get.......")
  3. // 在将 OrderService 类型,(OrderService)() -> System.out.println("get.......") 看作一个整体,讲个括号
  4. // 最后在调用该对象中的方法:((OrderService) () -> System.out.println("get.......")).get();
  5. @Test
  6. public void test03(){
  7. OrderService orderService = () -> System.out.println("get.......");
  8. orderService.get();
  9. }

Lambda表达式规范

一句话:
使用Lambda表达式依赖于函数式接口(只有一个抽象方法的接口为函数式接口,无论是否存在注解@FunctionalInterface)。

什么是函数式接口见前一篇文章

Lambda表达式语法

  1. () 参数列表(参数名不能重复)
  2. -> 分割
  3. {} 方法体

实例:

  1. (a,b)->{
  2. }

无参方法调用

定义函数接口

  1. package cn.unuuc.java.fun;
  2. @FunctionalInterface
  3. public interface FunctionInterface01 {
  4. void hello();
  5. }

测试方法

  1. @Test
  2. public void test04(){
  3. // 匿名内部类
  4. new FunctionInterface01() {
  5. @Override
  6. public void hello() {
  7. System.out.println("hello......");
  8. }
  9. }.hello();
  10. // Lambda表达式
  11. FunctionInterface01 functionInterface01 = ()->{
  12. System.out.println("Lambda hello......");
  13. };
  14. functionInterface01.hello();
  15. }

image.png

有参方法 返回值 调用

定义函数接口

  1. package cn.unuuc.java.fun;
  2. @FunctionalInterface
  3. public interface FunctionInterface02 {
  4. String get(int i,int j);
  5. }

测试方法

  1. @Test
  2. public void test05(){
  3. // 匿名内部类
  4. String s = new FunctionInterface02() {
  5. @Override
  6. public String get(int i, int j) {
  7. return i + "--" + j;
  8. }
  9. }.get(10, 20);
  10. System.out.println(s);
  11. // Lambda 表达式
  12. FunctionInterface02 functionInterface02 = (i,j)->{
  13. return i + "--" + j;
  14. };
  15. String s1 = functionInterface02.get(10, 20);
  16. System.out.println(s1);
  17. }

Lambda表达式简化

  1. @Test
  2. public void test07(){
  3. // 精简的代码
  4. ((FunctionInterface02)(i,j)-> {
  5. return i + "--" + j;
  6. }).get(10,20);
  7. }
  1. (
  2. (FunctionInterface02)(i,j)-> {return i + "--" + j;}
  3. )
  4. .get(10,20);

而如果对于方法体里只有一条语句,那么 { } 是可以省略掉的

  1. @Test
  2. public void test07(){
  3. ((FunctionInterface01)()-> System.out.println("hello")).hello();
  4. }

如果存在返回值的方法而方法体里只有一条语句,则return关键字也可以省略

  1. @Test
  2. public void test07(){
  3. // 精简的代码
  4. ((FunctionInterface02)(i,j)-> i+"--"+j).get(10,20);
  5. }