java8-07-方法引用总结

一:方法引用
                    如果Lambda体中的内容  已经有方法实现了 我们可以使用"方法引用"
                    (可以理解为方法引用是 lambda 表达式的另一种表现形式)
 
                    1.对象 ::  实例方法名
 
                    2.类::静态方法名
 
                    3.类::实例方法名
 
            使用条件:
                a.  lambda 表达式 实现函数式接口中的方法    参数列表(参数个数,参数类型)
                    与 lambda体的中方法   参数列表(参数个数,参数类型)    返回值类型 必须一致
 
                b. 如果lambda体中参数列表第一个参数 是方法的调用者 
                  第二个参数是调用方法的参数
                                                             可以使用类名::实例方法名
二:构造方法引用
 
 调用构造 参数列表(参数个数  参数类型)和 使用的函数式接口  参数列表(参数个数  参数类型)必须一致
        类名::new
 
 
三:数组引用
 
        数据类型[ ]::new
 
 
 
 
一 方法引用
 
1.对象 ::  实例方法名
结果
 
 
分析 Lambda表达式 实现Consumer 接口的  accpet()方法   
                  参数列表(参数个数,参数类型)     返回值类型 
                         与 PrintStream对象中的  println()方法   
                  参数列表(参数个数,参数类型)    返回值类型
 
两个方法 都是一个参数 无返回值
 
 
 
 
再使用
结果
分析 
Lambda表示式实现的Supplier 接口 中 get()方法
          与  Employee对象中 getName()方法 参数列表  返回值一致
 
 
 
 
2.类::静态方法名
结果
 
分析Comparator 函数式接口 的compare() 方法
         与Integer 的静态方法 compare() 参数列表 返回值一致
 
 两个方法都是两个参数 返回一个结果
 
3.类::实例方法名
结果
 
 
二 构造方法引用   
 
调用构造 参数列表(参数个数  参数类型)和 使用的函数式接口  参数列表(参数个数  参数类型)必须一致
 
Employee类中 有无参数构造  一个参数构造 两个参数构造
调用无参数构造
调用一个参数构造   和 两个参数构造
 
三 数组引用
 
 
  1 package com.wf.zhang.java8.lamdba;
  2 
  3 import org.junit.Test;
  4 
  5 import java.io.PrintStream;
  6 import java.util.Comparator;
  7 import java.util.function.*;
  8 
  9 public class TestMethodRef {
 10     /**
 11      *
 12      *如果Lambda体中的内容  已经有方法实现了 我们可以使用"方法引用"
 13      * (可以理解为方法引用是 lambda 表达式的另一种表现形式)
 14      *
 15      *
 16      */
 17 
 18     //1.对象 :: 实例方法名
 19     @Test
 20     public  void test01(){
 21 
 22         //之前写法
 23         Consumer<String> con = (a) -> System.out.println(a);
 24         con.accept("kickoff");
 25 
 26         //使用方法引用
 27         Consumer<String> con2 = System.out::println;
 28         con2.accept("abode");
 29 
 30 
 31 
 32         //分析过程
 33         PrintStream ps = System.out;
 34         Consumer<String> cs =(x) ->ps.println(x);
 35 
 36         //println()方法已经实现 可以使用方法引用简化
 37         Consumer<String> cs2 =ps::println;
 38         //最终写法
 39         Consumer<String> cs3 =System.out::println;
 40     }
 41 
 42     @Test
 43     public void test02(){
 44 
 45         //之前写法
 46         Employee employee = new Employee();
 47         Supplier<String> su = () ->employee.getName();
 48         String s = su.get();
 49         System.out.println(s);
 50 
 51         //使用方法引用
 52         Supplier<String> su2 = employee::getName;
 53         String s1 = su2.get();
 54         System.out.println(s1);
 55     }
 56 
 57 
 58     //2.类::静态方法名
 59     @Test
 60     public void test03(){
 61         //之前写法
 62         Comparator<Integer> com = (a,b) ->Integer.compare(a,b);
 63         int compare = com.compare(66, 88);
 64         System.out.println(compare);
 65 
 66         //使用方法引用
 67         Comparator<Integer> com2 = Integer::compare;
 68         int compare2 = com2.compare(66, 88);
 69         System.out.println(compare2);
 70 
 71     }
 72 
 73 
 74     /**
 75      * 当lambda体中参数列表第一个参数是调用者,   x
 76      *                   第二个参数是调用方法的传参  y
 77      *                   可以使用类名::实例方法
 78      */
 79     //3.类::实例方法名
 80     @Test
 81     public  void  test04(){
 82         BiPredicate<String,String> bp = (x,y) -> x.equals(y);
 83         boolean b = bp.test("abc", "cba");
 84         boolean b2 = bp.test("abc", "abc");
 85         System.out.println(b);
 86         System.out.println(b2);
 87 
 88         System.out.println("-------------使用方法引用后---------------");
 89 
 90 
 91         BiPredicate<String,String> bp2 = String::equals;
 92         boolean b3 = bp2.test("abc", "cba");
 93         boolean b4 = bp2.test("abc", "abc");
 94         System.out.println(b3);
 95         System.out.println(b4);
 96     }
 97 
 98     /**
 99      * 构造方法引用
100      */
101     @Test
102     public void test05(){
103 
104         //Supplier接口 无参数 返回一个接口
105         Supplier<Employee> sup = () -> new Employee();
106 
107         //构造方法引用   引用无参数构造
108         Supplier<Employee> sup2 = Employee::new;
109         Employee employee = sup2.get();
110         System.out.println(employee);
111 
112     }
113 
114 
115     @Test
116     public  void  test06(){
117         Function<Integer,Employee> fun = (a)->new Employee(a);
118 
119         //构造方法引用   引用一个参数构造
120         Function<Integer,Employee> fun2 = Employee::new;
121         Employee employee = fun2.apply(666);
122         System.out.println(employee);
123 
124         //构造方法引用   引用一个参数构造
125         BiFunction<Integer,Integer,Employee> bf =Employee::new;
126         Employee employee2 = bf.apply(123, 456);
127         System.out.println(employee2);
128 
129     }
130 
131     /**
132      * 数组引用
133      */
134     @Test
135     public void test07(){
136         Function<Integer,String[]> fun = (t) ->new String[t];
137         String[] str = fun.apply(10);
138         System.out.println(str.length);
139 
140         //使用数组引用
141         Function<Integer,String[]> fun2 = String[]::new;
142         String[] str2 = fun.apply(10);
143         System.out.println(str2.length);
144     }
145 
146 }
View Code

 

posted @ 2019-11-09 13:54  wf.zhang  阅读(337)  评论(0编辑  收藏  举报