You can’t replace the lambda input -> getValueProvider().apply(input).getValue() with a method reference without changing the semantics.

A method reference replace a single method invocation, so it can’t simply replace a lambda expression consisting of more than one method invocation.

A lambda expression of the form input -> getValueProvider().apply(input) could be replaced by getValueProvider()::apply if, and only if, the evaluation time of getValueProvider() does not matter as in the lambda form the method is invoked on each lambda body evaluation while for the method reference it is invoked only once and the result captured.

This is similar to the difference between x -> System.out.println(x) and System.out::println where reading the contents of the field System.out happens at different times but usually it doesn’t matter. But you should be aware of the difference.

In your example, a third method getValue() is invoked. The only way to express that with method references needs a functional interface like Function which has methods like andThen and/or compose. However, the way Java 8 works, that would require casting the first method reference to the target interface to invoke the combining method which would be by no way easier to read that the lambda expression you have now: ((Function<X,Y>)getValueProvider()::apply).andThen(Y::getValue) where Y is the type, apply(input) returns.

Note that the rule says “Replace lambdas with method references when possible” which gives you room to say, “well, here it is impossible”, however, I’m not sure how much you can call it a “rule” then…

Answer from Holger on Stack Overflow
Top answer
1 of 4
46

You can’t replace the lambda input -> getValueProvider().apply(input).getValue() with a method reference without changing the semantics.

A method reference replace a single method invocation, so it can’t simply replace a lambda expression consisting of more than one method invocation.

A lambda expression of the form input -> getValueProvider().apply(input) could be replaced by getValueProvider()::apply if, and only if, the evaluation time of getValueProvider() does not matter as in the lambda form the method is invoked on each lambda body evaluation while for the method reference it is invoked only once and the result captured.

This is similar to the difference between x -> System.out.println(x) and System.out::println where reading the contents of the field System.out happens at different times but usually it doesn’t matter. But you should be aware of the difference.

In your example, a third method getValue() is invoked. The only way to express that with method references needs a functional interface like Function which has methods like andThen and/or compose. However, the way Java 8 works, that would require casting the first method reference to the target interface to invoke the combining method which would be by no way easier to read that the lambda expression you have now: ((Function<X,Y>)getValueProvider()::apply).andThen(Y::getValue) where Y is the type, apply(input) returns.

Note that the rule says “Replace lambdas with method references when possible” which gives you room to say, “well, here it is impossible”, however, I’m not sure how much you can call it a “rule” then…

2 of 4
10

list.stream().sorted().collect(Collectors.toList()).forEach(element -> operate(element));

replace the above lambda with a method reference.

list.stream().sorted().collect(Collectors.toList()).forEach(this::operate);

🌐
Jsparrow
jsparrow.github.io › rules › lambda-to-method-reference.html
Replace Expression Lambda with Method Reference | jSparrow Documentation
This rule simplifies expression lambdas by using method reference. The rule can only be applied if the parameters of the lambda expression and the method match.
Discussions

