博主
258
258
258
258
专辑

第十节 Lambda表达式简介

亮子 2021-09-14 21:13:30 6634 0 0 0

图片alt

现代希腊语字母表

λ(希腊字母表中排序第十一位的字母)

Java 8的一个大亮点是引入Lambda表达式,使用它设计的代码会更加简洁。通过Lambda表达式,可以替代我们以前经常写的匿名内部类来实现接口。

Lambda表达式本质是一个匿名函数。

Lambda 表达式,也可称为闭包,它是推动 Java 8 发布的最重要新特性。

Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中)。

使用 Lambda 表达式可以使代码变的更加简洁紧凑。

(parameters) -> expression
// OR
(parameters) ->{ statements; }

1)、Lambda 表达式实例

// 1. 不需要参数,返回值为 5  
() -> 5  
  
// 2. 接收一个参数(数字类型),返回其2倍的值  
x -> 2 * x  
  
// 3. 接受2个参数(数字),并返回他们的差值  
(x, y) -> x – y  
  
// 4. 接收2个int型整数,返回他们的和  
(int x, int y) -> x + y  
  
// 5. 接受一个 string 对象,并在控制台打印,不返回任何值(看起来像是返回void)  
(String s) -> System.out.print(s)
public class LambdaApplication {

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

    public static void main(String[] args) {
        // 类型声明
        MathAdd add1 = (int a,int b) -> a+b;
        // 不用类型声明
        MathAdd add2 = (a, b) -> a+b;
        // 大括号中的返回语句
        MathAdd add3 = (int a, int b) -> { return a + b; };

        int add11 = add1.add(5, 6);
        System.out.println("add1=" + add11);

        int add22 = add2.add(5, 6);
        System.out.println("add1=" + add22);

        int add33 = add3.add(5, 6);
        System.out.println("add1=" + add33);
    }
}

2)、变量作用域

lambda 表达式只能引用标记了 final 的外层局部变量,这就是说不能在 lambda 内部修改定义在域外的局部变量,否则会编译错误。

public class LambdaTest1 {

    final static String salutation = "Hello! ";

    public static void main(String[] args) {
        MessageService service = message ->
                System.out.println(salutation + message);
        service.sayMessage("Runoob");
    }

    interface MessageService {
        void sayMessage(String message);
    }
}

3)、lambda的进化史

(1)基本代码

//--1 定义接口
interface IIntegerMethod {
    Integer add(Integer a, Integer b);
}

//--2 实现接口
class IntegerMethodImpl implements  IIntegerMethod {
    @Override
    public Integer add(Integer a, Integer b) {
        return a+b;
    }
}

public class LambdaDemo1 {

    //--3 调用实现类的方法
    public static void main(String[] args) {
        IIntegerMethod method = new IntegerMethodImpl();
        Integer add = method.add(1, 2);
        System.out.println(add);
    }
}

(2)静态内部类

把接口的实现定义再类的内部。

//--1 定义接口
interface IIntegerMethod {
    Integer add(Integer a, Integer b);
}


public class LambdaDemo1 {

    //--2 实现接口
    static class IntegerMethodImpl implements  IIntegerMethod {
        @Override
        public Integer add(Integer a, Integer b) {
            return a+b;
        }
    }

    //--3 调用实现类的方法
    public static void main(String[] args) {
        IIntegerMethod method = new IntegerMethodImpl();
        Integer add = method.add(1, 2);
        System.out.println(add);
    }
}

(3)局部内部类

把接口实现放到了函数的内部。

//--1 定义接口
interface IIntegerMethod {
    Integer add(Integer a, Integer b);
}

public class LambdaDemo1 {

    public static void main(String[] args) {

        //--2 实现接口
        class IntegerMethodImpl implements  IIntegerMethod {
            @Override
            public Integer add(Integer a, Integer b) {
                return a+b;
            }
        }

        //--3 调用实现类的方法
        IIntegerMethod method = new IntegerMethodImpl();
        Integer add = method.add(1, 2);
        System.out.println(add);
    }
}

(4)匿名内部类

//--1 定义接口
interface IIntegerMethod {
    Integer add(Integer a, Integer b);
}

public class LambdaDemo1 {

    public static void main(String[] args) {

        //--2 匿名内部类
        IIntegerMethod method = new IIntegerMethod() {
            @Override
            public Integer add(Integer a, Integer b) {
                return a+b;
            }
        };

        //--3 调用实现类的方法
        Integer add = method.add(1, 2);
        System.out.println(add);
    }
}

(5)lambda实现

//--1 定义接口
interface IIntegerMethod {
    Integer add(Integer a, Integer b);
}

public class LambdaDemo1 {

    public static void main(String[] args) {

        //--2 lambda
        IIntegerMethod method = (a, b) -> {
            return a+b;
        };

        //--3 调用实现类的方法
        Integer add = method.add(1, 2);
        System.out.println(add);
    }
}

(6)简化lambda

直接去掉了括号。

//--1 定义接口
interface IIntegerMethod {
    Integer add(Integer a, Integer b);
}

public class LambdaDemo1 {

    public static void main(String[] args) {

        //--2 lambda
        IIntegerMethod method = (a, b) -> a+b;

        //--3 调用实现类的方法
        Integer add = method.add(1, 2);
        System.out.println(add);
    }
}