方法引用的使用情景:
当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用
方法引用本质上是Lambda表达式,所以方法引用作为函数式接口实例
使用格式:
类(或对象)::方法名(不用写参数列表)
类(或对象)相当于就是调用者
比如accept方法用println方法去做替换,而println的调用者就是System.out返回的打印流的对象
具体分为如下三种情况:
1)对象::非静态方法名(实例方法名)
2)类::静态方法名
3)类::非静态方法名
注意:不能写对象::静态方法名
方法引用使用的要求:
要求接口中的抽象方法的形参列表和返回值类型与方法引用的方法的形参列表(具体类型也要相同)和返回值类型都相同(适用于情况1和情况2,注意情况3时有区别)
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public Employee() {
}
public Employee(int id) {
this.id = id;
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}';
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Employee employee = (Employee) o;
if (id != employee.id)
return false;
if (age != employee.age)
return false;
if (Double.compare(employee.salary, salary) != 0)
return false;
return name != null ? name.equals(employee.name) : employee.name == null;
}
@Override
public int hashCode() {
int result;
long temp;
result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + age;
temp = Double.doubleToLongBits(salary);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
}
public class EmployeeData {
public static List<Employee> getEmployees(){
List<Employee> list = new ArrayList<>();
list.add(new Employee(1001, "马化腾", 34, 6000.38));
list.add(new Employee(1002, "马云", 12, 9876.12));
list.add(new Employee(1003, "刘强东", 33, 3000.82));
list.add(new Employee(1004, "雷军", 26, 7657.37));
list.add(new Employee(1005, "李彦宏", 65, 5555.32));
list.add(new Employee(1006, "比尔盖茨", 42, 9500.43));
list.add(new Employee(1007, "任正非", 26, 4333.32));
list.add(new Employee(1008, "扎克伯格", 35, 2500.32));
return list;
}
}
public class MethodRefTest {
// 情况一:对象 :: 实例方法
//Consumer中的void accept(T t)
//PrintStream中的void println(T t)
@Test
public void test1() {
//Lambda表达式的写法
Consumer<String> con1= str-> System.out.println(str);//System.out.println(str)是Lambda体
con1.accept("北京");//北京
System.out.println("********************************");
//方法引用的写法
//Consumer中方法的特点为放一个变量不返回,而打印流PrintStream(System.out返回的就是一个打印流对象)中有一个方法叫做println
//println也是放一个参数的类型但不返回
//所以两者有联系
//所以可以把Lambda表达式升级,可以用方法引用
PrintStream ps=System.out;
Consumer<String> con2= ps::println;
con2.accept("北京");//北京
}
//Supplier中的T get()
//Employee中的String getName()
//这两个是类似的,什么也不放但是返回东西
@Test
public void test2() {
Employee emp=new Employee(1001,"Tom",23,5600);
Supplier<String> sup1=()->emp.getName();
System.out.println(sup1.get());//Tom
System.out.println("******************************");
//方法引用
Supplier<String> sup2=emp::getName;
System.out.println(sup2.get());//Tom
}
// 情况二:类 :: 静态方法
//Comparator中的int compare(T t1,T t2)
//Integer中的int compare(T t1,T t2)
@Test
public void test3() {
Comparator<Integer> com1=(t1,t2)->Integer.compare(t1,t2);//因为只有一条执行语句,所以大括号省略掉了,又因为是return,所以省略调return
System.out.println(com1.compare(12,21));//-1
System.out.println("**********************************");
//使用方法引用
Comparator<Integer> com2=Integer::compare;
System.out.println(com2.compare(12,3));//1
}
//Function中的R apply(T t)
//Math中的Long round(Double d)
@Test
public void test4() {
Function<Double,Long> func2=new Function<Double, Long>() {
@Override
public Long apply(Double d) {
return Math.round(d);
}
};
System.out.println("***********************");
Function<Double,Long> func1= d->Math.round(d);
System.out.println(func1.apply(12.3));//12
System.out.println("**********************");
Function<Double,Long> func3=Math::round;
System.out.println(func3.apply(12.6));//13
}
// 情况三:类 :: 实例方法
// Comparator中的int comapre(T t1,T t2)
// String中的int t1.compareTo(t2)
//这两个东西并不匹配,但t1是作为调用者出现的
@Test
public void test5() {
Comparator<String> com1=(s1,s2)->s1.compareTo(s2);
System.out.println(com1.compare("abc","abd"));//-1
System.out.println("***********************");
Comparator<String> com2=String::compareTo;
System.out.println(com2.compare("abd","abm"));//-9
}
//BiPredicate中的boolean test(T t1, T t2);
//String中的boolean t1.equals(t2)
//因为t1作为方法的调用者,所以也可以用方法引用
@Test
public void test6() {
BiPredicate<String,String> pre1=(s1,s2)->s1.equals(s2);
System.out.println(pre1.test("abc","abc"));//true
System.out.println("********************************");
BiPredicate<String,String> pre2=String::equals;
System.out.println(pre2.test("abc","abc"));//true
}
// Function中的R apply(T t)
// Employee中的String getName();
//T就是Employee的一个对象,就是方法的调用者
@Test
public void test7() {
Employee employee = new Employee(1001, "Jerry", 23, 6000);
Function<Employee,String> func1=e->e.getName();
System.out.println(func1.apply(employee));//Jerry
System.out.println("***********************************");
Function<Employee,String> func2=Employee::getName;
System.out.println(func2.apply(employee));//Jerry
}
}