Lambda 表达式

1.Lambda表达式有什么作用

首先要了解匿名内部类是什么东西(匿名内部类:就是在同一个类的内部有第二个类) 如:

// 想在不创建 Test11 实现类的情况下进行对接口中方法的实现的话
class DemoApplicationTests {
    
    // 这就是匿名内部类(我感觉是因为 Test11 是个接口这个类之在被调用运行时才会创建)
    Test11 test11 = new Test11() {
        @Override
        public int back1() {
            return 0;
        }
​
        @Override
        public int back2() {
            return 0;
        }
​
        @Override
        public int back3() {
            return 0;
        }
    };
​
}
// 一个接口有三个抽象方法
interface Test11{
    int back1();
    int back2();
    int back3();
}

Lambda表达式是在匿名内部类的这个接口中只有一个抽象方法的时候才可以使用,可以说是匿名内部类的这种特定情况的一种缩写形式;如:

无返回值情况

// 这个类想要调用 Test1 接口的 out 方法
class DemoApplicationTests {
    // Test1 test1 是指要调用的接口类
    // ()->固定写法小括号里写方法参数
    // {}里写对应这个参数列表和返回值的方法的逻辑代码
    Test1 test1 = ()->{        
        System.out.println("无参数无返回值");    
    };
    Test2 test2 = (int a)->{
        System.out.println("一个参数无返回值 参数为"+a);
    };
    Test3 test3 = (int a , int b)->{
        System.out.println("两个参数无返回值 参数为"+a+" "+b);
    };
    //在本类中调用时
    public void go(){
        this.test1.out();
    }
    public static void main(String[] args) {        
        DemoApplicationTests demoApplicationTests = new DemoApplicationTests();
        // 在其他类中调用时
        demoApplicationTests.test1.out();
        demoApplicationTests.test2.out(1);
        demoApplicationTests.test3.out(2,3);
    }
}
​
// 无参数无返回值的接口
interface Test1 {
    void out();
}
// 一个参数无返回值的接口
interface Test2 {
    void out(int a);
}
// 多个参数无返回值接口
interface Test3 {
    void out(int a , int b);
}

有返回值的情况

class DemoApplicationTests {
​
    Test1 test1 = () -> {
        System.out.println("无参数无返回值");
        return 1 + 1;
    };
    Test2 test2 = (int a) -> {
        System.out.println("一个参数无返回值 参数为" + a);
        return a + 1;
    };
    Test3 test3 = (int a, int b) -> {
        System.out.println("两个参数无返回值 参数为" + a + " " + b);
        return a + b;
    };
​
    public void go() {
        this.test1.out();
    }
​
    public static void main(String[] args) {
        DemoApplicationTests demoApplicationTests = new DemoApplicationTests();
        Integer out = demoApplicationTests.test1.out();
        System.out.println("无参数有返回值的返回值为"+out);
        Integer out1 = demoApplicationTests.test2.out(1);
        System.out.println("一个参数参数有返回值的返回值为"+out1);
        Integer out2 = demoApplicationTests.test3.out(2, 3);
        System.out.println("两个参数有返回值的返回值为"+out2);
        demoApplicationTests.go();
        System.out.println("内部引用无参数有返回值的返回值为"+out);
    }
​
}
​
interface Test1 {
    Integer out();
}
​
interface Test2 {
    Integer out(int a);
}
​
interface Test3 {
    Integer out(int a, int b);
}

 

 

简化表达式

1.参数类型可以省略

2.如果只有一个参数,()也可以省略

