构造器引用
格式: 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); } }
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
@Test public void test1() { Supplier<Employee> s1 = new Supplier<Employee>() { @Override public Employee get() { return new Employee(); } }; System.out.println(s1.get());
Supplier<Employee> s2 = () -> new Employee(); System.out.println(s2.get());
Supplier<Employee> s3 = Employee::new; System.out.println(s3.get()); }
|


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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
@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));
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----->各种引用
。不必每个都使用,只要看的懂知道怎么来的就行。