The double-colon operator is a method reference in Java, and the method references the format of the class Name:: Method name.
This is only the method name, and the method name is not followed by the parentheses "()". --------> Such a formula does not necessarily call this method. This is generally used as a lambda expression, and lambda has the so-called lazy load, not parentheses that is, to see how the method is invoked.
For example:
An expression:
Person->person.getage ();
can be replaced by
Person::getage
An expression:
(), New hashmap<> ();
can be replaced by
Hashmap::new
This method refers to or is a double-colon operation that corresponds to a parameter type of function<t,r>t that represents the passed-in type and R for the returned type. For example, the expression person--person.getage (), the passed parameter is person, the return value is Peron.getage (), then the method reference Person::getage
Corresponds to the funciton<person,integer> type.
Example code: Capitalize all the strings inside the list<string> and return to the new Arraylist<string>, as in the previous example.
@Testpublic void Converttest () { list<string> collected = new arraylist<> (); Collected.add ("alpha"); Collected.add ("beta"); collected = Collected.stream (). Map (String, String.touppercase ()). Collect (Collectors.tolist ()); SYSTEM.OUT.PRINTLN (collected); }
The following form is programmed after using the double-colon operator:
@Testpublic void Testconvertest () { list<string> collected = new arraylist<> (); Collected.add ("alpha"); Collected.add ("beta"); collected = Collected.stream (). Map (String::touppercase). Collect (Collectors.tocollection (arraylist::new)); SYSTEM.OUT.PRINTLN (collected); }
A method reference to a Java 8 lambda expression:: double-colon operator