java - How to replace a lambda with a method reference - Stack Overflow
How can I replace the lambda with a method reference in my case? ... Since there's only one lambda in your post I'd assume it needs to be something like Node::getName (or whatever the type of node is). ... What IDE are you using? Usually you should get a warning or a hint at which line you should replace the code ... I use Eclipse. the code has no problems and it's functioning as expected but i think the SonarCube has problems with lambdas, this ... More on stackoverflow.com
🌐 stackoverflow.com
[S1612] "Lambdas should be replaced with method references" should not be suggested if it would result in longer code
SonarQube version: Community Edition Version 7.9.2 (build 30863) What steps will reproduce the issue? Consider this example class: public static class SomeVeryLongAndComplicatedClassName { public void doThing() { Collections.singletonList("test").forEach(s -> print(s)); } public static void ... More on community.sonarsource.com
🌐 community.sonarsource.com
1
1
March 6, 2020
lambda x method reference taste
I always do method references if possible. The benefit is that it is immediately clear that the call is to a method with only 1 parameter and the argument is an element of the collection that is streamed over. It's not necessarily the case with a lambda. Method references leave less room for errors or oversights, such as negation. You also don't have to invent intermediate variable names. More on reddit.com
🌐 r/java
35
18
January 21, 2022
Suggestion to replace lambda with method reference causes invalid code (java:S1612)
Rule: Lambdas should be replaced with method references (java:S1612) Applying suggestion to replace lambda on Integer conversion to String with method reference causes invalid code. Original code: Collection … More on community.sonarsource.com
🌐 community.sonarsource.com
1
0
August 26, 2022
🌐
Blogger
javarevisited.blogspot.com › 2017 › 08 › how-to-convert-lambda-expression-to-method-reference-in-java8-example.html
How to Convert a Lambda Expression to Method Reference in Java 8?
If you like this article then please share with your friends and colleagues. If you have any question or feedback then please drop a comment. P. S. - If you don't mind learning from free resources then you can also check out this list of free Java 8 and Java 9 courses to learn better. ... Tomtom said... Hi, SonarLint helps finding where a lambda expression can be replaced by method reference.
🌐
JetBrains
jetbrains.com › help › inspectopedia › MethodRefCanBeReplacedWithLambda.html
Method reference can be replaced with lambda | Inspectopedia Documentation
Reports method references, like MyClass::myMethod and myObject::myMethod, and suggests replacing them with an equivalent lambda expression.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
How to convert a lambda expression to method reference in Java 8? - Java Code Geeks
August 14, 2017 - That’s it, but I know, it’s ... in Java 8. If you are using a lambda expression as an anonymous function but not doing anything with the argument passed, you can replace lambda expression with method reference....
🌐
JetBrains
jetbrains.com › help › inspectopedia › Convert2MethodRef.html
Lambda can be replaced with method reference | Inspectopedia Documentation
3 weeks ago - The inspection may suggest method references even if a lambda doesn't call any method, like replacing obj -> obj != null with Objects::nonNull. Use the Settings | Editor | Code Style | Java | Code Generation settings to configure special method references. This inspection depends on the following Java features:
🌐
Baeldung
baeldung.com › home › java › core java › method references in java
Method References in Java | Baeldung
March 26, 2025 - As it is a varargs method, it will work in any lambda expression, no matter the referenced object or number of parameters inferred. ... In this quick tutorial, we learned what method references are in Java and how to use them to replace lambda expressions, thereby improving readability and ...
Find elsewhere
🌐
Sonar Community
community.sonarsource.com › rules and languages › report false-positive / false-negative...
[S1612] "Lambdas should be replaced with method references" should not be suggested if it would result in longer code - Report False-positive / False-negative... - Sonar Community
March 6, 2020 - SonarQube version: Community Edition Version 7.9.2 (build 30863) What steps will reproduce the issue? Consider this example class: public static class SomeVeryLongAndComplicatedClassName { public void doThing() { Collections.singletonList("test").forEach(s -> print(s)); } public static void print(String str) { System.out.println(str); } } Currently, SonarQube suggests replacing s -> print(s) with a method reference, which would re...
🌐
OpenRewrite
docs.openrewrite.org › recipe catalog › static analysis and remediation › use method references in lambda
Use method references in lambda | OpenRewrite Docs
ReplaceLambdaWithMethodReferenceTest#toQualifiedMethodReference ... @@ -3,1 +3,1 @@ fun main() { val pets = listOf(Cat(), Dog()) - pets.forEach { it.move() } + pets.forEach ( Pet::move ) } interface Pet { fun move() { } } class Cat : Pet { override fun move() { println("Cat is moving") } } class Dog : Pet { override fun move() { println("Dog is moving") } } This recipe has no required configuration options. It can be activated by adding a dependency on org.openrewrite.recipe:rewrite-static-analysis in your build file or by running a shell command (in which case no build changes are needed):
Published   January 28, 2026
🌐
Reddit
reddit.com › r/java › lambda x method reference taste
r/java on Reddit: lambda x method reference taste
January 21, 2022 -

Hello guys, I'm jr web back-end and during my "trainee introduction" one of my seniors and my job's SonarQube forced me to switch lambda expressions for method reference. I've searched a bit about this change wondering if all of this is about performance, or patterns or code reduction but I didn't find something solid, I know it's probably has been discussed here, but I want more opinions about. For me, lambda expression is way better to read and code, please don't judge me :D

🌐
Medium
medium.com › @edouard.kaiser › lambda-and-method-reference-133867e19c01
Lambda and method reference. Java 8 brought us the lambda… | by Edouard Kaiser | Medium
February 26, 2018 - The method toUpperCase of the String object, gave as a lambda in this example, does not respect the signature of the functional interface (toUpperCase doesn’t expect any parameter where the apply method is expecting one). But it’s working here, because we referenced the method by using the arbitrary object of a particular type syntax (ClassName::instanceMethod). As said earlier, with this syntax, the compiler expects the first argument of the functional interface to be the same type as the class providing the instance method.
🌐
Reddit
reddit.com › r/java › subtle difference between lambda and method reference
r/java on Reddit: Subtle difference between lambda and method reference
September 23, 2022 -

So. Turns out there's a subtle difference between lambdas and method references. For instance, compare: myInstance::getStuff and () -> myInstance.getStuff()

This will mostly be considered equivalent. But. If myInstance happens to be null, the lambda will throw a null pointer when the lambda gets evaluated, but the method reference will throw right away when trying to access the reference.

So what? Well. This IS important if the code evaluating the lambda is inside a null-pointer try-catch.

Say I have a function mightBeNull(Supplier<T> function) that does something along the lines of:

try {
    doStuff(function.get().getSomeMore().getSomeMore());
} catch (NullPointerException e) {
    doOtherStuff();
}

If so. The call: mightBeNull(() -> myNullVariable.getStuff()) will work without exceptions, but the "equivalent": mightBeNull(myNullVariable::getStuff) will throw a null pointer exception right att the function call.

🌐
Stack Overflow
stackoverflow.com › questions › 71767693 › replace-lambda-expression-with-method-reference
java - Replace lambda expression with method Reference - Stack Overflow
How can I replace? ... Or just let your IDE do the conversion. ... @WJS Method references are commonly agreed to be, most of the time, more compact and readable than using lambdas, and are therefore preferred.