Java中Lambda表达式的使用

前言

Lambda表达式是在Java 8的时候添加的,引入了函数式接口的概念。Lambda表达式免去了使用匿名方法的麻烦,使用Lambda会让代码变得跟简洁紧凑。

Lambda的基础使用

1.Lambda表达式的结构

public class Program {
    public static void main(String[] args) {
        /*
        Lambda分为3部分
        ():参数列表
        ->:Lambda表达式运算符
        {}:方法体 写具体操作
         */
        Lam lam = (int a, int b) -> {return a + b;};
        System.out.println(lam.add(2,4));//6
    }

    interface Lam{
        int add(int a,int b);
    }
}

2.函数式的参数和返回值不同 有不同Lambda表达式的写法

public class LambdaTest01 {
    public static void main(String[] args) {

        /*
        方法体只有一条语句的话,花括号可以省略
        If1 if1 = () -> System.out.println("无参数无返回值");
         */
        If1 if1 = () -> {
            System.out.println("无参数无返回值");
        };
        if1.test();

        /*
        只有一个参数的话,小括号可以省略
        If2 if2 =a-> {
            System.out.println("a = " + a);
        };
         */
        //单个参数无返回值
        If2 if2 = (int a) -> {
            System.out.println("a = " + a);
        };
        if2.test(6);

        /*
        参数列表的参数类型可以省略,省略的话就要全部都省略
        //两个参数无返回值
        If3 if3 = (a,b) -> {
            System.out.println("a = " + a + "||b = " + b);
        };
         */
        //两个参数无返回值
        If3 if3 = (int a,int b) -> {
            System.out.println("a = " + a + "||b = " + b);
        };
        if3.test(2,4);

        /*
        如果方法体中只有一条语句并且是return语句,那样省略大括号的同时return也是省略
        If4 if4 = () -> 1;
         */
        //无参数有返回值
        If4 if4 = () -> {
            return 1;
        };
        System.out.println(if4.test());

        //单个参数有返回值
        If5 if5 = (int a) -> {
            return a;
        };
        System.out.println(if5.test(888));

        //两个参数有返回值
        If6 if6 = (int a,int b) -> {
            return a + b;
        };
        System.out.println(if6.test(4,5));
    }
}

interface If1{
    //无参数无返回值
    void test();
}

interface If2{
    //单个参数无返回值
    void test(int a);
}

interface If3{
    //两个参数无返回值
    void test(int a,int b);
}

interface If4{
    //无参数有返回值
    int test();
}

interface If5{
    //单个参数有返回值
    int test(int a);
}

interface If6{
    //两个参数有返回值
    int test(int a,int b);
}

3.Lambda封装成方法引用

public class LambdaTest02 {
    public static void main(String[] args) {
        /*
        If5 if5 = a -> a*2;
         */
        /*
        方法引用:如果多个lambda表达式实现函数是一样的话,我们可以封装成通用方法
        方法引用的语法:
        对象 :: 方法    例(lambdaTest02 :: testA)
        静态方法可以直接 类名 :: 方法 例(LambdaTest02::testB)
         */

        LambdaTest02 lambdaTest02 = new LambdaTest02();
        If8 if8 = lambdaTest02 :: testA;
        System.out.println(if8.test(8));

        If8 if88 = LambdaTest02::testB;
        System.out.println(if88.test(8));
    }
    public int testA(int a){
        return a * 2;
    }

    public static int testB(int a){
        return a * 2;
    }

}

interface If8{
    //单个参数有返回值
    int test(int a);
}

4.lambda表达式的构造方法引用

public class LambdaTest03 {
    public static void main(String[] args) {
//      DogService1 dogService1 = () -> new Dog();
//      System.out.println(dogService1.getDog());

        /*
        如果lambda表达式的实现刚好可以通过调用一个类的构造方法来实现
        那么就可以使用构造方法引用,语法是 类名::new
         */
        DogService1 dogService1 = Dog::new;
        System.out.println(dogService1.getDog());

        DogService2 dogService2 = Dog::new;
        System.out.println(dogService2.getDog("小黄",3));
    }
}

interface DogService1{
    Dog getDog();
}

interface DogService2{
    Dog getDog(String name,int age);
}
public class Dog {
    private String name;

    private int age;

    public Dog(){
        System.out.println("无参构造方法");
    }

    public Dog(String name, int age) {
        System.out.println("有参构造方法");
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

5.@Functionallnterface注解

函数式接口:有且仅有一个抽象方法的接口。使用Lambda表达式创建一个函数式接口的对象,一定要确保接口中有且仅有一个抽象方法,Lambda才能顺利的运行。
@Functionallnterface该注解用于一个接口的定义上,一旦使用该注解来定义接口,编译器将会强制检查该接口是否有且仅有一个抽象方法(equal和hashcode方法不算),否则将会报错。这个注解并不是必须的,只要符合函数式接口的定义,那么这个接口就是函数式接口。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值