• java8新特性(方法引用与构造器)


     

    方法引用: 若Lambda中的内容有方法已经实现,可以使用方法引用(方法引用是Lambda的另一种表达方式)

    注意:

      ①方法引用所引用的方法的参数列表与返回值类型,需要与函数式接口中抽象方法的参数列表和返回值类型保持一致!

      ②若Lambda 的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,格式: ClassName::MethodName

     

      1、对象的应用::实例方法名

           @Test
        public void test1(){
            PrintStream ps = System.out;
            Consumer<String> con = (str) -> ps.println(str);
            con.accept("Hello World!");
            
            System.out.println("--------------------------------");
            
            Consumer<String> con2 = ps::println;
            con2.accept("Hello Java8!");
            
            Consumer<String> con3 = System.out::println;
        }
        //对象的引用 :: 实例方法名
        @Test
        public void test2(){
            Employee emp = new Employee(101, "张三", 18, 9999.99);
            
            Supplier<String> sup = () -> emp.getName();
            System.out.println(sup.get());
            
            System.out.println("----------------------------------");
            
            Supplier<String> sup2 = emp::getName;
            System.out.println(sup2.get());
        }

     

     

      2、类名::静态方法名

    @Test
        public void test4(){
            Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
            
            System.out.println("-------------------------------------");
            
            Comparator<Integer> com2 = Integer::compare;
        }
        
        @Test
        public void test3(){
            BiFunction<Double, Double, Double> fun = (x, y) -> Math.max(x, y);
            System.out.println(fun.apply(1.5, 22.2));
            
            System.out.println("--------------------------------------------------");
            
            BiFunction<Double, Double, Double> fun2 = Math::max;
            System.out.println(fun2.apply(1.2, 1.5));
        }

     

     

      3、类名::实例方法名

    //类名 :: 实例方法名
        @Test
        public void test5(){
            BiPredicate<String, String> bp = (x, y) -> x.equals(y);
            System.out.println(bp.test("abcde", "abcde"));
            
            System.out.println("-----------------------------------------");
            
            BiPredicate<String, String> bp2 = String::equals;
            System.out.println(bp2.test("abc", "abc"));
            
            System.out.println("-----------------------------------------");
            
            
            Function<Employee, String> fun = (e) -> e.show();
            System.out.println(fun.apply(new Employee()));
            
            System.out.println("-----------------------------------------");
            
            Function<Employee, String> fun2 = Employee::show;
            System.out.println(fun2.apply(new Employee()));
            
        }

     

    构造器引用:构造器的参数列表,需要与函数式接口中的参数列表一致

      1、类名::new

    @Test
        public void test7(){
            Function<String, Employee> fun = Employee::new;
            
            BiFunction<String, Integer, Employee> fun2 = Employee::new;
        }
        
        @Test
        public void test6(){
            Supplier<Employee> sup = () -> new Employee();
            System.out.println(sup.get());
            
            System.out.println("------------------------------------");
            
            Supplier<Employee> sup2 = Employee::new;
            System.out.println(sup2.get());
        }

    数组引用:

      类型[] :: new

    @Test
        public void test8(){
            Function<Integer, String[]> fun = (args) -> new String[args];
            String[] strs = fun.apply(10);
            System.out.println(strs.length);
            
            System.out.println("--------------------------");
            
            Function<Integer, Employee[]> fun2 = Employee[] :: new;
            Employee[] emps = fun2.apply(20);
            System.out.println(emps.length);
        }
  • 相关阅读:
    C#_WinForm接收命令行参数
    SQLite不支持的SQL语法总结
    Thirft框架介绍
    jquery获取复选框的值
    REST构架风格介绍:状态表述转移
    RESTful HTTP的实践infoQ
    C#如何在webBrowser1控件通过TagName,Name查找元素(没有ID时)遍历窗体元素
    Html TO Ubb and Ubb TO Html
    SQL 位运算
    Memcached真的过时了吗?【转帖】
  • 原文地址:https://www.cnblogs.com/lzb0803/p/9068962.html
Copyright © 2020-2023  润新知