Lambda Expressions in Java 8
Fast recall
Quick revision
Core answer
Whether the candidate understands lambdas as a language feature and the motivation behind Java 8's functional additions
Describing lambda syntax without explaining functional interfaces — the type a lambda actually targets
Connect lambdas to the Streams API and mention method references as the even-shorter form
Interview-ready guide
Interview answer
A lambda expression is concise syntax for supplying the function method of a compatible functional interface.
compareTo(b)instead of an anonymous Comparator object whose only purpose is that method body. Java 8 introduced lambdas so behavior could be passed to APIs without repeated wrapper code. This is especially useful in Stream operations:filteraccepts a Predicate andmap` accepts a Function, and a lambda can provide either implementation.
The target functional-interface type is essential because it tells the compiler the parameter and return types. Lambdas replace suitable single-method anonymous-class cases, but an anonymous class remains different: it creates its own class scope, has its own this, and can declare fields and extra methods.
Lambda Expressions in Java 8
Direct Answer
A lambda expression supplies the function method of a compatible functional interface using concise parameter-and-body syntax. Java 8 introduced it to make passing behavior less verbose, which makes APIs such as Streams practical to read. A lambda can replace some single-method anonymous-class uses, but it is not a general replacement for anonymous classes.
Code Example
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Predicate;
public class LambdaSyntaxDemo {
public static void main(String[] args) {
List<String> names = Arrays.asList("Charlie", "Alice", "Bob");
Comparator<String> anonymous = new Comparator<String>() {
@Override
public int compare(String a, String b) {
return a.compareTo(b);
}
};
Collections.sort(names, anonymous);
Collections.sort(names, (a, b) -> a.compareTo(b));
Collections.sort(names, String::compareTo);
Runnable noParameters = () -> System.out.println("no parameters");
Predicate<String> longName = s -> s.length() > 5;
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
Runnable block = () -> {
System.out.println("step 1");
System.out.println("step 2");
};
noParameters.run();
System.out.println(longName.test("Charlie"));
System.out.println(add.apply(2, 3));
block.run();
}
}
The anonymous Comparator, lambda, and method reference express the same sort order. The remaining lines show the main lambda syntax shapes with real target types.
Common Mistakes & Pitfalls
- Forgetting the functional interface: Freshers describe lambda syntax but can't name what type a lambda becomes. A lambda needs a target interface with exactly one abstract method, like
RunnableorComparator— without one, it won't compile. - Treating a lambda as a standalone function: Trying to call a lambda where a plain method is expected. A lambda only fits where a functional-interface type is the expected target, not as a free function.
- Overusing block bodies: Wrapping a single expression in
{ return ...; }braces. Preferx -> x * 2overx -> { return x * 2; }when the body is one expression.
Check your understanding
Answer these without looking back at the article.
- What is a functional interface and how does it relate to lambdas?
- What is the difference between a lambda and a method reference?
- Can a lambda throw a checked exception?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.