Java Topics
Lambda Expression
Last Updated : 26 May, 2026
title: Lambda Expression in Java 8
title: Lambda Expression in Java 8 description: Lambda expressions ka complete guide
Anonymous function — naam ke bina function. Code concise hota hai.
Syntax
// (parameters) -> expression
// (parameters) -> { statements }
// Bina parameter
() -> System.out.println("Hello")
// Ek parameter (parentheses optional)
x -> x * x
(x) -> x * x
// Multiple parameters
(a, b) -> a + b
// Multiple statements
(a, b) -> {
int sum = a + b;
return sum;
}Functional Interface ke saath
Lambda sirf functional interface mein use hota hai (exactly 1 abstract method).
// Runnable
Runnable r = () -> System.out.println("Running!");
new Thread(r).start();
// Comparator
List<String> names = Arrays.asList("Zara", "Alex", "Mia");
names.sort((a, b) -> a.compareTo(b));
names.sort((a, b) -> a.length() - b.length()); // by length
// Custom Functional Interface
@FunctionalInterface
interface MathOperation {
int operate(int a, int b);
}
MathOperation add = (a, b) -> a + b;
MathOperation multiply = (a, b) -> a * b;
System.out.println(add.operate(5, 3)); // 8
System.out.println(multiply.operate(5, 3)); // 15Built-in Functional Interfaces (java.util.function)
// Predicate<T> — condition check (returns boolean)
Predicate<Integer> isEven = n -> n % 2 == 0;
isEven.test(4); // true
// Function<T, R> — T input, R output
Function<String, Integer> length = s -> s.length();
length.apply("Hello"); // 5
// Consumer<T> — input, no output
Consumer<String> printer = s -> System.out.println(s);
printer.accept("Hello");
// Supplier<T> — no input, T output
Supplier<Double> random = () -> Math.random();
random.get(); // 0.XYZ
// BiFunction<T, U, R>
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
add.apply(3, 4); // 7
// UnaryOperator<T>
UnaryOperator<String> upper = s -> s.toUpperCase();
upper.apply("hello"); // "HELLO"
// BinaryOperator<T>
BinaryOperator<Integer> max = (a, b) -> a > b ? a : b;
max.apply(5, 3); // 5Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Lambda Expression.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java topic.
Search Terms
java, java programming, core java, java master course, java notes, master, course, features
Related Java Topics