44

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

Set<String> set = people.stream()
                        .map(Person::getName)
                        .collect(Collectors.toCollection(TreeSet::new));
Eran
  • 387,369
  • 54
  • 702
  • 768
sixfeet
  • 934
  • 4
  • 16
  • 33

3 Answers3

68

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.

Eran
  • 387,369
  • 54
  • 702
  • 768
37

:: 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) {
        }
    }
}

So, In your example:

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());
    }
}
jbutler483
  • 24,074
  • 9
  • 92
  • 145
29

Person::getName in this context is shorthand for (Person p) -> p.getName()

See more examples and a detailed explanations in JLS section 15.13

Misha
  • 27,433
  • 6
  • 62
  • 78