1 What is a method reference (methods references)
Java 8 adds a familiar but unfamiliar symbol::. You might see this code.
System.out::p rintln
is actually a method reference (methods references). Since Java 8 also takes the method/function as the first input parameter. So you will see Inventory.comparing (apple::getweight); This "strange" code.
2 How to use method references (methods references)
In fact, a method reference is a syntactic sugar of a lambda expression. It is possible to simplify the code by means of a method reference when the lambda refers to only one method references.
(Apple a), a.getweight () = = = Apple::getweight (), Thread.CurrentThread (). DumpStack () = = = Thread.CurrentThread ( )::d umpstack (str, i), str.substring (i) = = = String::substring (String s), System.out.println (s) = = = System.out:: println
3 Types of method references
3.1
static method Reference
Example: Integer::p arseint (Parsetint is a static method)
3.2
Instance method
Example: String::length (Length () is an instance method)
3.3
Instance Object method Reference
Example:
Person p = new person
P::getname (P is an instance object)
This article is from the "Development" blog, make sure to keep this source http://jamesdev.blog.51cto.com/2066624/1858767
Java 8 Methods Reference (method references)