Java Lambda Expressions

Last Updated : 28 Nov, 2026

Lambda expressions were introduced in Java 8 to represent anonymous functions without declaring a separate method or class. They implement functional interfaces and provide a concise way to define behavior.

  • Lambda expressions use the arrow operator (->) to separate parameters from the body.
  • They can access local variables from the enclosing scope if those variables are final or effectively final.
Java
@FunctionalInterface
interface Calculator {
    int calculate(int a, int b);
}

public class GFG {
    public static void main(String[] args) {

        Calculator add = (a, b) -> a + b;
        Calculator multiply = (a, b) -> a * b;
        Calculator subtract = (a, b) -> a - b;

        System.out.println("Addition: " + add.calculate(10, 5));
        System.out.println("Multiplication: " + multiply.calculate(10, 5));
        System.out.println("Subtraction: " + subtract.calculate(10, 5));
    }
}

Output
Addition: 15
Multiplication: 50
Subtraction: 5

Explanation:

  • Calculator defines one abstract method, calculate().
  • Each lambda expression provides a different implementation.
  • Calling calculate() executes the corresponding lambda body.

Syntax of Lambda Expressions

(parameters) -> {
// Lambda body
}

A lambda expression consists of three parts:

  • Parameters specify the input values.
  • The arrow operator (->) separates the parameters from the body.
  • The body contains the expression or statements to execute.
lambda_expression_in_java

Functional interface

A functional interface contains exactly one abstract method. A lambda expression provides an implementation of that method.

The @FunctionalInterface annotation is optional but enables compile-time validation.

Java
interface FuncInterface{
    
    void abstractFun(int x);
    default void normalFun(){
        System.out.println("Hello");
        }
}

public class GFG{
    
    public static void main(String[] args){
        
        FuncInterface fobj = (int x) -> System.out.println(2 * x);
        fobj.abstractFun(5);
    }
}

Output
10

Explanation: In above example, FuncInterface contains one abstract method, abstractFun(), and one default method, normalFun(). The lambda expression (int x) -> System.out.println(2 * x) provides the implementation of the abstract method. When abstractFun(5) is called, the value 5 is multiplied by 2, so the output is 10.

Types of Lambda Parameters

Lambda expressions can accept zero, one, or multiple parameters.

1. Lambda with Zero Parameters

A zero-parameter lambda uses empty parentheses ().

Java
public class GFG{
    public static void main(String[] args){

        // Lambda expression with zero parameters
        Runnable task = () -> System.out.println("Hello Java");

        // Execute the lambda expression
        task.run();
    }
}

Output
Hello Java

Explanation:

  • Runnable is a functional interface containing the run() method.
  • () -> defines a lambda expression with zero parameters.
  • task.run() executes the lambda body and prints the message.

Note: task.run() executes the task on the current thread. To execute it on a new thread, use new Thread(task).start().

2. Lambda with a Single Parameter

A single-parameter lambda accepts one input value. Parentheses are optional when the parameter type is inferred.

Java
import java.util.function.Consumer;

class GFG{
    public static void main(String[] args){

        Consumer<String> message = name -> System.out.println("Hello " + name);

        message.accept("Geeks");
    }
}

Output
Hello Geeks

Here, name -> is equivalent to (name) ->.

3. Lambda with Multiple Parameters

A multiple-parameter lambda accepts two or more inputs. Parentheses are mandatory.

Java
import java.util.function.BiPredicate;

class GFG{
    public static void main(String[] args){

        // Compare two integers
        BiPredicate<Integer, Integer> greater = (a, b) -> a > b;

        System.out.println(greater.test(10, 5));
    }
}

Output
true

The lambda (a, b) -> a > b compares two integers and returns a boolean result.

Lambda Expressions with Collections and Streams

Lambda expressions are commonly used with collections and the Stream API for filtering and transforming data.

Example:

Java
import java.util.Arrays;
import java.util.List;

class GFG{
    public static void main(String[] args){

        List<String> names = Arrays.asList("Akshat", "Ram", "Nisha", "Anu");

        // Filter and transform names
        names.stream().filter(name -> name.startsWith("A"))
            .map(name -> name.toUpperCase())
            .forEach(System.out::println);
    }
}

Output
AKSHAT
ANU

Explanation:

  • filter() selects names starting with A.
  • map() converts matching names to uppercase.
  • forEach() prints the results.

Common Built-in Functional Interfaces

Java provides built-in functional interfaces that work with lambda expressions.

Interface

Abstract Method

Purpose

Predicate<T>

boolean test(T t)

Tests a condition.

Consumer<T>

void accept(T t)

Performs an action.

Supplier<T>

T get()

Supplies a value.

Function<T, R>

R apply(T t)

Transforms a value.

Comparator<T>

int compare(T a, T b)

Compares two objects for ordering.

Validity Check: Lambda Expressions

The following expressions are valid or invalid based on their syntax, assuming a compatible target functional interface for valid expressions.

Lambda Expression

Validity

Reason

() -> {}

Valid

Empty body with no parameters.

() -> "geeksforgeeks"

Valid

Returns a string expression.

() -> { return "geeksforgeeks"; }

Valid

Uses a return statement.

(Integer i) -> "geeksforgeeks" + i

Valid

Uses a typed parameter.

(String s) -> { return "geeksforgeeks"; }

Valid

An unused parameter is allowed.

x -> { return x + 1; }

Valid

Correct syntax when the target type supports it.

() -> { return "Hello" }

Invalid

Missing semicolon after return.

(int x, y) -> x + y

Invalid

Parameter types cannot be mixed this way.

Note: Lambda expressions require a compatible functional interface type. An expression that is syntactically valid may still fail compilation if its parameter or return types do not match the target interface.

Advantages of Lambda Expressions

  • Reduce boilerplate code compared with anonymous inner classes.
  • Allow behavior to be passed to methods through functional interfaces.
  • Support concise filtering and transformation with collections and streams.
Comment