The method reference is divided into 4 classes, and the method reference is also limited by access control permissions, which can be judged by whether the referenced method is invoked at the reference location. The specific classification information is as follows:
| type |
How to use |
| Static methods |
Containingclass::staticmethodname |
| Methods for specifying instances |
Containingobject::instancemethodname |
| Any method of a particular class instance |
Containingtype::methodname |
| Construction method |
Classname::new |
-
referencing static methods
Containingclass::staticmethodname
Example: string::valueof, corresponding Lambda: (s), string.valueof (s)
Relatively easy to understand, compared to static method calls, just put. Change to::
-
referencing an instance method of a specific object
Containingobject::instancemethodname
Example: x::tostring, corresponding lambda: ()-this.tostring ()
In contrast to a static method, it is used as an instance.
-
An instance method that references any object of a particular type
Containingtype::methodname
Example: string::tostring, corresponding Lambda: (s), s.tostring ()
It's too hard to understand. Things that are difficult to understand and difficult to maintain. It is recommended that you do not refer to this method.
An instance method is called by an object, and the method references the first parameter of the corresponding LAMBDA,LAMBDA to be the object that invokes the instance method.
-
Reference Constructors
Classname::new
Example: String::new, corresponding lambda: ()-New String ()
Constructors are static methods in nature, but the method names are more specific.
An example:
Packagemethodreference;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.List;ImportJava.util.function.Supplier;/** * @authorJiangxiulian * @create 2017-12-28 pm 12:03 **/ Public classMethodreference { Public Static voidMain (String args[]) {//Reference ConstructorsPersonfactory factory =NewPersonfactory (Person::New); //personfactory personfactory = new Personfactory (()->new person ());List<person> personlist =NewArraylist<person>(); Person P1=Factory.getperson (); P1.setname ("Kobe"); Personlist.add (p1); Person P2=Factory.getperson (); P2.setname ("James"); Personlist.add (p2); Person P3=Factory.getperson (); P3.setname ("Paul."); Personlist.add (p3); Person[] persons1= Personlist.toarray (Newperson[personlist.size ()]); System.out.print ("Before sorting"); PrintArray (PERSONS1); System.out.println (); //referencing static methodsArrays.sort (Persons1,methodreference::mycompare); System.out.print ("After sorting"); PrintArray (PERSONS1); System.out.println (); Person[] Persons2= Personlist.toarray (Newperson[personlist.size ()]); System.out.print ("Before sorting"); PrintArray (PERSONS2); System.out.println (); //referencing an instance method of a specific objectArrays.sort (Persons2,p1::compare); System.out.print ("After sorting"); PrintArray (PERSONS2); System.out.println (); Person[] Persons3= Personlist.toarray (Newperson[personlist.size ()]); System.out.print ("Before sorting"); PrintArray (PERSONS3); //An instance method that references any object of a particular typeArrays.sort (Persons3,person::compareto); System.out.print ("After sorting"); PrintArray (PERSONS3); } Public Static voidPrintArray (person[] persons) { for(person p:persons) {System.out.print (P.name+ " "); } System.out.println (); } Public Static intMycompare (person P1, person p2) {returnp1.getname (). CompareTo (P2.getname ()); } Static classperson{PrivateString name; PublicPerson () {} PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intCompare (person P1,person p2) {returnp1.getname (). CompareTo (P2.getname ()); } Public intcompareTo (person p) {return This. GetName (). CompareTo (P.getname ()); } } Static classpersonfactory{PrivateSupplier<person>supplier; PublicPersonfactory (supplier<person>supplier) { This. supplier =supplier; } PublicPerson Getperson () {returnSupplier.get (); } }}
Java 8Lambda Method Reference (Methods References)