1. Lambda表达式使用前后的对比

举例一:

  1. @Test
  2. public void test1(){
  3. Runnable r1 = new Runnable() {
  4. @Override
  5. public void run() {
  6. System.out.println("我爱北京天安门");
  7. }
  8. };
  9. r1.run();
  10. System.out.println("***********************");
  11. Runnable r2 = () -> System.out.println("我爱北京故宫");
  12. r2.run();
  13. }

举例二:

  1. @Test
  2. public void test2(){
  3. Comparator<Integer> com1 = new Comparator<Integer>() {
  4. @Override
  5. public int compare(Integer o1, Integer o2) {
  6. return Integer.compare(o1,o2);
  7. }
  8. };
  9. int compare1 = com1.compare(12,21);
  10. System.out.println(compare1);
  11. System.out.println("***********************");
  12. //Lambda表达式的写法
  13. Comparator<Integer> com2 = (o1,o2) -> Integer.compare(o1,o2);
  14. int compare2 = com2.compare(32,21);
  15. System.out.println(compare2);
  16. System.out.println("***********************");
  17. //方法引用
  18. Comparator<Integer> com3 = Integer :: compare;
  19. int compare3 = com3.compare(32,21);
  20. System.out.println(compare3);
  21. }

2. Lambda表达式的基本语法

  1. 举例: (o1,o2) -> Integer.compare(o1,o2);
  2. 格式:

-> :lambda操作符 或 箭头操作符 -> 左边:lambda形参列表 (其实就是接口中的抽象方法的形参列表 -> 右边:lambda体 (其实就是重写的抽象方法的方法体

3.如何使用:分为六种情况

image.png

//语法格式一:无参,无返回值
    @Test
    public void test1(){
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("我爱北京天安门");
            }
        };

        r1.run();

        System.out.println("***********************");

        Runnable r2 = () -> {
            System.out.println("我爱北京故宫");
        };

        r2.run();
    }
    //语法格式二:Lambda 需要一个参数,但是没有返回值。
    @Test
    public void test2(){

        Consumer<String> con = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        con.accept("谎言和誓言的区别是什么?");

        System.out.println("*******************");

        Consumer<String> con1 = (String s) -> {
            System.out.println(s);
        };
        con1.accept("一个是听得人当真了,一个是说的人当真了");

    }

    //语法格式三:数据类型可以省略,因为可由编译器推断得出,称为“类型推断”
    @Test
    public void test3(){

        Consumer<String> con1 = (String s) -> {
            System.out.println(s);
        };
        con1.accept("一个是听得人当真了,一个是说的人当真了");

        System.out.println("*******************");

        Consumer<String> con2 = (s) -> {
            System.out.println(s);
        };
        con2.accept("一个是听得人当真了,一个是说的人当真了");

    }

image.png

@Test
    public void test4(){

        ArrayList<String> list = new ArrayList<>();//类型推断

        int[] arr = {1,2,3};//类型推断

    }

    //语法格式四:Lambda 若只需要一个参数时,参数的小括号可以省略
    @Test
    public void test5(){
        Consumer<String> con1 = (s) -> {
            System.out.println(s);
        };
        con1.accept("一个是听得人当真了,一个是说的人当真了");

        System.out.println("*******************");

        Consumer<String> con2 = s -> {
            System.out.println(s);
        };
        con2.accept("一个是听得人当真了,一个是说的人当真了");


    }

    //语法格式五:Lambda 需要两个或以上的参数,多条执行语句,并且可以有返回值
    @Test
    public void test6(){

        Comparator<Integer> com1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                System.out.println(o1);
                System.out.println(o2);
                return o1.compareTo(o2);
            }
        };

        System.out.println(com1.compare(12,21));

        System.out.println("*****************************");
        Comparator<Integer> com2 = (o1,o2) -> {
            System.out.println(o1);
            System.out.println(o2);
            return o1.compareTo(o2);
        };

        System.out.println(com2.compare(12,6));


    }

    //语法格式六:当 Lambda 体只有一条语句时,return 与大括号若有,都可以省略
    @Test
    public void test7(){

        Comparator<Integer> com1 = (o1,o2) -> {
            return o1.compareTo(o2);
        };

        System.out.println(com1.compare(12,6));

        System.out.println("*****************************");

        Comparator<Integer> com2 = (o1,o2) -> o1.compareTo(o2);

        System.out.println(com2.compare(12,21));

    }

    @Test
    public void test8(){
        Consumer<String> con1 = s -> {
            System.out.println(s);
        };
        con1.accept("一个是听得人当真了,一个是说的人当真了");

        System.out.println("*****************************");

        Consumer<String> con2 = s -> System.out.println(s);

        con2.accept("一个是听得人当真了,一个是说的人当真了");

    }

总结六种情况: -> 左边:lambda形参列表的参数类型可以省略(类型推断);如果lambda形参列表只一个参数,其一对( )也可以省略 -> 右边:lambda体应该使用一对{}包裹;如果lambda体只一条执行语句(可能是return语句,省略这一对{}和return关键字)