Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of :: in Java syntax [duplicate]

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

Set<String> set = people.stream()                         .map(Person::getName)                         .collect(Collectors.toCollection(TreeSet::new)); 
like image 467
sixfeet Avatar asked Nov 19 '14 11:11

sixfeet


People also ask

What does :: mean in Java?

The double colon (::) operator, also known as method reference operator in Java, is used to call a method by referring to it with the help of its class directly. They behave exactly as the lambda expressions.

What does :: new mean in Java?

The Java new keyword is used to create an instance of the class. In other words, it instantiates a class by allocating memory for a new object and returning a reference to that memory.

What does class <> mean in Java?

A class — in the context of Java — is a template used to create objects and to define object data types and methods. Classes are categories, and objects are items within each category. All class objects should have the basic class properties.

Why constructors are used in Java?

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes. In Java, a constructor is a block of codes similar to the method.


2 Answers

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.

like image 192
Eran Avatar answered Oct 14 '22 20:10

Eran


:: 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());     } } 
like image 35
jbutler483 Avatar answered Oct 14 '22 20:10

jbutler483