-
Hajipur, Bihar, 844101
Lambda expressions were introduced in Java 8 to make code cleaner and more concise, especially when working with functional interfaces. A lambda expression allows you to write functions in a simpler way — without defining an entire class or method.
They’re most commonly used in functional programming, stream operations, and event handling in modern Java applications.
A lambda expression is basically a short block of code that takes in parameters and returns a value. It looks like this:
(parameter_list) -> { body }
For example:
(int a, int b) -> a + b
This expression adds two numbers and returns the result.
Before Java 8, to pass behavior (code) as an argument, you had to create anonymous classes. With lambdas, you can do it in one line.
Advantages:
Makes code more readable and shorter.
Removes unnecessary boilerplate code.
Enables functional-style programming.
Useful in collections and streams.
There are several ways to write a lambda depending on how simple or complex the expression is.
| Syntax Type | Example | Description |
|---|---|---|
| Without parameters | () -> System.out.println("Hello") |
No arguments |
| With one parameter | x -> x * x |
No parentheses needed |
| With multiple parameters | (a, b) -> a + b |
Enclosed in parentheses |
| With multiple statements | (x, y) -> { int sum = x + y; return sum; } |
Use curly braces |
A lambda expression works only with functional interfaces — interfaces that have exactly one abstract method.
Java has several predefined functional interfaces in the java.util.function package such as:
Predicate<T> – returns a boolean value.
Function<T, R> – returns a result.
Consumer<T> – performs an action and returns nothing.
Supplier<T> – returns a value but takes no input.
You can also create your own functional interface.
Example:
@FunctionalInterface
interface Greeting {
void sayHello(String name);
}
@FunctionalInterface
interface Sayable {
void say(String message);
}
public class LambdaExample {
public static void main(String[] args) {
Sayable s = (msg) -> {
System.out.println("Message: " + msg);
};
s.say("Welcome to Java Lambda Expressions!");
}
}
Output:
Message: Welcome to Java Lambda Expressions!
Here, (msg) -> { ... } defines a lambda expression that implements the say() method of the interface.
@FunctionalInterface
interface Calculator {
int operate(int a, int b);
}
public class LambdaCalc {
public static void main(String[] args) {
Calculator add = (a, b) -> a + b;
Calculator multiply = (a, b) -> a * b;
System.out.println("Addition: " + add.operate(5, 3));
System.out.println("Multiplication: " + multiply.operate(5, 3));
}
}
Output:
Addition: 8
Multiplication: 15
Instead of using an anonymous class for creating a thread, you can use a lambda expression.
public class LambdaThread {
public static void main(String[] args) {
Thread t = new Thread(() -> {
for (int i = 1; i <= 5; i++) {
System.out.println("Running in Lambda Thread: " + i);
}
});
t.start();
}
}
Output:
Running in Lambda Thread: 1
Running in Lambda Thread: 2
...
This makes thread creation much shorter and easier to read.
Lambdas are often used with Java’s collection classes to simplify operations like sorting.
import java.util.*;
public class SortExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Nisha", "Aarti", "Meera", "Riya");
Collections.sort(names, (a, b) -> a.compareToIgnoreCase(b));
System.out.println("Sorted Names: " + names);
}
}
Output:
Sorted Names: [Aarti, Meera, Nisha, Riya]
Without lambda, this would require a full anonymous Comparator class.
import java.util.function.Predicate;
public class PredicateExample {
public static void main(String[] args) {
Predicate<Integer> isEven = n -> n % 2 == 0;
System.out.println("Is 10 even? " + isEven.test(10));
}
}
import java.util.function.Consumer;
public class ConsumerExample {
public static void main(String[] args) {
Consumer<String> print = s -> System.out.println("Hello, " + s);
print.accept("Anjali");
}
}
import java.util.function.Function;
public class FunctionExample {
public static void main(String[] args) {
Function<String, Integer> strLength = s -> s.length();
System.out.println("Length: " + strLength.apply("Lambda"));
}
}
The Stream API works perfectly with lambdas. For example, filtering a list becomes very simple.
import java.util.*;
public class StreamExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
numbers.stream()
.filter(n -> n % 2 == 0)
.forEach(n -> System.out.println("Even: " + n));
}
}
Output:
Even: 2
Even: 4
Even: 6
You can omit parameter types — Java infers them automatically.
If there’s only one statement, curly braces {} and return are optional.
Lambda expressions cannot throw checked exceptions unless the interface declares them.
They can be assigned to variables or passed directly as method arguments.
When implementing functional interfaces.
When performing stream operations like filtering or mapping.
In event-driven programs (e.g., button click listeners).
To simplify thread or collection code.
Lambda expressions simplify how you write and read code in Java. Instead of creating anonymous classes, you can represent functionality directly using concise syntax. They work seamlessly with functional interfaces, collections, and streams.
Lambdas are one of the most useful features of modern Java, enabling cleaner, faster, and more expressive code.
Write a Java program using a lambda expression to print "Welcome to Java Programming" five times.
Create a functional interface named MathOperation with a method calculate(int a, int b). Use lambda expressions to perform addition, subtraction, multiplication, and division.
Write a Java program to sort a list of strings in reverse alphabetical order using a lambda expression.
Use a lambda expression to filter out odd numbers from a list of integers and print only even numbers.
Create a program that uses Consumer<String> to print each name in a list prefixed with “Hello”.
Write a Java program using Predicate<Integer> to check if a number is greater than 50.
Use Function<String, Integer> with a lambda to find and print the length of a given string.
Write a program that uses a lambda expression inside a Thread to print numbers from 1 to 10.
Create a functional interface Greeting with one method sayHello(String name). Use a lambda expression to greet multiple names from an array.
Write a Java program to use a lambda expression to find the maximum number in a list of integers.