一、方法引用
- 方法引用可以看做是
Lambda
表达式深层次的表达。换句话说,方法引用就是Lambda
表达式,也就是函数式接口的一个实例,通过方法的名字来指向一个方法。 - 当要传递给
Lambda
体的操作,已经有实现的方法了,就可以使用方法引用。
二、方法引用格式及使用方法
类(或对象) :: 方法名
- 对象 :: 非静态方法
- 类 :: 静态方法
- 类 :: 非静态方法
public class MethodRefTest {
// 情况一:对象 :: 实例方法 要求接口中的抽象方法的形参列表和返回值类型与方法引用的方法的形参列表和返回值类型相同
//Consumer中的void accept(T t)
//PrintStream中的void println(T t)
@Test
public void test1() {
Consumer<String> consumer1 = str -> System.out.println(str);
consumer1.accept("北京");
System.out.println("**********方法引用**********");
PrintStream ps = System.out;
Consumer<String> consumer2 = ps::println;
consumer2.accept("beijing");
}
//Supplier中的T get()
//Employee中的String getName()
@Test
public void test2() {
Employee employee = new Employee(1001,"zhangsan",34,20000);
Supplier<String> supplier1 = () -> employee.getName();
System.out.println(supplier1.get());
System.out.println("**********方法引用**********");
Supplier<String> supplier2 = employee::getName;
System.out.println(supplier2.get());
}
// 情况二:类 :: 静态方法 要求接口中的抽象方法的形参列表和返回值类型与方法引用的方法的形参列表和返回值类型相同
//Comparator中的int compare(T t1,T t2)
//Integer中的int compare(T t1,T t2)
@Test
public void test3() {
Comparator<Integer> comparator1 = (t1,t2) -> Integer.compare(t1,t2);
System.out.println(comparator1.compare(23, 44));
System.out.println("**********方法引用**********");
Comparator<Integer> comparator2 = Integer::compare;
System.out.println(comparator2.compare(11, 33));
}
//Function中的R apply(T t)
//Math中的Long round(Double d)
@Test
public void test4() {
Function<Double,Long> function1 = d -> Math.round(d);
System.out.println(function1.apply(11.1));
System.out.println("**********方法引用**********");
Function<Double,Long> function2 = Math::round;
System.out.println(function2.apply(11.9));
}
// 情况三:类 :: 实例方法 当函数式接口方法的第一个参数是需要引用方法的调用者,并且第二个参数是需要引用方法的参数(或无参数)时:ClassName::methodName
// Comparator中的int comapre(T t1,T t2)
// String中的int t1.compareTo(t2)
@Test
public void test5() {
Comparator<String> comparator1 = (s1,s2) -> s1.compareTo(s2);
System.out.println(comparator1.compare("aaa", "aab"));
System.out.println("**********方法引用**********");
Comparator<String> comparator2 = String::compareTo;
System.out.println(comparator2.compare("asd", "dsa"));
}
//BiPredicate中的boolean test(T t1, T t2);
//String中的boolean t1.equals(t2)
@Test
public void test6() {
BiPredicate<String,String> predicate1 = (s1,s2) -> s1.equals(s2);
System.out.println(predicate1.test("asd", "asa"));
System.out.println("**********方法引用**********");
BiPredicate<String,String> predicate2 = String::equals;
System.out.println(predicate2.test("sdf", "sdfg"));
}
// Function中的R apply(T t)
// Employee中的String getName();
@Test
public void test7() {
Employee employee = new Employee(1002,"lisi",22,22333);
Function<Employee,String> function1 = e -> e.getName();
System.out.println(function1.apply(employee));
System.out.println("**********方法引用**********");
Function<Employee,String> function2 = Employee::getName;
System.out.println(function2.apply(employee));
}
}
三、构造器引用与数组引用
和方法引用类似,函数式接口的抽象方法的形参列表和构造器的形参列表一致。抽象方法的返回值类型即为构造器所属的类的类型。
//构造器引用
//Supplier中的T get()
@Test
public void test1(){
Supplier<Employee> supplier1 = () -> new Employee();
System.out.println(supplier1.get());
System.out.println("**********构造器引用**********");
Supplier<Employee> supplier2 = Employee::new;
System.out.println(supplier2.get());
}
//Function中的R apply(T t)
@Test
public void test2(){
Function<Integer,Employee> function1 = id -> new Employee(id);
Employee employee = function1.apply(1001);
System.out.println(employee);
System.out.println("**********构造器引用**********");
Function<Integer,Employee> function2 = Employee::new;
Employee employee1 = function2.apply(1002);
System.out.println(employee1);
}
//BiFunction中的R apply(T t,U u)
@Test
public void test3(){
BiFunction<Integer,String,Employee> function1 = (id,name) -> new Employee(id,name);
System.out.println(function1.apply(1001, "zhangsan"));
System.out.println("**********构造器引用**********");
BiFunction<Integer,String,Employee> function2 = Employee::new;
System.out.println(function2.apply(1002, "lisi"));
}
数组引用可以把数组看做是一个特殊的类,则写法与构造器引用一致。
数组类型[] :: new
//数组引用
//Function中的R apply(T t)
@Test
public void test4(){
Function<Integer,String[]> function1 = length -> new String[length];
String[] arr1 = function1.apply(5);
System.out.println(Arrays.toString(arr1));
System.out.println("**********数组引用**********");
Function<Integer,String[]> function2 = String[]::new;
String[] arr2 = function2.apply(10);
System.out.println(Arrays.toString(arr2));
}