Using Java 8 Functional Interfaces to Replace if…else Branches
This article explains how Java 8's functional interfaces such as Function, Supplier, Consumer, and Runnable can be used to eliminate repetitive if…else statements, providing concrete interface definitions and utility methods with full code examples for exception handling, branch processing, and present‑or‑else logic.
In Java development, the ubiquitous if...else... pattern often clutters code; the article shows how Java 8's functional interfaces can replace these patterns.
Function functional interface
Mark an interface with @FunctionalInterface to indicate it contains a single abstract method. The main types are Supplier (no arguments, returns a value), Consumer (accepts an argument, returns void), Runnable (no arguments, no return), and Function (accepts an argument and returns a value).
Function can be seen as a transformation function.
Example 1 – Handling exceptions with a custom functional interface
/**
* 抛异常接口
*/
@FunctionalInterface
public interface ThrowExceptionFunction {
/**
* 抛出异常信息
* @param message 异常信息
*/
void throwMessage(String message);
}Utility method that returns the interface implementation and throws an exception when the condition is true:
public static ThrowExceptionFunction isTure(boolean b) {
return (errorMessage) -> {
if (b) {
throw new RuntimeException(errorMessage);
}
};
}Usage: call throwMessage with the error text; if the parameter is false the method executes normally.
Example 2 – Branch handling with two Runnable actions
/**
* 分支处理接口
*/
@FunctionalInterface
public interface BranchHandle {
/**
* 分支操作
* @param trueHandle 为true时的操作
* @param falseHandle 为false时的操作
*/
void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle);
}Factory method returning the interface implementation:
public static BranchHandle isTureOrFalse(boolean b) {
return (trueHandle, falseHandle) -> {
if (b) {
trueHandle.run();
} else {
falseHandle.run();
}
};
}Calling code passes two lambdas (or method references) to execute the appropriate branch.
Example 3 – Present‑or‑else handling for nullable values
/**
* 空值与非空值分支处理
*/
public interface PresentOrElseHandler
{
/**
* 值不为空时执行消费操作,值为空时执行其他操作
*/
void presentOrElseHandle(Consumer
action, Runnable emptyAction);
}Factory method that creates the handler based on a String parameter:
public static PresentOrElseHandler
isBlankOrNoBlank(String str) {
return (consumer, runnable) -> {
if (str == null || str.length() == 0) {
runnable.run();
} else {
consumer.accept(str);
}
};
}Clients invoke presentOrElseHandle with a Consumer for the non‑empty case and a Runnable for the empty case.
Overall, the article demonstrates that leveraging Java 8 functional interfaces can dramatically simplify code by removing repetitive conditional blocks and improving readability.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.