Java8 - Learn Constructor Reference tutorials with examples
In my previous post, we learned java8 method references with examples. In this blog post, We are going to learn the Constructor Reference tutorials with examples.
What is Constructor Reference in java?
Constructor references are to reference a class with an object created using class name and new keyword. for Like Method references, references to Constructor can be created to do object initialization.
method references
use method name, Constructor references use the new keyword and class name. Here is a Syntax
ClassName :: new
Constructor Reference Example
Here is an example for reference of String class name. String constructor is called using the new operator
Supplier<String> stringSupplier = String::new
Constructor reference with arguments example
First, In the below example, Create a Student class with name and id parameters.
class Student {
String name;
Integer id;
// Student Contructor
public Student(String name, Integer id) {
this.name = name;
this.id = id;
}
}
the second step is to create a Factory Singleton class for Student class.
interface StudentFactory {
public abstract Student getStudent(String name, Integer id);
}
StudentFactory is a Functional interface with the single abstract method- getStudent StudentFactory.getStudent() returns Student object.
Next is to create a Student object using constructor reference and supplying two parameters/arguments.
StudentFactory studentFactory = Student::new;
Student student = studentFactory.getStudent("John", 11);
System.out.println(student);
Constructor reference of Student Object is created using a new operator and StudentFactory reference. getStudent() method of StudentFactory is called with arguments, which internally executes the constructor of Student class.
BiFunction Constructor reference with parameters example
It is another way of referencing a constructor with a parameter. BiFunction is a functional interface with single abstract method apply - which accepts two parameters as arguments and produces output as a result.
BiFunction<String,Integer,Student> function = Student::new;
Student student=function.apply("Kiran",12);
System.out.println(student);
Constructor reference array example
as of now, we have only seen Constructor references with instance creation, We can also create an array using reference
<A> A[] toArray(IntFunction<A[]> generator)
toArray() is a terminal Operation that takes elements from String and converts them into an array of elements.
It accepts an array of IntFunction. IntFunction is a functional interface with the single abstract method, which takes int values and output results.
First, Create a User Object with the default constructor.
class User {
private String name;
public User() {
}
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Create a List of String names, We mapped this list of strings to User Instance using the one-argument constructor with a lambda expression.
List<String> usernames = Arrays.asList("Kiran", "Franc", "Tom", "John");
List<User> users = usernames.stream().map(name -> new User(name)) // lambda expression
.collect(Collectors.toList());
We replaced lambda expression with constructor reference as follows using one argument constructor. Created Stream for an array of objects, the Map method is called on each string of a stream array. Constructor reference - User:: new is created for each string of array, It calls the User constructor with a parameter.
List<User> users1 = usernames.stream().map(User::new) // Constructor reference
.collect(Collectors.toList());
Now we will see the constructor reference array example. the stream of strings is passed to the map method with constructor reference and finally called toArray() which returns an Array of elements from a stream.
User[] users2 = usernames.stream().map(User::new) // constructor reference for User object
.toArray(User[]::new); // constructor reference for User array object