VOOZH about

URL: https://www.geeksforgeeks.org/java/lambda-expressions-java-8/

⇱ Java Lambda Expressions - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Lambda Expressions

Last Updated : 8 May, 2026

lambda expressions, introduced in Java 8, allow developers to write concise, functional-style code by representing anonymous functions. They enable passing code as parameters or assigning it to variables, resulting in cleaner and more readable programs.

  • Lambda expressions implement a functional interface (An interface with only one abstract function)
  • Enable passing code as data (method arguments).
  • Lambda expressions can access only final or effectively final variables from the enclosing scope.
  • Lambdas cannot throw checked exceptions unless the functional interface declares them.

Syntax of Lambda Expressions

Java Lambda Expression has the following syntax:

(parameters) -> {
// multiple statements
}

👁 lambda_expression_in_java
  • Parameter List: Parameters for the lambda expression
  • Arrow Token (->): Separates the parameter list and the body
  • Body: Logic to be executed.

Functional interface

A functional interface has exactly one abstract method. Lambda expressions provide its implementation. @FunctionalInterface annotation is optional but recommended to enforce this rule at compile time.

  • A functional interface contains exactly one abstract method, but it can have multiple default and static methods.
  • It is mainly used with lambda expressions and method references to enable functional programming in Java.

Output
10

Types of Lambda Parameters

There are three Lambda Expression Parameters are mentioned below:

1. Lambda with Zero Parameters

Lambda expression without any input.

Syntax:

() -> System.out.println("Zero parameter lambda");


Output
This is a zero-parameter lambda expression!

2. Lambda with a Single Parameter

Takes one input; parentheses are optional.

Syntax:

(p) -> System.out.println("One parameter: " + p);

  • It is not mandatory to use parentheses if the type of that variable can be inferred from the context.
  • Parentheses are optional if the compiler can infer the parameter type from the functional interface.

Output
All elements:
1
2
3
Even elements:
2

Note: The forEach() method internally uses the Consumer<T> functional interface, which takes one argument and performs an action.

3. Lambda Expression with Multiple Parameters

Takes more than one input; parentheses are required.

Syntax:

(p1, p2) -> System.out.println("Multiple parameters: " + p1 + ", " + p2);


Output
9
20

Note: Lambda expressions are just like functions and they accept parameters just like functions. 

Examples in Collections / Streams

Lambda expressions are widely used with Java Collections and Streams for concise operations


Output
All names:
Alice
Bob
Charlie
Adam

Names starting with 'A':
ALICE
ADAM

Advantages

  • Concise Code: Reduce boilerplate compared to anonymous classes.
  • Functional Programming: Treat functions as first-class citizens.
  • Improved Readability: Code is easier to read and maintain.
  • Enhanced Collections and Streams: Simplify operations like filtering, mapping, and iterating.

Common Built-in Functional Interfaces

InterfaceMethodPurpose
Predicateboolean test(T t)Tests a given condition and returns true or false.
Consumervoid accept(T t)Performs an action on the given argument without returning a result.
SupplierT get()Supplies or generates a result without taking any input.
Comparator<T>int compare(T o1, T o2)Compares two objects to determine their order.
Comparable<T>int compareTo(T o)Defines the natural ordering for objects of a class.

Validity Check: Example Lambda Expressions

ExpressionValidityReason
() -> {}ValidNo parameters, empty body
() -> "geeksforgeeks"ValidSingle expression returns value
() -> { return "geeksforgeeks"; }ValidUses braces with return keyword
(Integer i) -> { return "geeksforgeeks" + i; }ValidCorrect syntax with typed parameter
(String s) -> { return "geeksforgeeks"; }ValidParameter unused but valid
() -> { return "Hello" }InvalidMissing semicolon after return statement
x -> { return x + 1; }InvalidInvalid if type inference not possible
(int x, y) -> x + yInvalidIf one parameter has type, all must
Comment
Article Tags: