What are Lambda Expressions?
Introduced in Java 8, a lambda expression is a short block of code that takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name, and they can be implemented right in the body of another method.
They are a cornerstone of functional programming in Java and provide a clear and concise way to represent an instance of a functional interface (an interface with a single abstract method).
The Problem Before Lambdas
Before Java 8, if you wanted to pass a piece of functionality as an argument to a method, you had to use an anonymous inner class. This syntax was verbose and clunky.
Anonymous Inner Class Example (Pre-Java 8): Letโs say we want to sort a list of strings by length.
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Arrays;
List<String> names = Arrays.asList("Peter", "Anna", "Mike");
Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return a.length() - b.length();
}
});
System.out.println(names); // [Mike, Anna, Peter]
This requires a lot of boilerplate code for a simple comparison logic.
The Solution: Lambda Expressions
Lambda expressions let you express instances of functional interfaces more compactly.
Lambda Expression Example (Java 8+):
import java.util.Collections;
import java.util.List;
import java.util.Arrays;
List<String> names = Arrays.asList("Peter", "Anna", "Mike");
// The lambda expression (a, b) -> a.length() - b.length() is an implementation of the Comparator interface.
Collections.sort(names, (String a, String b) -> {
return a.length() - b.length();
});
System.out.println(names); // [Mike, Anna, Peter]
This code does the exact same thing but is much easier to read and write.
Syntax of a Lambda Expression
A lambda expression has three parts:
- Parameter List: A comma-separated list of parameters enclosed in parentheses
(). The compiler can often infer the parameter types, so you can omit them. - Arrow Token: The
->token separates the parameters from the body. - Body: A single expression or a block of code enclosed in curly braces
{}.
Further Simplification:
The previous example can be made even more concise.
// Type inference for parameters a and b
// No curly braces or return statement needed for a single expression body
Collections.sort(names, (a, b) -> a.length() - b.length());
Where Can You Use Lambda Expressions?
Lambda expressions can be used anywhere a functional interface is expected. A functional interface is any interface that contains exactly one abstract method. The java.util.function package provides many built-in functional interfaces, such as:
Predicate<T>: Represents a predicate (boolean-valued function) of one argument. Method:boolean test(T t).Function<T, R>: Represents a function that accepts one argument and produces a result. Method:R apply(T t).Consumer<T>: Represents an operation that accepts a single input argument and returns no result. Method:void accept(T t).Supplier<T>: Represents a supplier of results. Method:T get().Comparator<T>: Used for comparing two objects. Method:int compare(T o1, T o2).
Example with forEach and a Consumer:
List<String> names = Arrays.asList("Peter", "Anna", "Mike");
// The lambda expression s -> System.out.println(s) is an implementation of the Consumer interface.
names.forEach(s -> System.out.println(s));
Method References
Method references are a special, even more compact type of lambda expression that allow you to refer to an existing method by name.
- Syntax:
ClassName::methodName
Example:
The forEach example can be rewritten with a method reference.
// System.out::println is a reference to the println method of the System.out object.
names.forEach(System.out::println);
This is often the most readable option when a lambda expression just calls an existing method.
Key Takeaway
Lambda expressions are a powerful feature for writing clean, functional-style code in Java. They reduce boilerplate, improve readability, and are essential for working with modern Java APIs like the Stream API. Embrace them to make your code more expressive and concise.
Leave a comment