What is a functional interface? How do lambdas and method references relate to it? Give examples using the built-in ones.
Show the explanation
A functional interface has exactly one abstract method (it may also have default and static methods). @FunctionalInterface asks the compiler to check that. A lambda is a compact implementation of that one method, and a method reference is a shorter lambda that just calls an existing method.
Common built-ins in java.util.function:
| Interface | Method | Shape |
|---|---|---|
Predicate<T> | test | T → boolean |
Function<T,R> | apply | T → R |
Consumer<T> | accept | T → void |
Supplier<T> | get | () → T |
BinaryOperator<T> | apply | (T, T) → T |
Predicate<String> isLong = s -> s.length() > 5;
Function<String, Integer> length = String::length; // method reference
Supplier<List<String>> fresh = ArrayList::new; // constructor reference
List<String> names = List.of("Asha", "Kabirraj", "Dev", "Meenakshi");
names.stream().filter(isLong).map(String::toUpperCase).forEach(System.out::println);Lambdas can use local variables from the enclosing scope only if they are effectively final (never reassigned). Unlike anonymous classes, this inside a lambda refers to the enclosing object, not to the lambda.
Original UniCircle question. Last reviewed 22 Oct 2026.
