开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第12天,点击查看活动详情
构造器引用
格式: ClassName::new 与函数式接口相结合,自动与函数式接口中方法兼容。
可以把构造器引用赋值给定义的方法,要求构造器参数列表要与接口中抽象方法的参数列表一致!且方法的返回值即为构造器对应类的对象。
首先我们先创建一个Employee类
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public Employee() {
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public Employee(int id) {
this.id = id;
}
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
public void setAge(int age) {
this.age = age;
}
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 double getSalary() {
return salary;
}
public void setSalary(int salary) {
this.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;
return id == employee.id && age == employee.age && Double.compare(employee.salary, salary) == 0 && Objects.equals(name, employee.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name, age, salary);
}
}
//Supplier<T>中的 T get()
//Employee的空参构造器 Employee()
@Test
public void test1() {
//普通形式
Supplier<Employee> s1 = new Supplier<Employee>() {
@Override
public Employee get() {
return new Employee();
}
};
System.out.println(s1.get());
//lambda
Supplier<Employee> s2 = () -> new Employee();
System.out.println(s2.get());
//构造器引用
Supplier<Employee> s3 = Employee::new;
System.out.println(s3.get());
}
//Function<T,R>中的R apply(T t)
//Employee中的 Employee(int id)
@Test
public void test2() {
//普通
Function<Integer, Employee> f1 = new Function<Integer, Employee>() {
@Override
public Employee apply(Integer id) {
return new Employee(id);
}
};
System.out.println(f1.apply(1));
//lambda
Function<Integer, Employee> f2 = id -> new Employee(id);
System.out.println(f2.apply(2));
//构造器引用
Function<Integer, Employee> f3 = Employee::new;
System.out.println(f3.apply(3));
}
数组引用
格式: type[] :: new
//Function<T, R>中的R apply(T t)
//数组的创建 String[Integer l]
@Test
public void test1() {
//普通
Function<Integer, String[]> f1 = new Function<Integer, String[]>() {
@Override
public String[] apply(Integer length) {
return new String[length];
}
};
String[] a1 = f1.apply(4);
System.out.println(Arrays.toString(a1));
//lambda
Function<Integer, String[]> f2 = length -> new String[length];
String[] a2 = f2.apply(5);
System.out.println(Arrays.toString(a2));
//数组引用
Function<Integer, String[]> f3 = String[]::new;
String[] a3 = f3.apply(6);
System.out.println(Arrays.toString(a3));
}
结论
构造器引用: 和方法引用类似,函数式接口的抽象方法形参列表和构造器形参列表一致。抽象方法的返回值类型 即为 构造器所属类的类型
数组引用: 可以把数组看作是一个特殊的类,那么和构造器引用一样了
使用方法,构造器,数组引用必须保证该方法能和某一个对应的函数接口的方法返回值,参数类型保持一致。如果看不懂可以像我一样一步一步推导普通形式----->lambda----->各种引用。不必每个都使用,只要看的懂知道怎么来的就行。