3.如果方法只有一条语句,{}也可以省略 (如果方法体中唯一语句是return语句的话,在省略{}的时候也要把return省略 如:

    Test1 test1 = ()->System.out.println("无参数无返回值");
    Test2 test2 = (a)->System.out.println("一个参数无返回值 参数为" + a);
    Test3 test3 = (a,b)->System.out.println("两个参数无返回值 参数为" + a + " " + b);
    Test4 test4 = ()-> 13+2;
    Test5 test5 = (a)-> a+2;
    Test6 test6 = (a,b)-> a+b;
​
    public static void main(String[] args) {
        DemoApplicationTests demoApplicationTests = new DemoApplicationTests();
        demoApplicationTests.test1.out();
        demoApplicationTests.test2.out(2);
        demoApplicationTests.test3.out(2,3);
        Integer out = demoApplicationTests.test4.out();
        System.out.println("无参数有返回值 返回值为:"+out);
        Integer out1 = demoApplicationTests.test5.out(2);
        System.out.println("一个参数有返回值 返回值为:" + out1);
        Integer out2 = demoApplicationTests.test6.out(2, 3);
        System.out.println("两个参数有返回值 返回值为:" + out2);
    }

方法的封装

如果多个Lambda表达式实现的方法逻辑一致的话可以将方法进行封装

// 修改前
class DemoApplicationTests {  
    Test6 test1 = (a,b)->{
        System.out.println("有返回值和两个参数 参数a:"+a+" b:"+b);
        return a+b;
    };
​
    Test6 test2 = (a,b)->{
        System.out.println("有返回值和两个参数 参数a:"+a+" b:"+b);
        return a+b;
    };
​
    public static void main(String[] args) {
        DemoApplicationTests demoApplicationTests = new DemoApplicationTests();
        demoApplicationTests.test1.out(1,2);
        demoApplicationTests.test2.out(5,9);
    }
}
interface Test6 {
    Integer out(int a , int b);
}
// 修改后
class DemoApplicationTests {     
    Test6 test1 = this::out;
    Test6 test2 = this::out;
​
    public static void main(String[] args) {
        DemoApplicationTests demoApplicationTests = new DemoApplicationTests();
        demoApplicationTests.test1.out(1,2);
        demoApplicationTests.test2.out(5,9);
    }
​
    public int out(int a,int b){
        System.out.println("有返回值和两个参数 参数a:"+a+" b:"+b);
        return a+b;
    }
}
interface Test6 {
    Integer out(int a , int b);
}

构造器引用

Dog实体类

package com.example.demo;
​
/**
 * @Author XiShuaiPeng
 * @Description
 * @Date 2022/7/25 16:07
 **/
public class Dog {
​
    private String name;
    private Integer age;
​
    public Dog(){
        System.out.println("无参构造器");
    }
​
    public Dog(String name, Integer age) {
        System.out.println("有参构造器");
        this.name = name;
        this.age = age;
    }
​
    public String getName() {
        return name;
    }
​
    public Integer getAge() {
        return age;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public void setAge(Integer age) {
        this.age = age;
    }
​
    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
​

操作方法

// 简化前
class DemoApplicationTests {
    
    // 有参构造器
    Dog1 dog1 = ()->new Dog();
    // 无参构造器
    Dog2 dog2 = (a,b)->new Dog(a,b);
​
    public static void main(String[] args) {
        DemoApplicationTests demoApplicationTests = new DemoApplicationTests();
        Dog dog = demoApplicationTests.dog1.getDog();
        System.out.println(dog);
        Dog dog1 = demoApplicationTests.dog2.getDog("肉肉",20);
        System.out.println(dog1);
    }
}
interface Dog1{
    Dog getDog();
}
interface Dog2{
    Dog getDog(String a ,Integer b);
}
// 简化后
class DemoApplicationTests {
    
    // 有参构造器
    Dog1 dog1 =Dog::new;
    // 无参构造器
    Dog2 dog2 =Dog::new;
​
    public static void main(String[] args) {
        DemoApplicationTests demoApplicationTests = new DemoApplicationTests();
        Dog dog = demoApplicationTests.dog1.getDog();
        System.out.println(dog);
        Dog dog1 = demoApplicationTests.dog2.getDog("肉肉",20);
        System.out.println(dog1);
    }
}
interface Dog1{
    Dog getDog();
}
interface Dog2{
    Dog getDog(String a ,Integer b);
}

@FunctionalInterface这个注解标在接口的函数方法上,可以验证这个接口是不是Lambda表达式专用的接口,比如只能有一个抽象方法,如果有多个会报错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值