- , : , , . - , .
, Person:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age age;
}
}
, BiFunction<String, Integer, Person>, Person:
BiFunction<String, Integer, Person> personCreator =
(String name, Integer age) -> new Person(name, age);
, - :
BiFunction<String, Integer, Person> personCreator = (name, age) -> new Person(name, age);
2-arg :
Person joe = personCreator.apply("Joe", 25);
, (name, age) ? , :
BiFunction<String, Integer, Person> personCreator = Person::new;
, :
Person jane = personCreator.apply("Jane", 23);
This is just to show you that the number of arguments doesn't matter when using method references. All that needs to be matched is the signature of the only abstract method of the functional interface (in this case BiFunction.apply) with the signature of the constructor.
If you want to read method references in more detail, go to the section on method references in the Java tutorial.
source
share