一秸弛、方法引用
1、什么是方法引用
當(dāng)要傳遞給Lambda體的操作搪搏,已經(jīng)有實(shí)現(xiàn)的方法狭握,可以使用方法引用。(實(shí)現(xiàn)抽象方法的參數(shù)列表疯溺,必須與方法引用方法的參數(shù)列表保持一致)
(可以將方法引用理解為 Lambda 表達(dá)式的另外一種表現(xiàn)形式)
方法引用:使用操作符“::”將方法名和對象或類的名字分隔開來论颅。
如下三種主要使用情況:
對象的引用 :: 實(shí)例方法名
類名 :: 靜態(tài)方法名
-
類名 :: 實(shí)例方法名
注意:
①方法引用所引用的方法的參數(shù)列表與返回值類型,需要與函數(shù)式接口中抽象方法的參數(shù)列表和返回值類型保持一致囱嫩!
②若Lambda 的參數(shù)列表的第一個(gè)參數(shù)恃疯,是實(shí)例方法的調(diào)用者,第二個(gè)參數(shù)(或無參)是實(shí)例方法的參數(shù)時(shí)墨闲,格式: ClassName::MethodName
2今妄、方法引用舉例
(1)、對象的引用 :: 實(shí)例方法名
// 1. 對象的引用 :: 實(shí)例方法名
@Test
public void test() {
// ①方法引用所引用的方法的參數(shù)列表與返回值類型鸳碧,需要與函數(shù)式接口中抽象方法的參數(shù)列表和返回值類型保持一致盾鳞!
Consumer<String> con = (x) -> System.out.println(x);
con.accept("hello");
PrintStream ps = System.out;
Consumer<String> con2 = (x) -> ps.println(x);
con2.accept("hello");
// 對象的引用 :: 實(shí)例方法名
PrintStream ps2 = System.out;
Consumer<String> con3 = ps2::println;
con3.accept("hello");
}
@Test
public void test2() {
Employee emp = new Employee();
Supplier<String> sup = () -> emp.getName();
String str = sup.get();
System.out.println(str);
// 對象的引用 :: 實(shí)例方法名
Supplier<Integer> sup2 = emp::getAge;
Integer num = sup2.get();
System.out.println(num);
}
(2)、類名 :: 靜態(tài)方法名
// 2. 類名 :: 靜態(tài)方法名
@Test
public void test3() {
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
// int compare = com.compare(2, 3);
// System.out.println(compare);
// 類名 :: 靜態(tài)方法名
Comparator<Integer> com2 = Integer::compare;
}
(3)瞻离、類名 :: 實(shí)例方法名
// 3. 類名 :: 實(shí)例方法名
@Test
public void test4() {
BiPredicate<String, String> bp = (x, y) -> x.equals(y);
// 使用要求: 若Lambda 的參數(shù)列表的第一個(gè)參數(shù)腾仅,是實(shí)例方法的調(diào)用者,第二個(gè)參數(shù)(或無參)是實(shí)例方法的參數(shù)時(shí)套利,格式:
// ClassName::MethodName
BiPredicate<String, String> bp2 = String::equals;
// boolean test = bp2.test("hello", "hello");
// System.out.println(test);
}
}
二推励、構(gòu)造器引用
1、什么是構(gòu)造器引用
格式:ClassName::new
與函數(shù)式接口相結(jié)合日裙,自動與函數(shù)式接口中方法兼容吹艇。可以把構(gòu)造器引用賦值給定義的方法昂拂,與構(gòu)造器參數(shù)列表要與接口中抽象方法的參數(shù)列表一致受神!
2、構(gòu)造器引用的例子
//二格侯、構(gòu)造器引用 :構(gòu)造器的參數(shù)列表鼻听,需要與函數(shù)式接口中參數(shù)列表保持一致财著!
//1. 類名 :: new
//ClassName::new
//注意:需要調(diào)用的構(gòu)造器的參數(shù)列表要與函數(shù)式接口中抽象方法的參數(shù)列表保持一致、
@Test
public void test5() {
Supplier<Employee> sup =()->new Employee();
//構(gòu)造器引用方式
Supplier<Employee> sup2=Employee::new;
Employee emp=sup2.get();
System.out.println(emp);
}
@Test
public void test6() {
Function<Integer,Employee> fun=(x)->new Employee(x);
Function<Integer,Employee> fun2=Employee::new;
Employee emp=fun2.apply(101);
System.out.println(emp);
//需要調(diào)用的構(gòu)造器的參數(shù)列表要與函數(shù)式接口中抽象方法的參數(shù)列表保持一致撑碴、
//這里的參數(shù)列表撑教,就對應(yīng)employee的構(gòu)造器的列表一樣
BiFunction<Integer,Integer,Employee> bif=Employee::new;
Employee apply = bif.apply(2, 3);
System.out.println(apply);
}
三、數(shù)組引用
1醉拓、數(shù)組引用格式
格式:type[]::new
2伟姐、數(shù)組引用例子
//數(shù)組引用
//三、數(shù)組引用
//類型[] :: new;
@Test
public void test7() {
Function<Integer,String[]> fun=(x)->new String[x];
String[] apply = fun.apply(10);
System.out.println(apply.length);
Function<Integer,String[]> fun2=String[]::new;
String[] apply2 = fun2.apply(10);
System.out.println(apply2.length);
}