What is the meaning of :: in the following code?
Set<String> set = people.stream()
.map(Person::getName)
.collect(Collectors.toCollection(TreeSet::new));
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
.
::
is called Method Reference. It is basically a reference to a single method. i.e. it refers to an existing method by name.
Method reference using ::
is a convenience operator.
Method reference is one of the features belonging to Java lambda expressions. Method reference can be expressed using the usual lambda expression syntax format using –>
In order to make it more simple ::
operator can be used.
Example:
public class MethodReferenceExample {
void close() {
System.out.println("Close.");
}
public static void main(String[] args) throws Exception {
MethodReferenceExample referenceObj = new MethodReferenceExample();
try (AutoCloseable ac = referenceObj::close) {
}
}
}
Set<String> set = people.stream()
.map(Person::getName)
.collect(Collectors.toCollection(TreeSet::new));
Is calling/creating a 'new' treeset.
A similar example of a Contstructor Reference is:
class Zoo {
private List animalList;
public Zoo(List animalList) {
this.animalList = animalList;
System.out.println("Zoo created.");
}
}
interface ZooFactory {
Zoo getZoo(List animals);
}
public class ConstructorReferenceExample {
public static void main(String[] args) {
//following commented line is lambda expression equivalent
//ZooFactory zooFactory = (List animalList)-> {return new Zoo(animalList);};
ZooFactory zooFactory = Zoo::new;
System.out.println("Ok");
Zoo zoo = zooFactory.getZoo(new ArrayList());
}
}
Person::getName in this context is shorthand for (Person p) -> p.getName()
See more examples and a detailed explanations in JLS section 15.13