Java 8 Lambda-Reference to a constructor

preview_player
Показать описание
In this video tutorial you will learn how to Reference to a constructor in lambda.
Below is the GitHub link to download source Code:
Рекомендации по теме
Комментарии
Автор

This tutorial not to print sqrt of x, to show how to refer a constructor
f.apply(x) returns new Integer(x)
it's just a simple example, let's think about it takes String employee name as argument instead of x and returns an employee object instead of simple integer

kalyanchakravaty
Автор

ni explanation of Function and f.apply

jatashukla
Автор

What if I use constructor reference with generics? Why does
l = ArrayList<>::new; have error?

But if I use lambda expression, l = ()-> new ArrayList<>();
this is okay. Why?

public class Test{



public static void main(String... args){

Supplier<ArrayList>

l = ()-> new ArrayList(); // good

l = ()-> new ArrayList<>(); // good

l = ()-> new ArrayList<Integer>(); // good

l = ArrayList<Integer>::new; // good

l = ArrayList::new; // good

l = ArrayList<>::new; // Error, why?



}

}

HenryLeu
Автор

why to use Function and f.apply and what is the advantage of this.

sanjayjitu