Java · Core APIs

Lambda Expressions

Lambdas are short, unnamed functions you can pass around. They make sorting, filtering and event handling much shorter.

Intermediate 5 min Lesson 20 of 31Examples target Java 21 LTS (they also run on Java 25).
Before you start:Generics
0/31 in Java
On this page · 5 min · Intermediate

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#

JavaSorting and filtering products Illustrative
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)));
Expected output
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#

  1. Comparator.comparingDouble sorts by price using a method reference.
  2. forEach runs a lambda for each item.
  3. A Predicate expresses “cheap”; negate() flips it.
  4. 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#

Changing local variables inside lambdas

Captured variables must be effectively final.

Huge multi-line lambdas

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
Java Illustrative
names.sort(Comparator.comparingInt(String::length).thenComparing(Comparator.naturalOrder()));

Mini quiz#

Lambda Expressions quizMultiple choice Question 1 of 1

Which functional interface returns a boolean?

Score so far: 0/0

Want more? Practise all Java MCQs · Java interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.