Lambdas, Functional Interfaces and Method References
Lambdas, introduced in Java 8, are short implementations of functional interfaces: interfaces with exactly one abstract method. Interviewers ask about the built-in functional interfaces, method references and the effectively-final rule.
What you will learn
- Define a functional interface and @FunctionalInterface
- Know Function, Predicate, Consumer, Supplier and BiFunction
- Use the four kinds of method reference
- Explain effectively final variables
Key concepts
Functional interface
An interface with one abstract method (Runnable, Comparator, Callable). Default and static methods do not count. @FunctionalInterface makes the compiler enforce it.
Core interfaces
Function<T,R> transforms, Predicate<T> tests, Consumer<T> uses, Supplier<T> produces, UnaryOperator and BinaryOperator for same-type operations, plus primitive versions like IntPredicate to avoid boxing.
Method references
Static (Integer::parseInt), bound instance (System.out::println), unbound instance (String::toUpperCase) and constructor (ArrayList::new).
Effectively final
A lambda can use local variables only if they are never reassigned after initialisation, because the lambda captures their value, not the variable.
Step by step
From anonymous class to lambda
// Java 7
Collections.sort(names, new Comparator<String>() {
@Override public int compare(String a, String b) { return a.length() - b.length(); }
});
// Java 8+
names.sort((a, b) -> Integer.compare(a.length(), b.length()));
names.sort(Comparator.comparingInt(String::length));Examples
Composing functions
Predicate<Order> paid = o -> o.status() == Status.PAID;
Predicate<Order> big = o -> o.total().compareTo(new BigDecimal("5000")) > 0;
List<Order> vip = orders.stream().filter(paid.and(big)).toList();
Function<String, String> trim = String::trim;
Function<String, String> clean = trim.andThen(String::toLowerCase);
clean.apply(" JioCoder "); // "jiocoder"Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Write your own functional interface TriFunction and use it with a lambda.
- Explain why int count = 0; list.forEach(x -> count++); does not compile, and fix it.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
Is a lambda an anonymous class?
No. The compiler turns lambdas into invokedynamic calls with methods generated in the enclosing class, so there is no extra .class file per lambda, and this inside a lambda refers to the enclosing instance, not the lambda.
Why must captured variables be effectively final?
The lambda may run later or on another thread, after the method returned. Capturing a copy of a value that never changes avoids confusing results and data races.
Summary
Lambdas implement single-method interfaces concisely; know the java.util.function interfaces, the four method reference forms, and that captured locals must be effectively final.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus