Lambda 表达式简介Lambda 是一个简洁匿名函数,我们可以把 Lambda 表达式理解为是一段可以传递的代码(将代码像数据一样进行传递)。可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格,使 Java 的语言表达能力得到了提升。
Lambda 表达式的应用范围Lambda 表达式本质:作为函数式接口(只有一个抽象方法的接口)的实例,Lambda 表达式和方法引用,只能用在函数式接口上。
只能是接口:否则报 Target type of a lambda conversion must be an interface 
只能有一个 abstract 方法:否则报 Multiple non-overriding abstract methods found xxx 
 
Lambda 表达式的语法基础语法:Java8 中引入了一个新的操作符 -> 该操作符称为箭头操作符或 Lambda 操作符。
箭头操作符将 Lambda 表达式拆分成两部分:
左侧:Lambda 表达式的参数列表 
右侧:Lambda 表达式中所需执行的功能, 即 Lambda 体 
 
语法格式 
无参数,无返回值 
一个参数,无返回值 
数据类型可以省略,因为可由编译器推断得出,称为“类型推断” 
若只需一个参数,参数的小括号可以省略 
需要两个或两个以上的参数,多条执行语句,并且可以有返回值 
Lambda 体只有一条语句时,return 与大括号若有,都可省略 
 
示例 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 public  class  Lambda           @Test      public  void  m1 ()                    Runnable runnable = new  Runnable() {             public  void  run ()                   System.out.println("Vincent帅到掉渣..." );             }         };         runnable.run();                  Runnable rL = () -> {             System.out.println("Vincent帅到掉渣..." );         };         rL.run();     }          @Test      public  void  m2 ()                    Consumer<String> consumer = new  Consumer<String>() {             @Override              public  void  accept (String s)                   System.out.println(s);             }         };         consumer.accept("谎言和誓言的区别是什么呢?" );                  Consumer<String> cL = (String s) -> {             System.out.println(s);         };         cL.accept("一个是听得人当真了,一个是说的人当真了......" );     }          @Test      public  void  m3 ()                    Consumer<String> consumer = new  Consumer<String>() {             @Override              public  void  accept (String s)                   System.out.println(s);             }         };         consumer.accept("Vincent帅到吐血..." );                  Consumer<String> cL = (s) -> {             System.out.println(s);         };         cL.accept("Vincent帅到吐血..." );     }          @Test      public  void  m4 ()                    Consumer<String> consumer = new  Consumer<String>() {             @Override              public  void  accept (String s)                   System.out.println(s);             }         };         consumer.accept("Vincent帅到喷汁..." );                  Consumer<String> cL = s -> {             System.out.println(s);         };         cL.accept("Vincent帅到喷汁..." );     }          @Test      public  void  m5 ()                    Comparator<Integer> comparator = new  Comparator<Integer>() {             @Override              public  int  compare (Integer o1, Integer o2)                   System.out.println("o1:"  + o1);                 System.out.println("o2:"  + o2);                 return  o1.compareTo(o2);             }         };         int  compare = comparator.compare(100 , 99 );         System.out.println(compare);                  Comparator<Integer> cL = (o1, o2) -> {             System.out.println("o1:"  + o1);             System.out.println("o2:"  + o2);             return  o1.compareTo(o2);         };         int  compareL = cL.compare(520 , 1314 );         System.out.println(compareL);     }          @Test      public  void  m6 ()                    Comparator<Integer> comparator = new  Comparator<Integer>() {             @Override              public  int  compare (Integer o1, Integer o2)                   return  o1.compareTo(o2);             }         };         int  compare = comparator.compare(100 , 99 );         System.out.println(compare);                  Comparator<Integer> cL = (o1, o2) -> o1.compareTo(o2);         int  compareL = cL.compare(520 , 1314 );         System.out.println(compareL);     } } 
总结 
-> 左边:lambda 形参列表的参数类型可以省略(类型推断);如果 lambda 形参列表只有一个参数,其 () 可以省略-> 右边:lambda 体应该使用 {} 包裹;如果 lambda 体只有一条执行语句(可以是 return 语句),可以省略 {} 和 return 关键字 
案例源码:https://github.com/V-Vincen/jdk_18 
        
  
    
      If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !