Sunday, 10 April 2016

Meaning of :: in Java syntax





What is the meaning of :: in the following code?



Set set = people.stream()
.map(Person::getName)
.collect(Collectors.toCollection(TreeSet::new));


Answer



This is method reference. Added in Java 8.



TreeSet::new refers to the default constructor of TreeSet.



In general A::B refers to method B in class A.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...