The Ultimate Guide to Lambda Expressions in Java

Last modified: April 19, 2018

Lambda expressions are functions that can be passed around as arguments. Using lambdas, you can create "anonymous methods" that implement functional interfaces with a more concise syntax. Lambdas have become increasingly popular with the movement towards more functional programming.

In this tutorial, we'll cover everything you need to know about lambdas. We'll explain what lambdas are, how they originated, and how they improve upon things like anonymous classes and functional interfaces. We'll explore best use cases and provide examples of using lambda expressions in various contexts.

What are lambdas in Java?

Lambda expressions are functions as arguments. They provide a more convenient syntax for implementing a functional interface as demonstrated below:

interface  Print {
    void print(String message);
}

public class MainClass {
    public static void main(String args[]) {
        Print myPrinter = x -> System.out.println(x);
        myPrinter.print("Hello lambdas!");
    }
}

Notice how we first define an interface Print that defines exactly one abstract method. Any interface that defines exactly one abstract method is a functional interface.

We use a lambda expression x -> System.out.println(x) to implement this single abstract method in a more concise way.

Lambdas provide a clear and concise way of representing a functional interface. Lambdas can also be considered "anonymous functions" and can be passed around as arguments to other functions:

interface  MathOp {
    int operate(int a, int b);
}

public class MainClass {
    public static void printResults(int x) {
        System.out.println(x);
    }

    public static void main(String args[]) {
        MathOp add = (a, b) -> {
            return a + b;
        };
        printResults(add.operate(2, 2));
        //prints 4
    }
}

Notice how we first define a functional interface MathOp that defines a single operate method returning an integer.

We define a lambda expression add which implements the MathOp interface. Notice how this expression takes two parameters (a, b) and returns the added result a + b.

We can then pass our lambda expression add into our separate printResults() method. We can do this because our lambda expression evaluates to the target data type int accepted by the printResults() method.

Lambda Syntax

Lambda expressions consist of three entities:

(a, b) -> {}

1) Argument List (a,b)

The argument list is a comma-separated list of parameters enclosed in parentheses. These arguments usually don't require type declarations as their types can be inferred by the compiler. The parentheses () are optional if you only have a single argument.

2) Arrow token ->

This is the syntax for lambda expressions and signifies the passing of the parameters to the body.

3) Body {}

The body is a code block that represents the implementation of a functional interface. If the body is a single statement, the expression will evaluate and return the result. In this case, the brackets {} are optional. If the body is more than a single statement, you must use brackets and return the result.

Why use lambdas in Java?

A more concise syntax

Lambda expressions allow you to implement a functional interface with fewer lines of code than an anonymous class. Working off our previous example, let's also implement the same Print interface using a nested class:

interface  Print {
    void print(String message);
}

public class MainClass {
    public static void main(String args[]) {
        //using nested class
        Print myNestedPrinter = new Print(){
            public void print(String x) {
                System.out.println(x);
            }
        };
        //using lambda
        Print myPrinter = x -> System.out.println(x);

        myNestedPrinter.print("Hello nested classes");
        myPrinter.print("Hello lambdas!");
    }
}

This example demonstrates the benefit of using a lambda over an anonymous class. With the anonymous class, we have to explicitly create a new instance of the Print interface and implement its print method. This takes 4 more lines of code than using using a lambda expression.

Lambdas make it easier to work with collections

Lambdas provide a much more elegant way of working with collections. For example:

import java.util.*;
public class MainClass {
    public static void main(String args[]) {
        List<Character> list = new ArrayList<Character>();
        list.add('a');
        list.add('b');
        list.add('c');
        list.forEach(x -> System.out.println(x));
    }
}

Using the forEach method, we define a lambda expression x -> System.out.println(x) that is applied to every element in the collection.

This is considered an internal iteration and can have some performance implications when used with the Streams API. More specifically, parallel processing can be easily implemented with internal iterations as they offload the management of the iteration to a process. This is different from external iterations which fully manage how the iteration is implemented.

