Skip to content
UniCircle
MediumConceptualModern Java

Lambdas, functional interfaces and method references

Question JAVA-MOD-029

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:

InterfaceMethodShape
Predicate<T>testT → boolean
Function<T,R>applyT → R
Consumer<T>acceptT → void
Supplier<T>get() → T
BinaryOperator<T>apply(T, T) → T
Java
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.