Java Stream How to - Use constructor reference in method reference








Question

We would like to know how to use constructor reference in method reference.

Answer

/*  w ww  .j av a  2 s  .  c om*/
public class Main {
  public static void main(String... args) {
    // 
    StudentCreator<Student> personFactory = Student::new;
    Student person = personFactory.create("First", "Last");
  }
}
interface StudentCreator<P extends Student> {
  P create(String firstName, String lastName);
}
class Student {
  String firstName;
  String lastName;

  Student() {}

  Student(String firstName, String lastName) {
      this.firstName = firstName;
      this.lastName = lastName;
  }
}