构造器引用

格式: ClassName::new 与函数式接口相结合,自动与函数式接口中方法兼容。

可以把构造器引用赋值给定义的方法,要求构造器参数列表要与接口中抽象方法的参数列表一致!且方法的返回值即为构造器对应类的对象。

首先我们先创建一个Employee类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

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);
}
}

image-20201223154133077

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//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());
}

image-20201223154230621

image-20201223154651967

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//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));
}

image-20201223154720921

数组引用

格式: type[] :: new

image-20201223154852716

image-20201223155104851

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//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));
}

image-20201223155140177

结论

使用方法,构造器,数组引用必须保证该方法能和某一个对应的函数接口的方法返回值,参数类型保持一致。如果看不懂可以像我一样一步一步推导普通形式----->lambda----->各种引用。不必每个都使用,只要看的懂知道怎么来的就行。