2022.11.11 Lambda表达式

2. Lambda表达式

2.1 概述

Lambda是JDK8中一个语法糖。他可以对某些匿名内部类的写法进行简化。它是函数式编程思想的一个重要体现。让我们不用关注是什么对象。而是更关注我们对数据进行了什么操作。

2.2 核心原则

可推导可省略

2. 3 基本格式

 (参数列表)->{代码}

例一

我们在创建线程并启动时可以使用匿名内部类的写法:

 1  new Thread(new Runnable() {
 2      @Override
 3      public void run() {
 4          System.out.println("你知道吗 我比你想象的 更想在你身边");
 5      }
 6  }).start();
 7 可以使用Lambda的格式对其进行修改。修改后如下:
 8 
 9  new Thread(()->{
10      System.out.println("你知道吗 我比你想象的 更想在你身边");
11  }).start();
12  

例二:

现有方法定义如下,其中IntBinaryOperator是一个接口。先使用匿名内部类的写法调用该方法。

 1      public static int calculateNum(IntBinaryOperator operator){
 2          int a = 10;
 3          int b = 20;
 4          return operator.applyAsInt(a, b);
 5      }
 6  7      public static void main(String[] args) {
 8          int i = calculateNum(new IntBinaryOperator() {
 9              @Override
10              public int applyAsInt(int left, int right) {
11                  return left + right;
12              }
13          });
14          System.out.println(i);
15      }
16 Lambda写法:
17 
18      public static void main(String[] args) {
19          int i = calculateNum((int left, int right)->{
20              return left + right;
21          });
22          System.out.println(i);
23      }

例三:

现有方法定义如下,其中IntPredicate是一个接口。先使用匿名内部类的写法调用该方法。

 1      public static void printNum(IntPredicate predicate){
 2          int[] arr = {1,2,3,4,5,6,7,8,9,10};
 3          for (int i : arr) {
 4              if(predicate.test(i)){
 5                  System.out.println(i);
 6              }
 7          }
 8      }
 9      public static void main(String[] args) {
10          printNum(new IntPredicate() {
11              @Override
12              public boolean test(int value) {
13                  return value%2==0;
14              }
15          });
16      }

Lambda写法:

 1      public static void main(String[] args) {
 2          printNum((int value)-> {
 3              return value%2==0;
 4          });
 5      }
 6      public static void printNum(IntPredicate predicate){
 7          int[] arr = {1,2,3,4,5,6,7,8,9,10};
 8          for (int i : arr) {
 9              if(predicate.test(i)){
10                  System.out.println(i);
11              }
12          }
13      }

例四:

现有方法定义如下,其中Function是一个接口。先使用匿名内部类的写法调用该方法。

 1      public static <R> R typeConver(Function<String,R> function){
 2          String str = "1235";
 3          R result = function.apply(str);
 4          return result;
 5      }
 6      public static void main(String[] args) {
 7          Integer result = typeConver(new Function<String, Integer>() {
 8              @Override
 9              public Integer apply(String s) {
10                  return Integer.valueOf(s);
11              }
12          });
13          System.out.println(result);
14      }

Lambda写法:

1          Integer result = typeConver((String s)->{
2              return Integer.valueOf(s);
3          });
4          System.out.println(result);

例五:

现有方法定义如下,其中IntConsumer是一个接口。先使用匿名内部类的写法调用该方法。

 1      public static void foreachArr(IntConsumer consumer){
 2          int[] arr = {1,2,3,4,5,6,7,8,9,10};
 3          for (int i : arr) {
 4              consumer.accept(i);
 5          }
 6      }
 7      public static void main(String[] args) {
 8          foreachArr(new IntConsumer() {
 9              @Override
10              public void accept(int value) {
11                  System.out.println(value);
12              }
13          });
14      }

Lambda写法:

1      public static void main(String[] args) {
2          foreachArr((int value)->{
3              System.out.println(value);
4          });
5      }

2.4 省略规则

  • 参数类型可以省略

  • 方法体只有一句代码时大括号return和唯一一句代码的分号可以省略

  • 方法只有一个参数时小括号可以省略 

posted @ 2022-11-11 13:46  暴躁C语言  阅读(15)  评论(0编辑  收藏  举报