Method references cannot always replace Lambda expressions

Posted by Steven

A couple of weeks ago, I gave on of my "Writing awesome Java code" workshops. While I was explaining ways of designing APIs using lambda expressions, a participant asked a question that I couldn't answer. There was a simple lambda expression in the example code, the question was if it could be replaced with a method reference. I agreed and changed it to a method reference. However, the code didn't compile. That confused me, of course. Only after having thought for a while about this, the answer came to me - and it's pretty simple.  In this article, I want to share my insights.

There are two simple methods, one without any parameters, one with exactly one parameter:

  1. private int methodWithoutParameter() {
  2. return 0;
  3. }
  5. /**
  6.  * Equals exactly method signature of java.util.function.Function:apply
  7.  */
  8. private int methodWithParameter(int x) {
  9. return x;
  10. }

The following test documents that the lambda expression in line 15 cannot be replaced with the method reference in line 16. The reason for this is that the lambda expression is of the same signature than the required function, which needs one Integer parameter "in" and one Integer as a return value. However, the lambda ignores the "in" parameter because methodWithoutParameter() is called without using k at all. That works fine for the lambda. However, the method reference needs the exact signature and doesn't work otherwise.

  1. @Test
  2. public void methodReferencesCannotAlwaysReplaceLambdas() {
  4. Map<Integer, Integer> myMap = new HashMap<>();
  6. // Both lambda and method reference can be used to implement Function, if the method signature equals
  7. // signature of java.util.function.Function::apply:
  8. Function<Integer, Integer> functionWithParameter1 = (k) -> methodWithParameter(k);
  9. Function<Integer, Integer> functionWithParameter2 = this::methodWithParameter;
  10. myMap.computeIfAbsent(2, functionWithParameter1);
  11. myMap.computeIfAbsent(2, functionWithParameter2);
  12. functionWithParameter1.apply(5);
  14. // Only lambda can be used to implement Function if the method signature differs from the right signature:
  15. Function<Integer, Integer> functionWithoutParameter1 = k -> methodWithoutParameter();
  16. Function<Integer, Integer> functionWithoutParameter2 = this::methodWithoutParameter; // not possible
  17. myMap.computeIfAbsent(2, functionWithoutParameter1);
  18. functionWithoutParameter1.apply(5);
  19. }


What can be expressed with a Lamba cannot be necessarily expressed with a method reference



To use the method without parameter with a method reference, you can use the Supplier:

Supplier<Integer> functionWithoutParameter2 = this::methodWithoutParameter; 

You're right, thanks!