package java_8; import org.junit.Test; import java.io.PrintStream; import java.util.Comparator; import java.util.function.*; import java.util.function.Function; /** * 方法引用:若Lambda體中的內容有方法已經實現了,那么我們可以使用"方法引用" * (可以理解為方法引用時Lambda表達式的另外一種表現形式 * * 主要有三種語法格式: * * 對象::實例方法名 * * 類::靜態方法名 * * 類::實例方法名 * * 注意: * 1. Lambda體中調用方法的參數列表與返回值類型,要與函數式接口中抽象方法的函數列表和返回值類型保存一致 * 2.若Lambda參數列表中的第一個參數是實例方法的調用者,而第二個參數是實例方法的參數時,可以使用ClassName::method * * 二、構造器引用 * 格式: * ClassName::new * 注意:需要調用的構造器方法與函數式接口中抽象方法的參數列表保持一致 * * 三、數組引用 * Type::new; */ public class TestMethodRef { //數組引用: @Test public void test7(){ Function<Integer, String[]> fun = x -> new String[x]; String[] strs = fun.apply(10); System.out.println(strs.length); Function<Integer,String[]> fun1 = String[]::new; strs = fun1.apply(20); System.out.println(strs.length); } //構造器引用 @Test public void test5(){ Supplier<Employee> sup = ()-> new Employee(); Employee emp = sup.get(); //構造器引用 //根據參數列表自動匹配構造器 Supplier<Employee> sup2 = Employee::new; emp = sup2.get(); System.out.println(emp); } @Test public void test6(){ Function<Integer,Employee> func = x -> new Employee(x); Employee emp = func.apply(10); System.out.println(emp); Function<Integer,Employee> func1 = Employee :: new; emp = func1.apply(10); System.out.println(emp); // BiFunction<Integer, Integer, Employee> bf = Employee::new;編譯錯誤,沒有兩個Integer構造器 } //對象::實例方法名 @Test public void test1(){ Consumer<String> con = x -> System.out.println(x); PrintStream ps = System.out; //打印流 //前提條件: Consumer中的方法體參數與返回值要與ps.println方法中的參數和返回值類型相同 //Consumer: void accept(T t);在這里T為String //PrintStream: public void println(String x) //兩者傳入的參數都為String,返回值都為void所以滿足,可以使用方法引用 Consumer<String> con1 = ps::println; Consumer<String> con2 = System.out::println;//這三種方式結果相同 con.accept("huang"); con1.accept("huang"); con2.accept("huang"); } @Test public void test2(){ Employee emp = new Employee(); Supplier<String> sup = () -> emp.getName(); Supplier<String> sup2 = emp::getName; } //--------------------------------------- //類::靜態方法名 @Test public void test3(){ Comparator<Integer> com = (x, y) ->Integer.compare(x, y); //前提條件:和上面相同 Comparator<Integer> com1 = Integer::compare; } //類::實例方法名 @Test public void test4(){ BiPredicate<String, String> bp = (x, y) -> x.equals(y); boolean bool = bp.test(new String("huang"),"huang"); System.out.println(bool); //前提:第一個參數是實例方法的調用者,第二個參數是實例方法的參數 //例如 x 是equal方法的調用者,y是實例方法的參數 BiPredicate<String,String> bp2 = String::equals; bool = bp2.test("huang","huang"); System.out.println(bool); } }
package java_8; public class Employee { private int id; private String name; private int age; private double salary; public Employee() { } public Employee(int id){ this.id = id; } @Override public String toString() { return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}'; } public Employee(String name, int age, double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }