[!NOTE] Introduced in Java 8, Lambda Expressions fundamentally modernized the language. They allow you to pass specific "behavior" (a function) into another function, without having to build a massive, verbose Anonymous Inner Class.
The Boilerplate Problem
Imagine you have a Button object in a UI. You want to tell the button: "When you are clicked, execute this specific block of code."
Historically, you had to physically create a brand new class that implemented the ClickListener interface, write the overriding method, instantiate it, and pass it in.
// 2004 Java (Pre-Java 8)
button.setOnClickListener(new ClickListener() {
@Override
public void onClick() {
System.out.println("Button clicked!");
}
});
The Lambda Solution
A Lambda expression condenses all of that into a single, elegant arrow function: parameter -> expression.
If the interface only has one abstract method (called a Functional Interface), the Java compiler is smart enough to just inject your arrow function directly into that slot!
// Modern Java 8+ Implementation!
button.setOnClickListener( () -> System.out.println("Button Clicked!") );
Anatomy of a Lambda
Lambdas can take parameters and can have multi-line bodies wrapped in curly braces.
- No parameters:
() -> System.out.println("No args");
- One parameter (parentheses are optional!):
name -> System.out.println("Hello " + name);
- Multiple parameters with a multi-line body (requires { } and 'return'):
(x, y) -> {
int sum = x + y;
return sum;
}
Practical Use: Processing ArrayLists
Lambdas shine brightest when interacting with Collections.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(5);
numbers.add(9);
numbers.add(8);
numbers.add(1);
// Instead of writing a complex Iterator while-loop...
// We pass a Lambda function that tells '.forEach()' exactly what to do with each element 'n'!
numbers.forEach( (n) -> { System.out.println(n); } );
}
}
[!TIP] You cannot use a lambda expression if the target interface has two or more abstract methods. Lambdas ONLY work on Functional Interfaces (like
Runnable,Callable, orComparator) because otherwise the compiler wouldn't know which method you are trying to override!