posts - 325,  comments - 25,  trackbacks - 0

          /*
           * 一、方法引用:若 Lambda 體中的功能,已經(jīng)有方法提供了實(shí)現(xiàn),可以使用方法引用
           *      (可以將方法引用理解為 Lambda 表達(dá)式的另外一種表現(xiàn)形式)
           *
           * 1. 對(duì)象的引用 :: 實(shí)例方法名
           *
           * 2. 類名 :: 靜態(tài)方法名
           *
           * 3. 類名 :: 實(shí)例方法名
           *
           * 注意:
           *   ①方法引用所引用的方法的參數(shù)列表與返回值類型,需要與函數(shù)式接口中抽象方法的參數(shù)列表和返回值類型保持一致!
           *   ②若Lambda 的參數(shù)列表的第一個(gè)參數(shù),是實(shí)例方法的調(diào)用者,第二個(gè)參數(shù)(或無參)是實(shí)例方法的參數(shù)時(shí),格式: ClassName::MethodName
           *
           * 二、構(gòu)造器引用 :構(gòu)造器的參數(shù)列表,需要與函數(shù)式接口中參數(shù)列表保持一致!
           *
           * 1. 類名 :: new
           *
           * 三、數(shù)組引用
           *
           *  類型[] :: new;
           *
           *
           */
          public class TestMethodRef {
           //數(shù)組引用
           @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);
           }
           
           //構(gòu)造器引用
           @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());
           }
           
           //類名 :: 實(shí)例方法名
           @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()));
            
           }
           
           //類名 :: 靜態(tài)方法名
           @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));
           }

           //對(duì)象的引用 :: 實(shí)例方法名
           @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());
           }
           
           @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;
           }

          posted on 2018-03-06 08:35 長(zhǎng)春語林科技 閱讀(127) 評(píng)論(0)  編輯  收藏 所屬分類: java8
          <2018年3月>
          25262728123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

           

          長(zhǎng)春語林科技?xì)g迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          收藏夾

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 大城县| 桓台县| 平舆县| 慈溪市| 陆川县| 五台县| 蓬莱市| 平昌县| 濮阳市| 淳化县| 岑巩县| 岳阳县| 泰宁县| 利辛县| 盐山县| 沙雅县| 德庆县| 东乡县| 卓尼县| 德兴市| 徐水县| 平远县| 扎鲁特旗| 洛隆县| 合肥市| 遵义县| 东港市| 海阳市| 屏山县| 大姚县| 达尔| 襄垣县| 客服| 平陆县| 沈阳市| 泊头市| 榆树市| 库尔勒市| 河东区| 马边| 马关县|