BrilworksarrowBlogarrowProduct Engineering

Java Lambda Expression: What is it? With an example

Colin Shah
Colin Shah
November 3, 2023
Clock icon2 mins read
Calendar iconLast updated August 26, 2025
Banner-Lambda expression
Quick Summary:- Lambda expressions, or Lambda expressions in Java, were added to Java 8. They were a significant addition to the language. In this article, we will explore why they were introduced and what their purpose is.

Lambda expressions in Java are a straightforward way to define functionality, especially when working with functional interfaces. They were introduced to simplify code and bring functional programming concepts into the Java ecosystem. Instead of writing verbose anonymous classes, developers can now express behavior in a more readable form.

What is a Lambda Expression in Java?

A lambda expression in Java is a block of code that you can pass around, similar to a method, but without the boilerplate of defining a full class or method. It represents an implementation of a functional interface (an interface with exactly one abstract method).

In simpler terms, a lambda lets you treat functionality as data, you can define it once and use it wherever it’s needed.

Basic Syntax:

(parameters) -> expression
Or, if you need multiple lines:

(parameters) -> {

    // body of code

    return result;

}

Why Were Lambdas Introduced in Java?

Before lambdas, Java developers relied on anonymous classes for passing behavior, especially in collections or event handling. While functional, anonymous classes were often verbose. For instance, iterating through a list required boilerplate that distracted from the actual logic.

Lambda expressions were introduced to solve this problem by:

  1. Reducing verbosity.

  2. Making code more readable.

  3. Enabling functional programming patterns alongside object-oriented ones.

This shift aligned Java more closely with modern programming trends, where clean, expressive code is highly valued.

Example of a Lambda Expression in Java

Let’s compare an anonymous class with a lambda expression.

1. Using Anonymous Class

List<String> names = Arrays.asList("Anubhav", "Riya", "Karan");

Collections.sort(names, new Comparator<String>() {

    @Override

    public int compare(String a, String b) {

        return a.compareTo(b);

    }

});

2. Using Lambda Expression

List<String> names = Arrays.asList("Anubhav", "Riya", "Karan");

Collections.sort(names, (a, b) -> a.compareTo(b));

The lambda version is shorter and communicates intent more clearly.

Functional Interfaces and Lambdas

Lambdas work with functional interfaces that define exactly one abstract method. Common examples include:

  1. Runnable (method: run)

  2. Callable (method: call)

  3. Comparator<T> (method: compare)

  4. Custom user-defined interfaces with one method

Example with Runnable

Runnable task = () -> System.out.println("Task is running...");

task.run();

Here, the lambda directly provides the implementation for the run() method.

Use Cases of Lambda Expressions

1. Iterating Collections

List<String> list = Arrays.asList("Apple", "Banana", "Mango");

list.forEach(item -> System.out.println(item));

2. Filtering with Streams

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);

numbers.stream()

       .filter(n -> n % 2 == 0)

       .forEach(System.out::println);

3. Event Handling

button.setOnAction(event -> System.out.println("Button clicked!"));

These examples highlight how lambdas eliminate boilerplate while keeping the focus on business logic.

Advantages of Lambda Expressions in Java

  1. Cleaner Code: Eliminates the need for unnecessary class definitions.

  2. Improved Readability: Shorter syntax makes logic easier to follow.

  3. Functional Programming Support: Brings Java closer to modern paradigms like map, filter, and reduce.

  4. Better Parallelism: Works seamlessly with streams for parallel processing.

  5. Flexibility: Behavior can be treated as a value and passed around.

Limitations of Lambda Expressions

While powerful, lambdas are not a perfect solution for every problem:

  1. Debugging Challenges: Stack traces may be less informative compared to named classes.

  2. Overuse Risks: Using lambdas in deeply nested logic can hurt readability.

  3. Limited Reusability: A lambda is tied to a single-use context unless refactored into a method reference.

  4. Learning Curve: Developers new to functional programming might find them confusing at first.

Lambda Expressions vs Anonymous Classes

Although they may look interchangeable, lambdas and anonymous classes are not identical.

  1. Type Binding: A lambda’s type is determined by the functional interface it’s assigned to. Anonymous classes create a new class every time.

  2. this Keyword: Inside a lambda, this refers to the enclosing class. Inside an anonymous class, it refers to the anonymous class itself.

  3. Performance: Lambdas are more lightweight compared to anonymous class instances.

Method References: A Cleaner Alternative

Java also provides method references, which are even shorter than lambdas when an existing method matches the functional interface.

Example:

List<String> names = Arrays.asList("Anubhav", "Riya", "Karan");

names.forEach(System.out::println);

Here, System.out::println is a method reference that achieves the same result as a lambda.

Conclusion

Lambda expressions in Java mark a turning point in how developers write code. They strip away verbosity, encourage functional programming practices, and make everyday tasks like sorting, filtering, and iteration simpler. While they have some limitations, their advantages outweigh the drawbacks in most cases.

For developers working with modern Java, understanding and effectively using lambda expressions is no longer optional; it’s essential. They represent a mindset shift, one that emphasizes clarity, expressiveness, and efficiency in coding.

FAQ

Lambda expressions are a concise way to define anonymous functions in Java. They provide a lightweight alternative to traditional anonymous inner classes, making code cleaner and more readable.

Lambdas are typically used with functional interfaces. These interfaces have a single abstract method, and lambda expressions provide the implementation for that method. You can pass lambdas as arguments to methods or assign them to variables.

Lambdas offer several advantages. They simplify code by reducing boilerplate associated with anonymous inner classes. They also promote functional programming style, leading to more concise and expressive code, especially when working with collections and streams.

The basic syntax involves parameters (optional parentheses), an arrow (->), and the function body. You can have zero or more parameters, and the body can be a single expression or a block of statements enclosed in curly braces.

Lambdas can only access final or effectively final local variables from the surrounding scope. This ensures predictable behavior and prevents accidental modification of variables within the lambda.

Colin Shah

Colin Shah

As a lead Java developer with 8+ years of experience, I design and develop high-performance web applications using Java, Spring Boot, Hibernate, Microservices, RESTful APIs, AWS, and DevOps. I'm dedicated to sharing knowledge through blogs and tutorials.

Get In Touch

Contact us for your software development requirements

You might also like

Get In Touch

Contact us for your software development requirements