Lambda表达式的简单操作
Lambda表达式
为什莫要使用Lambda表达式
- 避免匿名内部类定义过多
- 代码简洁
函数式接口
定义:
- 任何接口只包含唯一的抽象方法,就是函数式接口
public interface method(){
public abstractvoid method1();
}
- 函数式接口可以使用lambda表达式
上Code
不带参数的
package test;
/**
* @author blackcats
* @date 2020/8/24 8:34
* 代码太难了
*/
public class Lambda1 {
//静态内部类
static class test2 implements test{
@Override
public void lambda() {
System.out.println("我是静态内部类lambda2");
}
}
public static void main(String[] args) {
test test = new test1();
test.lambda();
test = new test2();
test.lambda();
//局部内部类
class test3 implements test{
@Override
public void lambda() {
System.out.println("我是局部内部类lambda3");
}
}
test= new test3();
test.lambda();
//匿名内部类
test=new test() {
@Override
public void lambda() {
System.out.println("我是匿名内部类lambda4");
}
};
test.lambda();
//lambda表达式
test=() ->{
System.out.println("我是lambda5表达式");
};
test.lambda();
}
}
//定义函数式接口
interface test{
void lambda();
}
//实现类
class test1 implements test{
@Override
public void lambda() {
System.out.println("我是lambda1");
}
}
带参数的
package test;
/**
* @author blackcats
* @date 2020/8/24 9:16
* 代码太难了
*/
public class Lambda2 {
//静态内部类
static class IHave implements Have{
@Override
public void have(String car) {
System.out.println("我有一辆车——————> "+car);
}
}
public static void main(String[] args) {
//局部内部类
class IHave1 implements Have{
@Override
public void have(String car) {
System.out.println("我有一辆车——————> "+car);
}
}
Have have = new IHave();
have.have("野马");
have = new IHave1();
have.have("野驴");
Have test1=new IHave(){
@Override
public void have(String car) {
System.out.println("我有一辆车——————> "+car);
}
};
test1.have("car");
//带参数类型
Have have1=(String car)->{
System.out.println("我有一辆车——————带参数类型> "+car);
};
have1.have("宝马");
//去掉参数类型
Have have2=(car)->{
System.out.println("我有一辆车——————去掉参数类型> "+car);
};
have2.have("宝马");
//去掉括号
Have have3=car->{
System.out.println("我有一辆车——————去掉括号> "+car);
};
have3.have("宝马");
//去掉花括号
Have have4=car-> System.out.println("我有一辆车——————去掉花括号> "+car);
;
have4.have("宝马");
abc a=(q,b,d)->{
System.out.println("我是多个参数"+q+"-"+b+"-"+d);
};
a.sout(1,2,3);
//总结
/*
lambda只有一行代码时可以省略花括号,多行必须代码块包裹
必须是函数式接口
多个参数也可以去掉参数类型(全部去掉),必须括号包裹
*/
}
}
interface Have{
void have(String car);
}
interface abc{
void sout(int a,int b,int c);
}
总结:
- lambda只有一行代码时可以省略花括号,多行必须代码块包裹
- 必须是函数式接口
- 多个参数也可以去掉参数类型(全部去掉),必须括号包裹
Lambda之map,List简单操作
package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* @author blackcats
* @date 2020/8/24 9:42
* 代码太难了
*/
public class Lambda3 {
public static void main(String[] args) {
//map操作
HashMap<String, String> map = new HashMap<>();
HashMap<String, String> map1 = new HashMap<>();
map.put("1","炒饭");
map.put("2","野驴");
map.put("3","宝马");
map.put("4","西红柿");
//如果key值存在就使用旧值,如果不存在 就是用新值
map.computeIfAbsent(
"4",m->{return "蛋炒饭";}
);
System.out.println(map);
map.forEach(
(key,value)->{
map1.put(key,value);
System.out.println("key--->"+key+"===value--->"+value);
}
);
System.out.println(map1);
//List操作
List<Car> list = new ArrayList();
Car car = new Car("黄色", "福特", 200);
Car car1 = new Car("红色", "大众", 300);
Car car2 = new Car("黑色", "byd", 400);
list.add(car);
list.add(car1);
list.add(car2);
list.forEach(
cars->{
System.out.println(cars.getName());
if(cars.getPrice()>300){
System.out.println("我是最贵的--->"+cars.getName());
}
}
);
}
}