Lambda Expressions
Lambdas are short, unnamed functions you can pass around. They make sorting, filtering and event handling much shorter.
On this page · 5 min · Intermediate
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 30, 2026
Related tutorials
In simple words#
A lambda is a sticky note with one instruction — “x → x * 2” means “take x, give back double”. You hand the note to a method that applies it wherever needed.
- Lambda
- (params) -> expression.
- Functional interface
- Interface with one abstract method (Predicate, Function…).
- Method reference
- String::length — shortcut for a lambda.
Learning objectives#
- Write lambda expressions
- Use common functional interfaces
- Use method references
- Sort and filter with lambdas
Explanation#
Syntax: x -> x * 2, (a, b) -> a + b, or a block: x -> { … return y; }.
Lambdas implement functional interfaces: Predicate<T> (test), Function<T,R> (apply), Consumer<T> (accept), Supplier<T> (get), Comparator<T>.
Method references are shortcuts: String::toUpperCase, System.out::println, Product::price.
Lambdas can use effectively final local variables from the surrounding scope.
Code example: Sorting and filtering products#
import java.util.*;
import java.util.function.*;
record Product(String name, double price) {}
List<Product> products = new ArrayList<>(List.of(
new Product("Laptop", 55000), new Product("Mouse", 1400), new Product("Keyboard", 2200)));
products.sort(Comparator.comparingDouble(Product::price)); // method reference
products.forEach(p -> System.out.println(p.name() + " ₹" + p.price()));
Predicate<Product> cheap = p -> p.price() < 5000;
Function<Product, String> label = p -> p.name().toUpperCase();
products.removeIf(cheap.negate()); // keep only cheap ones
products.forEach(p -> System.out.println("Cheap: " + label.apply(p)));
Mouse ₹1400.0 Keyboard ₹2200.0 Laptop ₹55000.0 Cheap: MOUSE Cheap: KEYBOARD
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Comparator.comparingDouble sorts by price using a method reference.
- forEach runs a lambda for each item.
- A Predicate expresses “cheap”; negate() flips it.
- A Function transforms a product into a label.
Real-world use case#
Admin screens sort and filter product lists by price, name or stock with one-line lambdas.
Common mistakes#
Captured variables must be effectively final.
Move complex logic into a named method and use a method reference.
Best practices#
- Keep lambdas short
- Prefer method references when clearer
- Use built-in functional interfaces
- Name complex predicates
Remember this#
(params) -> result.
Predicate tests, Function maps, Consumer uses, Supplier gives.
Class::method = method reference.
Practice exercise#
Your task: Sort a list of names by length, then alphabetically.
Show solution
names.sort(Comparator.comparingInt(String::length).thenComparing(Comparator.naturalOrder()));
Mini quiz#
Which functional interface returns a boolean?
Want more? Practise all Java MCQs · Java interview questions
My notes#
Saved in this browser.