Reference to an instance method using method references in Java8
https://www.tutorialspoint.com/reference-to-an-instance-method-using-method-references-in-java8
-
13-09-2020 - |
题
Reference to an instance method using method references in Java8
Lambda expressions In Java allows you to pass functionality as an argument to a method. You can also call an existing method using lambda expressions.
list.forEach(n -> System.out.println(n));
Method references are simple, easy-to-read lambda expressions to call/refer and existing method by name in a lambda expression.
Syntax
Following is the syntax to reference a instance method in Java
Object:methodName
Example
Following Java example references a instance method in Java.
interface myInterface{ void greet(); } public class MethodReferences { public static void demo() { System.out.println("Sample method"); } public static void main(String args[]) { MethodReferences obj = new MethodReferences(); myInterface in = obj::demo; in.greet(); } }
Output
Sample method
Advertisements
不隶属于 Tutorialspoint