Java8新特性之Lambda

  1. Lambda表达式
    1. 概念:lambda表达式从本质上讲,是一个匿名函数。可以使用这个匿名函数,以简洁的方式来实现接口中的方法,从而简化代码。
    2. 使用场景:简化接口使用。使用接口的方式有多种,如实现类、匿名内部类等,对于lambda表达式而言,可以达到同样的效果,且代码较为简洁。  
      1.   如:
        private interface Num {
                int test1(int num1, int num2);
            }
            @Test
            public void test3(){
                Num num = new Num() {
                    @Override
                    public int test1(int num1, int num2) {
                        return num1 * num2;
                    }
                };
                System.out.println(num);
        
            }

        可以简化为:

        private interface Num {
                int test1(int num1, int num2);
            }
            @Test
            public void test3(){
                Num num = (num1, num2) -> num1 * num2;
        
            }

        lambda表达式并不是适用于所有的接口,而是特定的接口:函数式接口,在此不过多讲述什么是函数式接口,可以利用@FunctionalInterface注解类验证是否是函数式接口。

    3. 相关语法:lambda表达式是一个匿名函数,不需要关注具体方法名,事实上,返回值也可不用关注,只要关注参数列表以及方法体即可。
      1.   参数部分的精简:
        1.   参数的类型:
          1.   由于在接口的方法中,已经定义了每一个参数的类型是什么,且利用lambda时,必须保证参数的数量以及类型要和接口定义的一致,因此,lambda表达式中的参数可以不写。
        2.        参数的小括号:若参数列表有且只有一个,可以省略不写;
      2.        方法体大括号的精简:
        1.   若方法体只有一句,则可以精简;
        2.        若方法体只有一句,且是返回语句(return),则省略大括号的同时,return也一并省略掉。
    4. 方法引用:
      1.   构造方法的引用
        1.   如果函数式接口定义的方法是为了得到某个类的对象,可以使用lambda来简化。语法为类名::new
          1.   如:
            //对象
            private
            static class Student { int id; String name; public Student(int id) { System.out.println("Single Param"); this.id = id; } public Student(int id, String name) { System.out.println("Multiple Params"); this.id = id; this.name = name; } public Student() { System.out.println("No Param"); } }
            //函数式接口
            private interface S1 { Student get(); } private interface S2 { Student get(int id); } private interface S3 { Student get(int id, String name); }
            public static void main(String[] args) {
                    S1 s1 = Student::new;
                    s1.get();
                    S2 s2 = Student::new;
                    s2.get(3);
                    S3 s3 = Student::new;
                    s3.get(44, "zhangsan");
            
                }

            结果如下:

        2. 静态方法的引用:   

          1.   语法 类名::方法名 
            1.   
              public class Test1 {
              
                  public static int f1(int num1, int num2) {
                      return num1 + num2;
              
                  }
              }
              public static void main(String[] args) {
                      Num g1 = (x, y) -> f1(x, y);
                      System.out.println(g1.test1(1, 2));
                      // 可简化为以下方式
                      Num g2 = Test1::f1;
                      System.out.println(g2.test1(2, 3));
                  }
        3. 非静态方法
          1.   语法:对象::方法名。 
      2.  注意:对于方法引用,不必使用(),即对象::方法名()为错误使
  2. 相关类:
    /**
     * 简单描述
     *
     * @author lijie
     * @data 2021/2/15 21:13
     */
    @FunctionalInterface
    public interface NoReturnMuleParams {
        void test1(int a, int b);
    }
    /**
     * 简单描述
     *
     * @author lijie
     * @data 2021/2/15 21:11
     */
    @FunctionalInterface
    public interface NoReturnNoParams {
        void test1();
    }
    /**
     * 简单描述
     *
     * @author lijie
     * @data 2021/2/15 21:12
     */
    @FunctionalInterface
    public interface NoReturnSingleParam {
        void test1(int a);
    }
    /**
     * 简单描述
     *
     * @author lijie
     * @data 2021/2/15 21:16
     */
    @FunctionalInterface
    public interface SingleMuleParams {
        int test1(int a, int b);
    }
    /**
     * 简单描述
     *
     * @author lijie
     * @data 2021/2/15 21:15
     */
    @FunctionalInterface
    public interface SingleReturnSingleParam {
        int test1(int a);
    
    }
    /**
     * 简单描述
     *
     * @author lijie
     * @data 2021/2/17 9:24
     */
    public class Test1 {
        @Test
        public void test1() {
            System.out.println("NoReturnMuleParams2");
            NoReturnMuleParams2 n1 = (int a, int b) -> a * b;
    
            int i = n1.test1(1, 8);
            System.out.println(i);
        }
    
        @Test
        public void test2() {
            NoReturnSingleParam n2 = x -> System.out.println(x * 2);
            n2.test1(8);
        }
    
        public static int f1(int num1, int num2) {
            return num1 + num2;
    
        }
    @FunctionalInterface
        private interface Num {
            int test1(int num1, int num2);
        }
        @Test
        public void test3(){
            Num num = (num1, num2) -> num1 * num2;
    
        }
    
    
        private static class Student {
            int id;
            String name;
    
            public Student(int id) {
                System.out.println("Single Param");
                this.id = id;
            }
    
            public Student(int id, String name) {
                System.out.println("Multiple Params");
                this.id = id;
                this.name = name;
            }
    
            public Student() {
                System.out.println("No Param");
            }
        }
    
        private interface S1 {
            Student get();
        }
    
        private interface S2 {
            Student get(int id);
        }
    
        private interface S3 {
            Student get(int id, String name);
        }
    
        public int f2(int num1, int num2) {
            if (num1 > num2) {
                return num1 - num2;
            }
            return num2 - num1;
        }
    
        public static void main(String[] args) {
            Num g1 = (x, y) -> f1(x, y);
            System.out.println(g1.test1(1, 2));
            Num g2 = Test1::f1;
            System.out.println(g2.test1(2, 3));
            System.out.println("------------------------");
            Num g3 = new Test1()::f2;
            System.out.println(g3.test1(3, 5));
            System.out.println("------------------------");
    
            S1 s1 = Student::new;
            s1.get();
            S2 s2 = Student::new;
            s2.get(3);
            S3 s3 = Student::new;
            s3.get(44, "zhangsan");
        }
    
    
    }

No ParamSingle ParamMultiple Params

原文地址:https://www.cnblogs.com/codehero/p/14408380.html