Lesson · Lambdas & Functional Interfaces
Functional interfaces: the one method that makes a lambda legal
14 min readFunctional Interfaces
A lambda needs a target. That target is always an interface with exactly one abstract method, which is why the same lambda syntax can become a Predicate, a Comparator or a Runnable depending on where it is assigned.
One abstract method is the whole contract
An interface is functional when it has exactly one abstract method. That single method is the shape a lambda fills in: parameters in, result out. The compiler does not care about the interface's name, only that the shape matches.
Runnable, Comparator and Callable were functional interfaces long before the phrase existed. Java 8 added the java.util.function family so common shapes would stop being reinvented per project.
@FunctionalInterface interface Validator { boolean isValid(String value); } Validator notBlank = value -> !value.isBlank(); System.out.println(notBlank.isValid("jvm")); // true
The shapes you will meet daily
Four interfaces cover most everyday code. Learn their single methods and where the lambda's value goes, and every stream signature becomes readable.
| Interface | Single method | Shape |
|---|---|---|
| Predicate<T> | test(T) | one value in, boolean out |
| Function<T, R> | apply(T) | one value in, another type out |
| Consumer<T> | accept(T) | one value in, nothing out |
| Supplier<T> | get() | nothing in, one value out |
| BiFunction<T, U, R> | apply(T, U) | two values in, one out |
| UnaryOperator<T> | apply(T) | same type in and out |
| BinaryOperator<T> | apply(T, T) | two of one type in, one out |
SAM inspector · one method decides everything
single abstract methodsingle method
test
example lambda
when it runs
longEnough.test("java") → true
One value in, boolean out. The shape behind filter, removeIf and anyMatch.
What does and does not count
The annotation @FunctionalInterface is documentation with teeth: it tells the compiler to reject any change that adds a second abstract method. Without it, an interface with one abstract method is still usable for lambdas, just not protected.
- Default and static methods do not count. They have bodies, so they are not abstract. A functional interface may have any number of them.
- Methods inherited from Object do not count. Redeclaring equals or hashCode does not break the single-method rule.
- Inheritance counts totals, not declarations. An interface may extend others as long as exactly one abstract method remains in the union of all signatures.
- Two abstract methods break it. The compiler reports multiple non-overriding abstract methods, and no lambda can implement the pair in one expression.
- Checked exceptions are not part of the shape. A lambda for Function cannot throw a checked exception, because apply is not declared to. Interfaces you write decide this themselves.
Small behaviors build larger ones
Because the shapes are interfaces, the JDK ships combinators on them. Predicate.and/or/negate, Function.andThen/compose and Comparator.thenComparing are just methods returning the same interface, which means a lambda can be split into pieces and recombined at the call site.
Predicate<String> longEnough = s -> s.length() >= 4; Predicate<String> startsWithJ = s -> s.startsWith("j"); Predicate<String> both = longEnough.and(startsWithJ); System.out.println(both.test("java")); // true System.out.println(both.negate().test("java")); // false
Writing your own functional interface
Write one when no JDK shape fits or when a name carries meaning for your domain. Give it the single method, a default combinator if useful, and the annotation so the contract is enforced.
When to prefer the JDK set instead: if your shape is exactly Predicate's or Function's, reuse it. Fifteen project-specific interfaces that all look like Function<String, Integer> make a codebase harder, not clearer.
A functional interface is a type, not a class. This is why lambdas can implement it without inheritance: the compiler verifies the shape, then generates the implementation.
The annotation is a promise, not a mechanism
@FunctionalInterface adds no runtime behavior. It exists so that a future edit that adds a second abstract method fails at compile time instead of breaking every lambda assigned to the interface. Use it on every interface you design as a behavior type.
Interview angles
What makes an interface functional?+
Exactly one abstract method. Default methods, static methods and redeclarations of Object methods do not count against it; any second abstract method (declared or inherited) makes lambdas impossible.
Why does @FunctionalInterface exist if the compiler supports single-method interfaces anyway?+
To freeze the contract. Any interface with one abstract method works as a lambda target, but the annotation makes the one-method rule a compile-time invariant, so accidental additions are caught immediately.
Can a functional interface have default methods?+
Yes, as many as you like. Default methods have bodies, so they are not abstract. Many JDK ones do: Predicate.and, Function.andThen, Comparator.thenComparing are all default methods returning the same functional type.
Can a functional interface extend another interface?+
Yes, if the total number of abstract methods in the inherited chain is still exactly one. It may also extend an interface with abstract methods that the new interface overrides with defaults, since the override removes the abstract obligation.
Why does Function not throw checked exceptions?+
Its apply method does not declare any, so a lambda assigned to it cannot throw them either. If you need a throwing shape, define your own interface whose method declares the exception, then wrap it where the generic API expects a standard one.