Java 8 Functional Interfaces: Function, Supplier, Consumer, Runnable and Practical Usage Patterns
This article introduces Java 8 functional interfaces—including Function, Supplier, Consumer, and Runnable—explains their signatures, demonstrates how they can replace traditional if‑else statements for exception handling, branch processing, and null checks, and provides concrete code examples and usage tips.
Java 8 provides several built‑in functional interfaces— Function , Supplier , Consumer and Runnable —that can be used to replace verbose if...else... logic in backend code.
The @FunctionalInterface annotation marks an interface that contains exactly one abstract method, making it eligible for lambda expressions. The four core interfaces have the following characteristics:
Supplier : no parameters, returns a value.
Consumer : accepts a single parameter, returns nothing.
Runnable : no parameters and no return value.
Function : accepts one parameter and returns a result; the other three can be seen as special cases of Function .
Example 1 – ThrowExceptionFunction
/**
* 抛异常接口
*/
@FunctionalInterface
public interface ThrowExceptionFunction {
/**
* 抛出异常信息
*
* @param message 异常信息
*/
void throwMessage(String message);
}Utility method that creates a ThrowExceptionFunction which throws an exception when a boolean flag is true:
/**
* 如果参数为true抛出异常
*/
public static ThrowExceptionFunction isTure(boolean b) {
return (errorMessage) -> {
if (b) {
throw new RuntimeException(errorMessage);
}
};
}Example 2 – BranchHandle
/**
* 分支处理接口
*/
@FunctionalInterface
public interface BranchHandle {
/**
* 分支操作
*
* @param trueHandle 为true时要进行的操作
* @param falseHandle 为false时要进行的操作
*/
void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle);
}Method that returns a BranchHandle to execute different Runnable s based on a boolean condition:
/**
* 参数为true或false时,分别进行不同的操作
*/
public static BranchHandle isTureOrFalse(boolean b) {
return (trueHandle, falseHandle) -> {
if (b) {
trueHandle.run();
} else {
falseHandle.run();
}
};
}Example 3 – PresentOrElseHandler
/**
* 空值与非空值分支处理
*/
public interface PresentOrElseHandler
{
/**
* 值不为空时执行消费操作,值为空时执行其他操作
*
* @param action 值不为空时的 Consumer
* @param emptyAction 值为空时的 Runnable
*/
void presentOrElseHandle(Consumer
action, Runnable emptyAction);
}Factory method that creates a handler to process a possibly‑null string:
/**
* 参数为true或false时,分别进行不同的操作
*/
public static PresentOrElseHandler
isBlankOrNoBlank(String str) {
return (consumer, runnable) -> {
if (str == null || str.length() == 0) {
runnable.run();
} else {
consumer.accept(str);
}
};
}These functional interfaces allow developers to write concise lambda expressions that encapsulate conditional logic, improve code readability, and reduce the clutter of repetitive if...else... blocks in Java backend projects.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.