Functional Interfaces

A functional interface is an interface with exactly one abstract method. Its single method describes the operation a lambda must implement. Java may add default and static methods without breaking the rule. @FunctionalInterface asks the compiler to verify the contract.

Functional interfaces connecting lambdas to Java operations

@FunctionalInterface
interface IdFormatter {
    String format(long id);
}

IdFormatter formatter = id -> "user-" + id;
System.out.println(formatter.format(42));

The lambda is an object compatible with IdFormatter; it is not a free-standing function. The target interface determines parameter types and the return type. This is why x -> x * 2 can mean different things in different contexts, such as Function<Integer, Integer> or ToIntFunction<Integer>.

Interface Method Typical question
Predicate<T> boolean test(T) Does this value satisfy a condition?
Function<T, R> R apply(T) How is this value converted?
Consumer<T> void accept(T) What action should run?
Supplier<T> T get() How can a value be produced lazily?

Choose the narrowest interface that describes the behavior. A predicate should not mutate an object; a consumer should not secretly calculate a result that callers need; a supplier should create or retrieve a value only when asked. Naming variables after their role makes pipelines easier to read: activeUser, toDto, sendEmail, and newRequestId.

Predicate<User> activeUser = User::active;
Function<User, UserDto> toDto = user -> new UserDto(user.id(), user.name());
Consumer<User> audit = user -> log.info("user={}", user.id());
Supplier<UUID> newRequestId = UUID::randomUUID;

The standard interfaces are generic and reusable, but a domain-specific interface can be clearer when the operation has a meaningful business name or several domain constraints.

Glossary
bytecode
Platform-independent compiled code that the JVM can execute.
class
A blueprint that defines fields and methods for objects in object-oriented programming.
object
A runtime instance of a class with its own state and behavior.
method
A named block of behavior that belongs to a class or object and can be called by code.
field
A variable stored inside a class or object that represents part of its state.
constructor
A special method-like block used to create and initialize a new object.
package
A namespace that groups related Java classes and helps organize code.
interface
A contract that defines methods a class can implement without prescribing all internal details.
generics
A language feature that lets code work with types as parameters while preserving type safety.
type erasure
The Java compiler process that removes most generic type details from runtime bytecode.
collection
A data structure that stores a group of elements and provides operations over them.
list
An ordered collection where elements have positions and duplicates are allowed.
set
A collection that stores unique elements and is commonly used for membership checks.
map
A key-value data structure where each key points to a value.
queue
A collection designed for processing elements in a defined order, often first-in-first-out.
lambda
A compact function-like expression used to pass behavior as data.
functional interface
An interface with one abstract method, suitable for lambdas and method references.
stream
A Java API for processing data sequences with operations such as map, filter, and reduce.
optional
A container object that represents a value that may or may not be present.
thread
An independent path of execution inside a running program.
synchronization
Coordination that protects shared data when multiple threads access it.
heap
The runtime memory area where Java objects are allocated.
stack
The runtime memory area that stores method calls, local variables, and call frames.
garbage collector
A JVM component that automatically frees memory occupied by objects no longer reachable.
memory leak
A situation where memory remains occupied because objects are still referenced but no longer useful.