方法引用的使用–Java8新特性
1.使用情境:
当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法的引用!
2.方法引用本质
本质上就是Lambda表达式,而Lambda表达式作为函数式接口的实例。所以方法引用,也是
函数式接口的实例。
3.使用格式:
类(或对象即方法调用者) :: 方法名
4.具体分为如下三种情况:
对象 :: 非静态方法
类 :: 静态方法
类 :: 非静态方法 (注意:方法引用中可以这样)
5.方法引用使用的要求:
要求接口中的抽象方法的形参列表和返回值类型和方法引用的方法的形参列表和返回值相同!
下面介绍使用情况的实例分析:
/*
* @author zhCoding
* @create 2020-08-10 14:13
*/
public class MethodReference {
//情况一: 对象 :: 实例方法
//Consumer中的void accept(T t);
//PrintStream中的void println(T t);
@Test
public void test1(){
Consumer<String> con1 = s -> System.out.println(s);
con1.accept("北京");
System.out.println("*************************");
Consumer<String> con2 = System.out::println;
con2.accept("南京");
}
//情况一: 对象::非静态方法
//Supplier中的T get()
//Employee中的String getName()
@Test
public void test2(){
Supplier<String> sup1 = new Supplier<String>() {
@Override
public String get() {
return "背景";
}
};
System.out.println(sup1.get());
System.out.println("*****************");
Supplier<String> sup2 = () -> "东京";
System.out.println(sup2.get());
System.out.println("*************");
Employee emp = new Employee("Jerry",23,15000);
Supplier<String> sup3 = () -> emp.getName();
System.out.println(sup3.get());
System.out.println("******************");
Supplier<String> sup4 = emp::getName;
System.out.println(sup4.get());
}
//情况二: 类::静态方法
//Comparator中的int compare(T t1,T t2)
// Integer中的 int compare(T t1,T t2)
@Test
public void test3(){
Comparator<Integer> com1 = (o1,o2) -> Integer.compare(o1,o2);
System.out.println(com1.compare(12, 21));
System.out.println("************************");
Comparator<Integer> com2 = Integer :: compare;
System.out.println(com2.compare(21, 12));
}
//Math中的Long round(Double d)
//Function中的 R apply(T t)
@Test
public void test4(){
Function<Double,Long> fun1 = d -> Math.round(d);
System.out.println(fun1.apply(15.753));
System.out.println("*******************");
Function<Double,Long> fun2 = Math::round;
System.out.println(fun2.apply(16.3521));
}
//情况三: 类 :: 实例方法 (有难度)
//Comparator中的int compare(T t1,T t2)
// String 中的int t1.compareTo(t2)
@Test
public void test5(){
Comparator<String> com1 = (s1,s2) -> s1.compareTo(s2);
System.out.println(com1.compare("abc", "abd"));
System.out.println("*****************");
Comparator<String> com2 = String::compareTo;
System.out.println(com2.compare("abd", "abc"));
}
//BiPredicate中的boolean test(T t1,T t2)
// String中的boolean t1.equals(t2)
@Test
public void test6(){
BiPredicate<String,String> bip1 = (s1,s2) -> s1.equals(s2);
System.out.println(bip1.test("abb", "abb"));
System.out.println("************");
BiPredicate<String,String> bip2 = String::equals;
System.out.println(bip2.test("bba", "abb"));
}
//Function中的 R apply(T t)
//Employee中的 String getName()
@Test
public void test7(){
Employee emp = new Employee("Tony",23,18000);
Function<Employee,String> fun1 = (e) -> e.getName(); //R apply(T t)里面放的t 是方法的调用者
System.out.println(fun1.apply(emp));
System.out.println("*******************");
Function<Employee,String> fun2 = Employee::getName;
System.out.println(fun2.apply(emp));
}
}