lambda

  • λ希腊字母表中排序第十一位的字母,英语名称为 Lambda,
  • 避免匿名内部类定义过多
  • 其实质属于函数式编程的概念
(params) -> expression
(params) -> statement
(params) -> { statements }
new Thread(()->System.out.println("多线程学习。。。")).start();

1、lambda表达式逐级推导

 package com.sxt.thread;

 /**
* Lambda表达式 简化线程(用一次)的使用
*/
public class LambdaThread { //静态内部类
static class Test implements Runnable {
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("一边听歌");
}
}
} public static void main(String[] args) { //new Thread(new Test()).start(); //局部内部类
class Test2 implements Runnable {
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("一边听歌");
}
}
} new Thread(new Test2()).start(); //匿名内部类 必须借助接口或是父类
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("一边听歌");
}
}
}).start(); //jdk8简化
new Thread(() -> {
for (int i = 0; i < 20; i++) {
System.out.println("一边听歌");
}
}).start();
}
}

2、再次推导,加深印象

 package com.sxt.thread;

 /**
* @ClassName LambdaTest01
* @Description TODO
* @Date 2019/7/23 22:46
* @Version 1.0
*/
public class LambdaTest01 { //静态内部类
static class Like2 implements ILike{ @Override
public void lambda() {
System.out.println("I Like Lambda2");
}
} public static void main(String[] args) { ILike like = new Like();
like.lambda(); like = new Like2();
like.lambda(); //方法内部类
class Like3 implements ILike{ @Override
public void lambda() {
System.out.println("I Like Lambda3");
}
}
like = new Like3();
like.lambda(); //匿名内部类
like = new ILike() {
@Override
public void lambda() {
System.out.println("I Like Lambda4");
}
};
like.lambda();
//lambda推导必须存在类型
like = () -> {
System.out.println("I Like Lambda5");
};
like.lambda();
}
} interface ILike{
void lambda();
} //外部类
class Like implements ILike{ @Override
public void lambda() {
System.out.println("I Like Lambda");
}
}

3、lambda参数传递(无返回值)以及简化

 package com.sxt.thread;

 /**
* lambda推导+参数
*/
public class LambdaTest02 { public static void main(String[] args) {
ILove love = (int a) -> {
System.out.println("I Like Lambda-->" + a);
};
love.lambda(100); //简化
love = (a) -> {
System.out.println("I Like Lambda-->" + a);
};
love.lambda(60); //只有一个参数,()可以省略
love = a -> {
System.out.println("I Like Lambda-->" + a);
};
love.lambda(80); //如果代码只是一行,还可以省略
love = a -> System.out.println("I Like Lambda-->" + a);
love.lambda(90);
}
} interface ILove {
void lambda(int a);
} //外部类
class Love implements ILove { @Override
public void lambda(int a) {
System.out.println("I Like Lambda-->" + a);
}
}

4、多个参数+返回值

 package com.sxt.thread;

 /**
* lambda推导+参数+返回值
*/
public class LambdaTest03 { public static void main(String[] args) {
IInterest interest = (int a, int c)-> {
System.out.println("I Like Lambda-->" + (a + c));
return a + c;
}; interest.lambda(100,200); interest = (a,c)-> {
System.out.println("I Like Lambda-->" + (a + c));
return a + c;
};
interest.lambda(200,200); //()不可以省略
/*interest = a,c-> {
System.out.println("I Like Lambda-->" + (a + c));
return a + c;
};*/ interest = (a,c)-> {
return a + c;
}; interest = (a,c)->a+c; interest = (a,c)->100; System.out.println(interest.lambda(10,20)); } } interface IInterest {
int lambda(int a, int b);
} //外部类
class Interest implements IInterest { @Override
public int lambda(int a, int c) {
System.out.println("I Like Lambda-->" + (a + c));
return a + c;
}
}

5、多线程示例

 package com.sxt.thread;

 /**
* lambda推导+参数+返回值
*/
public class LambdaTest04 { public static void main(String[] args) { new Thread(()->{
for (int i = 0; i <100 ; i++) {
System.out.println("123");
}
}).start(); new Thread(()->System.out.println("456")).start();
} }

lambda表达式推导和使用-LMLPHP

05-27 23:50