lambda表达式推导和使用
Posted 116970u
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lambda表达式推导和使用相关的知识,希望对你有一定的参考价值。
lambda
- λ希腊字母表中排序第十一位的字母,英语名称为 Lambda,
- 避免匿名内部类定义过多
- 其实质属于函数式编程的概念
(params) -> expression (params) -> statement (params) -> statements
new Thread(()->System.out.println("多线程学习。。。")).start();
1、lambda表达式逐级推导
1 package com.sxt.thread; 2 3 /** 4 * Lambda表达式 简化线程(用一次)的使用 5 */ 6 public class LambdaThread 7 8 //静态内部类 9 static class Test implements Runnable 10 public void run() 11 for (int i = 0; i < 20; i++) 12 System.out.println("一边听歌"); 13 14 15 16 17 public static void main(String[] args) 18 19 //new Thread(new Test()).start(); 20 21 //局部内部类 22 class Test2 implements Runnable 23 public void run() 24 for (int i = 0; i < 20; i++) 25 System.out.println("一边听歌"); 26 27 28 29 30 new Thread(new Test2()).start(); 31 32 //匿名内部类 必须借助接口或是父类 33 new Thread(new Runnable() 34 @Override 35 public void run() 36 for (int i = 0; i < 20; i++) 37 System.out.println("一边听歌"); 38 39 40 ).start(); 41 42 //jdk8简化 43 new Thread(() -> 44 for (int i = 0; i < 20; i++) 45 System.out.println("一边听歌"); 46 47 ).start(); 48 49
2、再次推导,加深印象
1 package com.sxt.thread; 2 3 /** 4 * @ClassName LambdaTest01 5 * @Description TODO 6 * @Date 2019/7/23 22:46 7 * @Version 1.0 8 */ 9 public class LambdaTest01 10 11 //静态内部类 12 static class Like2 implements ILike 13 14 @Override 15 public void lambda() 16 System.out.println("I Like Lambda2"); 17 18 19 20 public static void main(String[] args) 21 22 ILike like = new Like(); 23 like.lambda(); 24 25 like = new Like2(); 26 like.lambda(); 27 28 //方法内部类 29 class Like3 implements ILike 30 31 @Override 32 public void lambda() 33 System.out.println("I Like Lambda3"); 34 35 36 like = new Like3(); 37 like.lambda(); 38 39 //匿名内部类 40 like = new ILike() 41 @Override 42 public void lambda() 43 System.out.println("I Like Lambda4"); 44 45 ; 46 like.lambda(); 47 //lambda推导必须存在类型 48 like = () -> 49 System.out.println("I Like Lambda5"); 50 ; 51 like.lambda(); 52 53 54 55 interface ILike 56 void lambda(); 57 58 59 //外部类 60 class Like implements ILike 61 62 @Override 63 public void lambda() 64 System.out.println("I Like Lambda"); 65 66
3、lambda参数传递(无返回值)以及简化
1 package com.sxt.thread; 2 3 /** 4 * lambda推导+参数 5 */ 6 public class LambdaTest02 7 8 9 public static void main(String[] args) 10 ILove love = (int a) -> 11 System.out.println("I Like Lambda-->" + a); 12 ; 13 love.lambda(100); 14 15 //简化 16 love = (a) -> 17 System.out.println("I Like Lambda-->" + a); 18 ; 19 love.lambda(60); 20 21 //只有一个参数,()可以省略 22 love = a -> 23 System.out.println("I Like Lambda-->" + a); 24 ; 25 love.lambda(80); 26 27 //如果代码只是一行,还可以省略 28 love = a -> System.out.println("I Like Lambda-->" + a); 29 love.lambda(90); 30 31 32 33 interface ILove 34 void lambda(int a); 35 36 37 //外部类 38 class Love implements ILove 39 40 @Override 41 public void lambda(int a) 42 System.out.println("I Like Lambda-->" + a); 43 44
4、多个参数+返回值
1 package com.sxt.thread; 2 3 /** 4 * lambda推导+参数+返回值 5 */ 6 public class LambdaTest03 7 8 9 public static void main(String[] args) 10 IInterest interest = (int a, int c)-> 11 System.out.println("I Like Lambda-->" + (a + c)); 12 return a + c; 13 ; 14 15 interest.lambda(100,200); 16 17 interest = (a,c)-> 18 System.out.println("I Like Lambda-->" + (a + c)); 19 return a + c; 20 ; 21 interest.lambda(200,200); 22 23 //()不可以省略 24 /*interest = a,c-> 25 System.out.println("I Like Lambda-->" + (a + c)); 26 return a + c; 27 ;*/ 28 29 interest = (a,c)-> 30 return a + c; 31 ; 32 33 interest = (a,c)->a+c; 34 35 interest = (a,c)->100; 36 37 38 System.out.println(interest.lambda(10,20)); 39 40 41 42 43 44 45 interface IInterest 46 int lambda(int a, int b); 47 48 49 //外部类 50 class Interest implements IInterest 51 52 @Override 53 public int lambda(int a, int c) 54 System.out.println("I Like Lambda-->" + (a + c)); 55 return a + c; 56 57
5、多线程示例
1 package com.sxt.thread; 2 3 /** 4 * lambda推导+参数+返回值 5 */ 6 public class LambdaTest04 7 8 public static void main(String[] args) 9 10 new Thread(()-> 11 for (int i = 0; i <100 ; i++) 12 System.out.println("123"); 13 14 ).start(); 15 16 new Thread(()->System.out.println("456")).start(); 17 18 19
以上是关于lambda表达式推导和使用的主要内容,如果未能解决你的问题,请参考以下文章
Python 基础入门笔记———— 公共操作推导式函数lambda表达式高阶函数
Python 基础入门笔记———— 公共操作推导式函数lambda表达式高阶函数
Python 基础入门笔记———— 公共操作推导式函数lambda表达式高阶函数