While the differences between internal and external iterators are outside the scope of this article, Dhruv Rai Puri's Java 8 Internal Iterators vs External Iterators explains these differences in greater detail.

Method References

When a lambda expression simply calls an existing method you can use a method reference instead.

interface Print {
    void print(String msg);
}

public class MainClass {
    public static void systemPrint(String x) {
        System.out.println(x);
    }

    public static void main(String args[]) {
        Print lambdaPrint = x -> System.out.println(x);
        Print methodPrint = MainClass::systemPrint;
        Print objectPrint = System.out::println;

        lambdaPrint.print("printing with a lambda expression!");
        //prints printing with a lambda expression!
        methodPrint.print("printing with a static method reference");
        //prints printing with a static method reference
        objectPrint.print("printing with an object method reference");
        //prints printing with an object method reference
    }
}

In our example above, notice how lambdaPrint is set to a lambda expression x -> System.out.println(x). This expression is equivalent to the method reference MainClass::systemPrint used on the next line. This is considered a reference to a static method

We also reference an instance method of a particular object on the next line System.out::println. It turns out there are four different kinds of method references:

  • reference to a static method
  • reference to an instance method of a particular object
  • reference to an instance method of an arbitrary object of a particular type
  • reference to a constructor

The general syntax for a method reference is:

Object :: method

With a method reference, you generally don't need to worry about passing arguments. In the above example, the compiler can infer the arguments based on the static method definition for systemPrint().

While the different types of method references have a similar syntax, there are some subtle differences in how they handle arguments, etc. If you want to dig further, Esteban Herrera does a great job of explaining these differences in his article Java 8 Method Reference: How to Use It.

The java.util.function package

Lambda expressions implement functional interfaces. You can't use a lambda expression without referencing an existing functional interface. This is why all of the examples you see in this tutorial first define a functional interface.

It can't get rather tedious creating functional interfaces for every method you want to implement as a lambda expression. For these reasons, Java provides a set of reusable, generic, functional interfaces that are ready for use out of the box:

import java.util.function.Consumer;
public class MainClass {
    public static void main(String args[]) {
        String message = "Hello world!";
        Consumer<String> myConsumer = x -> System.out.println(x);
        myConsumer.accept(message);
        //prints Hello world!
    }
}

In this example, notice how we first import the java.util.function.Consumer interface. The Consumer is a generic interface that accepts one argument and returns no result. This is perfect for situations where you want to log a message because you aren't returning a value but performing an action on a single message argument.

While we could have defined our own interface to achieve the same results, we save ourself time by using a predefined functional interface in the java.util.function package.

Notice how we call the accept() method as defined in the Consumer interface.

Before creating your own functional interface, be sure to check the java.util.function package for existing generic functional interfaces you can use in your code.

Lambdas are lexically scoped

Lambdas are lexically scoped, meaning they don't inherit any names from supertypes or introduce new level of scoping. For these reasons, lambdas don't have the same shadowing issues experienced with nested classes. Baeldung does a great job of explaining this concept in his Lambda Tips and Best Practices article.

Remember that lambdas can access any local variables in their enclosing scope as long as they are final or effectively final. This is a fancy term for saying a value isn't reassigned:

interface Print {
    void print();
}

public class MainClass {
    public static void main(String args[]) {
        String message = "Hello world!";
        message = "new message";
        Print myPrinter = () -> {
            System.out.println(message);
            //compile time error as message has been reassigned
        };
        myPrinter.print();
    }
}

This example results in a compile time error because we reassign the message variable. If we hadn't reassigned message, we could reference the variable directly in our lambda expression as it would be considered effectively final.

Conclusion

Lambdas are key to achieving a more functional approach to programming in Java. They provide a more elegant way of implementing functional interfaces and allow you to pass around anonymous methods as regular arguments.

Still confused? Check out Luis Santiago's intro to working with Lambda expressions in Java. This provides a high level introduction with some good examples for getting started.

If you are craving more, check out Lambda Expressions in Java 8 and Lambda and Streams in Java 8. These are a bit dated but are written by reputable authors who know what they are talking about.

You might also like: