Skip to content
Veltoryx Technologies
ProLesson 8.1Intermediate3 min readUpdated 30 October 2026

Lambdas

Pass behaviour as a value. Learn lambda syntax step by step, where lambdas can be used, and the effectively-final rule.

A lambda is a short, unnamed function that you can pass around like a value. Lambdas (Java 8) changed how Java code looks: sorting, filtering, event handlers and streams all use them.

01From anonymous class to lambda

Here's the same comparator three ways:

LambdaSteps.java
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class LambdaSteps {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>(List.of("Vikram", "Asha", "Jo"));

        // 1. Anonymous class (before Java 8)
        names.sort(new Comparator<String>() {
            @Override
            public int compare(String a, String b) {
                return a.length() - b.length();
            }
        });

        // 2. Lambda with full syntax
        names.sort((String a, String b) -> { return a.length() - b.length(); });

        // 3. Lambda, shortest form
        names.sort((a, b) -> a.length() - b.length());

        System.out.println(names);
    }
}
Expected output
[Jo, Asha, Vikram]

All three do the same thing. The lambda keeps only what matters: the parameters and the body.

Loading the rest of the lesson…