Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 44

Lambda Expressions in Java

In this lesson: Write concise code with Java lambda expressions — syntax, functional interfaces, forEach and sorting — with output.

What is a lambda

A lambda expression is a short, unnamed function you can pass around as a value. Introduced in Java 8, it uses the arrow -> and lets you write behaviour compactly — perfect for sorting, looping and event handling without writing full classes.

💡 A lambda is a tiny function: (parameters) -> body.

The lambda syntax

// (parameters) -> body
(x) -> x * 2                 // one parameter
(a, b) -> a + b             // two parameters
() -> System.out.println("Hi")  // no parameters
x -> {                       // block body
    int y = x + 1;
    return y;
}

The left side lists parameters, the arrow separates, and the right side is the body. Single expressions return automatically; a { } block uses return.

Functional interfaces

interface Greeter {
    void greet(String name);    // one abstract method
}
// lambda provides that method:
Greeter g = name -> System.out.println("Hi " + name);
g.greet("Aman");
Hi Aman

A lambda works with a functional interface — an interface with exactly one abstract method. The lambda becomes that method's implementation, with no class needed.

Using lambdas with lists

import java.util.ArrayList;
ArrayList<Integer> nums = new ArrayList<>();
nums.add(3); nums.add(1); nums.add(2);

nums.forEach(n -> System.out.println(n));  // print each
nums.sort((a, b) -> a - b);                // sort ascending
System.out.println(nums);
3
1
2
[1, 2, 3]

Lambdas shine with collections: forEach takes a lambda to run on each item, and sort takes a lambda for the order. Much shorter than old loops or classes.

Built-in functional interfaces

  • Runnable — () -> { ... }, code to run.
  • Comparator — (a, b) -> ..., for sorting.
  • Predicate — x -> boolean, a test.
  • Function — x -> result, a transform.

Java provides many ready functional interfaces in java.util.function, so you rarely define your own.

Method references

// lambda:
nums.forEach(n -> System.out.println(n));
// method reference (shorter):
nums.forEach(System.out::println);
1
2
3

When a lambda only calls one existing method, you can replace it with a method reference using ::. It is the most compact form and reads cleanly.

Common mistakes

  • Using a lambda on a non-functional interface. It must have exactly one abstract method.
  • Forgetting return in a block body. Single expressions return automatically; { } blocks do not.
  • Overcomplicating lambdas. For long logic, a named method is clearer.
  • Wrong parameter count. It must match the interface's single method.

Summary

  • A lambda is a short, unnamed function: (params) -> body.
  • It works with a functional interface (one abstract method).
  • Lambdas make collection code (forEach, sort) concise.
  • Java provides ready functional interfaces like Predicate and Function.
  • Method references (::) are an even shorter form.

Next, process data with the Stream API, or sort with Comparator.

Frequently Asked Questions

What is a lambda expression in Java?
A lambda expression is a short way to write a function without a name, using the arrow -> syntax. For example (x) -> x * 2. It lets you pass behaviour as a value, making code shorter and cleaner.
What is a functional interface?
A functional interface is an interface with exactly one abstract method. A lambda can be used wherever a functional interface is expected, because the lambda provides that single method. Examples include Runnable and Comparator.
When were lambdas added to Java?
Lambda expressions were introduced in Java 8, along with the Stream API and many built-in functional interfaces. They brought a functional-programming style to Java for cleaner, more expressive code.
What is a method reference?
A method reference is an even shorter form of a lambda that just calls an existing method, written with ::, like System.out::println. It is used when the lambda only calls one method.
Why use lambdas instead of anonymous classes?
A lambda does the same job as a small anonymous class but with far less code — no class boilerplate, just the logic. This makes code that passes behaviour (like sorting or event handling) much more readable.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicStream API Your progress: 44 / 61 (72%) · Lesson 45 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free