46

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

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

3 Answers 3

69

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.

Sign up to request clarification or add additional context in comments.

4 Comments

+1, what will they think of next? String switching perhaps... Interfaces with methods...
@Bathsheba, I'm crossing my fingers for operator overloading.
@ChiefTwoPencils Especially if they also define , as an operator.
@Bathsheba Default and Static methods are available in Interfaces since 1.8
39

:: 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());
    }
}

Comments

30

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

See more examples and a detailed explanations in JLS section 15.13

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.