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
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. A Little Lambda Tutorial

A Little Lambda Tutorial

Functional programming, and lambda expressions in particular, might not be ideal for every situation, but they're great tools for simple operations.

Dan Newton user avatar by
Dan Newton
·
Jan. 23, 17 · Tutorial
Like (34)
Save
Tweet
Share
25.98K Views

Join the DZone community and get the full member experience.

Join For Free

Lambda expressions are thought to be one of the biggest features in the release of Java 8, as they allow a more functional approach to Java programming. This is something that wasn’t built into Java in previous versions. Java follows the object-oriented programming paradigm, which uses objects, which can store data in fields and manipulate them. Functional programming, on the other hand, is declarative and uses expressions or declarations instead of statements.

As someone who didn’t particularly like functional programming while at university, I was very skeptical about even trying to use lambda expressions in Java. I didn’t want to relive the pain I had endured from trying to understand what the hell was going on. But I gave it a try and I wrote this little tutorial from what I learned.

Lambda expressions are defined by:

 parameters -> expression body 

 Comparator comparator = (a,b) -> a.compareTo(b);  

It takes in a parameter or parameters and does stuff with them.

Some rules about using Lambda expressions:

  • Type declaration is optional: The compiler can figure out types from the value of the input parameters.
  • Brackets are not needed around a singular parameter: If there is one parameter, then brackets are not needed (but can still be used), although multiple parameters will still require the brackets.
  • Optional curly brackets: If the expression body consists of a single statement, then the curly brackets can be removed.
  • Return keyword is optional: The compiler will return the value of the expression if it contains a single statement. If there are curly brackets around the expression body, then it will require a return statement whether it consists of a single statement or not.

Using a lambda expression first requires a functional interface that contains a method that it will override. The are many classes that are already built into Java that can be used as a functional interface, such as Runnable and Comparator.

@FunctionalInterface
public interface Runnable {
    public abstract void run();
}
@FunctionalInterface
public interface Comparator<T> {
    int compare(T o1, T o2);
  // contains other methods but compare is what needs to be overridden 
}


Before Java 8, there were a few ways to use classes and override their behavior. Option 1 is to make a new class and implement it and its method. Unless it is a piece of code that is going to be used a lot, this can be seen as overkill and could lead to lots of tiny classes that are only used once. Option 2 is to use an Anonymous class and create an instance of the interface in the current code where it is needed and override the method there. For a piece of code that is used once, this makes more sense, as you get the functionality you need without the hassle and clutter of making a new class.

Option 1: Create a new class:

public class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("I have implemented Runnable");
    }

    public static void main(String args[]) {
        MyRunnable runnable = new MyRunnable();
        runnable.run();
    }
}


Option 2: Create an Anonymous class:

Runnable runnable = new Runnable() {
  @Override
  public void run() {
    System.out.println("I have implemented Runnable");
  }
};
runnable.run();


Now we can follow the same path as the Anonymous class, but this time, the implementation of the interface will be done by using a lambda expression.

Runnable runnable = () -> System.out.println("I have implemented Runnable");
runnable.run();


As you can see, this allows this simple implementation to be done in much fewer lines of code. This example, using Runnable, does not require any parameters, as the run() method also has no input parameters.

Lambda expressions can also be passed in as inputs into methods that require a functional interface as one of their parameters.

public class ComparatorExample {
    public static void main(String args[]) {
        List<Integer> list = Arrays.asList(1,2,9,80,50,4,25,7);
        System.out.println("Unsorted list: " + list);
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer a, Integer b) {
                return a.compareTo(b);
            }
        });
        System.out.println("Sorted list: " + list);
    }
}


Following the rules defined earlier in this post, there are a few ways this could be rewritten using lambda expressions, although one is clearly nicer than the rest.

public class LambdaComparatorExample {
    public static void main(String args[]) {
        List<Integer> list = Arrays.asList(1,2,9,80,50,4,25,7);
        System.out.println("Unsorted list: " + list);

        // Display types
        Collections.sort(list, (Integer a, Integer b) -> a.compareTo(b));

        // Display curly brackets + return statement
        Collections.sort(list, (a,b) -> { return a.compareTo(b); });

        // Simplest
        Collections.sort(list, (a,b) -> a.compareTo(b));

        System.out.println("Sorted list: " + list);
    }
}


By now you hopefully have a little understanding into using lambda expressions in Java 8. Currently, I am still skeptical about using them in my code but in writing this post and the examples, I can see why they are useful. The main reason I am starting to like them is that for simple operations, they are clear and concise, making the code much easier to read when quickly scanning through it. Although they might not be as useful once code becomes more complex, for simple operations, such as performing a function on the elements of a list, I would not hesitate to use a little lambda expression.

Bracket (tournament) Java (programming language)

Published at DZone with permission of Dan Newton, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Choosing the Right Framework for Your Project
  • Integrate AWS Secrets Manager in Spring Boot Application
  • 5 Steps for Getting Started in Deep Learning
  • 11 Observability Tools You Should Know

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
  • +1 (919) 678-0300

Let's be friends: