Does Java 8 Method references - can used only in consumer Functional Interfaces

Siva Tharun :

I have been using Java 8 method references for quite some time, but i have this question in my mind.

i know that method references is a short hand notation for a lambda expression,which calls a single method (the method may be a static one or a constructor or method belonging to a instance object).

does this mean, method references can used only as a substitute for lambda of consumer functional interfaces?

For example

Consumer<String> c = s -> System.out.println(s); 

can be rewritten as

 Consumer<String> c = System.out::println;

And

Consumer<T> c=(args) -> Class.staticMethod(args)

can be re written as

Class::staticMethod
shmosel :

does this mean, method references can used only as a substitute for lambda of consumer functional interfaces?

No. Method references can be used in place of any lambda expression, as long as the signatures match. For example:

Function<Object, String> toString = Object::toString;
Function<Object, String> valueOf = String::valueOf;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=99952&siteId=1