Java Lambda Expressions

Last Updated : 4 Apr 2026

Java introduced lambda expressions to make code simpler and shorter, especially when working with collections and functional-style operations.

In this chapter, you will learn about lambda expressions, functional interfaces, and how they help in writing clean and efficient Java code.

What is Lambda Expression?

A lambda expression is a feature introduced in Java 8 that provides a short and clear way to represent the implementation of a functional interface (an interface with a single abstract method).

It is widely used in the collection framework to perform operations like iteration, filtering, and data processing.

With lambda expressions, there is no need to write separate classes or methods for implementation. Instead, you can write the implementation directly using an expression.

Functional Interface

A functional interface is an interface that contains only one abstract method. Lambda expressions are used to provide the implementation of such interfaces.

Java provides the @FunctionalInterface annotation to declare a functional interface.

Function Reference (Method Reference)

A method reference is a shorter way to refer to a method using :: operator. It is used in place of lambda expressions when the method already exists.

Here is the syntax for function reference:

Example:

Here, instead of writing a lambda expression, we directly refer to the println method.

Why use Lambda Expression?

Lambda expressions make Java code shorter, cleaner, and easier to read. They remove the need for extra boilerplate code, especially when implementing functional interfaces.

They are very useful when working with collections and streams to write operations like filtering and iteration in a simple way.

Lambdas also replace anonymous classes which makes the code less complex and to write more structured and less error-prone programs.

Creating Lambda Expression

To create a lambda expression, define the parameters inside parentheses {}, use the -> arrow, and write the implementation (body) on the right side.

A lambda expression follows a simple syntax:

A lambda expression consists of three main parts:

  • Argument-list: It can be empty or contain one or more parameters
  • Arrow (->): It connects the argument list with the body
  • Body: It contains the logic (expressions or statements)

No Parameter Syntax

Here is the syntax of a lambda expression with no parameter:

One Parameter Syntax

Here is the syntax of a lambda expression with only one parameter:

Two Parameter Syntax

Here is the syntax of a lambda expression with two parameters:

Creating Interface Without Using Lambda Expression

Before lambda expressions, we used anonymous classes to provide implementation of interfaces.

Example

In this example, we implement an interface using an anonymous class without using a lambda expression.

Compile and Run

Output:

Drawing 10

Creating Interface Using Lambda Expression

Lambda expressions provide a shorter and cleaner way to implement functional interfaces. They remove the need for anonymous classes.

Example

In this example, we use a lambda expression to simplify the implementation of a functional interface.

Output:

1 2 3 4 5 6 7 8 9 10

Lambda Expression with No Parameter

A lambda expression can have no parameters when the method does not require any input.

Example

In this example, we use a lambda expression that does not take any parameters.

Compile and Run

Output:

I have nothing to say.

Lambda Expression with Single Parameter

A lambda expression can take a single parameter. Parentheses are optional when only one parameter is used.

Example

In this example, we use a lambda expression with a single parameter to process input.

Compile and Run

Output:

Hello, Sonoo
Hello, Sonoo

Lambda Expression with Multiple Parameters

Lambda expressions can accept multiple parameters when the method requires more than one input.

Example

In this example, we use a lambda expression with multiple parameters to perform a calculation.

Compile and Run

Output:

Average: 20.0

Lambda Expression with and without Return Keyword

Lambda expressions may or may not use the return keyword. It is optional for single-line expressions but required for multiple statements.

Example

In this example, we demonstrate lambda expressions with and without using the return keyword.

Compile and Run

Output:

30
300

Lambda Expression with forEach Loop

Lambda expressions are commonly used with collections to iterate elements in a simple way.

Example

In this example, we use a lambda expression with forEach to iterate over a collection.

Compile and Run

Output:

ankit
mayank
irfan
jai

Lambda Expression for Creating Thread

Lambda expressions can be used to simplify thread creation by implementing the Runnable interface.

Example

In this example, we use a lambda expression to create and run a thread.

Compile and Run

Output:

Thread1 is running...
Thread2 is running...