DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Using Render Log Streams to Log to Papertrail
  • What Is JHipster?
  • Integration Architecture Guiding Principles, A Reference
  • Data Freshness: Definition, Alerts To Use, and Other Best Practices

Trending

  • Using Render Log Streams to Log to Papertrail
  • What Is JHipster?
  • Integration Architecture Guiding Principles, A Reference
  • Data Freshness: Definition, Alerts To Use, and Other Best Practices
  1. DZone
  2. Data Engineering
  3. Data
  4. Method References Cannot Always Replace Lambda Expressions

Method References Cannot Always Replace Lambda Expressions

Make sure you keep your types, arguments, and signatures straight. While method references can sometimes replace lambdas, they can't always make them work.

Steven Schwenke user avatar by
Steven Schwenke
·
May. 16, 17 · Tutorial
Like (5)
Save
Tweet
Share
13.84K Views

Join the DZone community and get the full member experience.

Join For Free

A couple of weeks ago, I gave one 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, and 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. After having thought for a while, 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:

private int methodWithoutParameter() {
    return 0;
}

/**
* Equals exactly method signature of java.util.function.Function:apply
*/
private int methodWithParameter(int x) {
    return x;
}


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.

@Test
public void methodReferencesCannotAlwaysReplaceLambdas() {

    Map<Integer, Integer> myMap = new HashMap<>();

    // Both lambda and method reference can be used to implement Function, if the method signature equals
    // signature of java.util.function.Function::apply:
    Function<Integer, Integer> functionWithParameter1 = (k) -> methodWithParameter(k);
    Function<Integer, Integer> functionWithParameter2 = this::methodWithParameter;
    myMap.computeIfAbsent(2, functionWithParameter1);
    myMap.computeIfAbsent(2, functionWithParameter2);
    functionWithParameter1.apply(5);

    // Only lambda can be used to implement Function if the method signature differs from the right signature:
    Function<Integer, Integer> functionWithoutParameter1 = k -> methodWithoutParameter();
    Function<Integer, Integer> functionWithoutParameter2 = this::methodWithoutParameter; // not possible
    myMap.computeIfAbsent(2, functionWithoutParameter1);
    functionWithoutParameter1.apply(5);
}


So, what can be expressed with a lamba cannot be necessarily expressed with a method reference.

Data Types Awesome (window manager) Insight (email client) Java (programming language) Testing IT Document

Published at DZone with permission of Steven Schwenke. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Using Render Log Streams to Log to Papertrail
  • What Is JHipster?
  • Integration Architecture Guiding Principles, A Reference
  • Data Freshness: Definition, Alerts To Use, and Other Best Practices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: