Java : Lambda Function
In this article, We will explore the fundamental concepts around Lambda Function, understand their purpose, benefits, and look at practical examples on how to create and use them.
What are Lambada Functions?
Lambda functions (also known as Lambda Expression), are Anonymous Functions that can be treated as values and passed around as arguments to methods. They allow developers to define functions inline without creating a full method with a name, return type, and access modifiers. Lambda functions enable functional programming concepts in Java and help define inline blocks of code in a clear, concise and compact manner..
Why Lambda Functions?
In earlier versions of Java, passing a block of code (e.g., A callback) was cumbersome. not easy to provide a block of code to someone. You had to create an instance of a class that implemented an interface or anonymous instances of a class and override the method, which led to verbose boilerplate code.
// Without Lambda Functions
List<String> names = List.of("Tony", "Peter", "Bruce");
names.forEach(new Consumer<String>() {
@Override
public void accept(String name) {
System.out.println(name);
}
});Lambda Function were introduced in Java 8 to solve this problem. They offered several advantages, making Java code more readable, expressive and concise.
// With Lambda Functions
List<String> names = List.of("Tony", "Peter", "Bruce");
names.forEach(name -> System.out.println(name));Lambda Function Syntax
Lambda functions are made of three components as shown in the following snippet.
(<parameters>) -> { <body> }Function Parameters
- Parameters are listed inside parenthesis
(String a, String b) -> { }. - Parameter type can be omitted, if Java can automatically infer the types of the parameters based on the context
(a, b) -> { }. - In case of no parameters, use an empty parenthesis
() -> { }. - In case of single parameter, parenthesis can be ignored
a -> { }.
Function Body
- The body contains the code to be executed. It is enclosed in curly braces
{}. If there's only a single statement, you can omit the braces.
Arrow Operator
- The arrow operator
->separates the parameters from the body of the lambda function.
Lambda Function Usage
Lambda functions can only be assigned to a Functional Interface — Interfaces with Single Abstract Method (also known as SAM interface). Behind the scenes, Java converts the lambda function into an instance of that interface.
In Fact, conversion of lambda function to a functional interface is the only thing Java allows you to do with a lambda function.
Some of the common scenarios where lambda functions come handy is shown in the following examples.
1. Threading & Concurrency
Lambda functions makes it easier to work with threading and concurrency code, by simplifying the creation of Runnable and Callable instances.
// Without Lambda Function
new Thread(new Runnable() {
@Override
public void run() {
// ...
}
}).start();
// With Lambda Function
new Thread(() -> {
//...
}).start();2. Event Handling
Lambda functions can be used with event handling, where an action is executed in response to event generated by user while interacting with a UI or event generated by system.
// Without Lambda Function
button.addActionListener(new ActionListener() {
@Override
void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
}
});
// With Lambda Function
button.addActionListener(e -> System.out.println("Button Clicked"));3. Collections
Lambda functions are extremely useful while working with collections and especially while using the Stream API to perform operations on collections, such as filtering, mapping and reducing.
List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Filtering
List<Integer> oddNumbers = numbers.stream()
.filter(i -> i % 2 == 1)
.collect(Collectors.toList());
// Mapping
List<Integer> doubleNumbers = numbers.stream()
.map(i -> i * 2)
.collect(Collectors.toList());
// Reducing
int sum = numbers.stream()
.reduce(0, (accumulator, i) -> accumulator + i);Conclusion
In this article, we explored Lambda Functions in Java — blocks of code that can be passed around and executed. We discussed their syntax, key components, and practical applications in areas like threading, event handling, and working with collections using the Stream API. Introduced in Java 8, lambda expressions bring the power of functional programming to Java, enabling developers to write cleaner, more concise, and more expressive code with significantly less boilerplate. However, lambda functions don’t operate in isolation. Behind the scenes, they depend on Functional Interfaces.
Get Guhan B’s stories in your inbox
Join Medium for free to get updates from this writer.
In the subsequent article, we’ll take a deep dive into Functional Interfaces — what they are, why they matter, how Java uses them, and how you can define your own to write more flexible, reusable code.










