Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double Colon lambda for two levels method invocation

Tags:

java

lambda

Is there a way to use doubleColon to invoke second level method. Example ClassA::getClassBObject::classBMethod in lambda invocation

like image 844
Ravisha Avatar asked Jun 13 '26 05:06

Ravisha


2 Answers

What you are expecting is called chaining. Method references does not support chaining.

Read more about Method References.

like image 51
Aniket Sahrawat Avatar answered Jun 17 '26 02:06

Aniket Sahrawat


You could compose the functions:

((Function<ClassA, ClassB>) ClassA::getClassBObject).andThen(ClassB::classBMethod)

But you're probably better off with just (x, arg) -> x.getClassBObject().classBMethod(arg).

More info on the composition approach: https://stackoverflow.com/a/32838924/2093695

like image 20
Brian McCutchon Avatar answered Jun 17 '26 01:06

Brian McCutchon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!