Method References)
Methods in Java 8 are also an object that can be referenced By a name.However, the only purpose of method reference is to support the shorthand for Lambda.The method name is used to represent Lambda. It cannot be referenced by methods or information such as method signature.
Category of method reference
There are four types of method references. The first two methods are commonly used. Method reference is also restricted by access control permissions. You can determine whether the referenced method can be called at the reference location. The specific category information is as follows:
Reference static methods
ContainingClass: staticMethodName
Example: String: valueOf, corresponding Lambda :( s)-> String. valueOf (s)
It is easier to understand. Compared with static method calls, it only replaces. ::
Instance method for referencing a specific object
ContainingObject: instanceMethodName
Example: x: toString, corresponding Lambda :()-> this. toString ()
Compared with the reference of static methods, they are all replaced by instances.
Instance method for referencing any object of a specific type
ContainingType: methodName
Example: String: toString, corresponding Lambda :( s)-> s. toString ()
It's hard to understand. Difficult to understand and maintain. We recommend that you do not reference this method.
The instance method must be called through an object. The method reference corresponds to Lambda. The first parameter of Lambda becomes the object for calling the instance method.
Reference Constructor
ClassName: new
Example: String: new, corresponding Lambda :()-> new String ()
Constructors are essentially static methods, but their names are special.
Example of method reference
Public class MethodReference {public static void main (String [] args) {// method reference: Reference constructor PersonFactory factory = new PersonFactory (Person: new); List
PersonList = new ArrayList
(); 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 (new Person [personList. size ()]); System. out. print (Before sorting:); printArray (persons1); // method reference: Reference static method Arrays. sort (persons1, MethodReference: myCompare); System. out. print (sorted:); printArray (persons1); System. out. println (); Person [] persons2 = personList. toArray (new Person [personList. size ()]); System. out. print (Before sorting:); printArray (persons2); // method reference: Arrays. sort (persons2, p1: compare); System. out. print (sorted:); printArray (persons2); System. out. println (); Person [] persons3 = personList. toArray (new Person [personList. size ()]); System. out. print (Before sorting:); printArray (persons3); // method reference: Arrays. sort (persons3, Person: compareTo); System. out. print (sorted:); printArray (persons3);} public static void printArray (Person [] persons) {for (Person p: persons) {System. out. print (p. name +);} System. out. println ();} public static int myCompare (Person p1, Person p2) {return p1.getName (). compareTo (p2.getName ();} static class Person {private String name; public Person () {} public String getName () {return name;} public void setName (String name) {this. name = name;} public int compare (Person p1, Person p2) {return p1.getName (). compareTo (p2.getName ();} public int compareTo (Person p) {return this. getName (). compareTo (p. getName () ;}} static class PersonFactory {private Supplier
Supplier; public PersonFactory (Supplier
Supplier) {this. supplier = supplier;} public Person getPerson () {return supplier. get ();}}}
Conclusion
Method reference is only a complementary service of Lambda. The main purpose is to obtain Lambda by name and reuse existing methods.