Purpose of Reference to a Constructor : Java 8 -


i going through documentation of method references in java8

in encountered table kinds of method reference , out of 4 last 1

----------------------------------------------- reference constructor :    classname::new  ----------------------------------------------- 

you can reference constructor in same way static method using name new.

i unable understand reference constructor do ?

note : know java documentation self explanatory in case i'm failing understand wants convey .

a constructor reference can used whenever call method requires supplier (i.e. has parameter type supplier<t>). constructor of class t can serve supplier<t>.

so, example, consider example taken javadoc of collectors class:

set<string> set =      people.stream()           .map(person::getname)           .collect(collectors.tocollection(treeset::new)); 

collectors.tocollection requires supplier. when pass constructor of treeset - treeset::new - use create treeset instance contain elements of stream. if pass hashset::new, create hashset instance instead of treeset.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -