Eliminate if…else Chaos in Java 8 with Functional Interfaces
Learn how to replace repetitive if…else statements in Java by leveraging Java 8’s functional interfaces—Supplier, Consumer, Runnable, and Function—through clear examples, custom interfaces, and utility methods that streamline exception handling, branch processing, and null checks for cleaner, more maintainable code.
In Java development, excessive use of if...else for exception throwing and branching makes code ugly.
Java 8 introduces functional interfaces that can replace such patterns.
Functional Interface Annotation
Mark an interface with @FunctionalInterface to indicate it contains a single abstract method. Typical functional interfaces include Supplier (no arguments, returns a value), Consumer (accepts one argument, returns void), Runnable (no arguments, no return), and Function (accepts one argument and returns a value).
Function can be seen as a transformation function.
Supplier Example
Supplier provides data without parameters.
Consumer Example
Consumer consumes a value without returning.
Runnable Example
Runnable has neither parameters nor return value.
Custom Functional Interfaces
Define a functional interface to throw an exception:
@FunctionalInterface
public interface ThrowExceptionFunction {
void throwMessage(String message);
}Utility method returning this interface:
public static ThrowExceptionFunction isTrue(boolean b) {
return errorMessage -> {
if (b) {
throw new RuntimeException(errorMessage);
}
};
}Usage example: VUtils.isTrue(condition).throwMessage("Error"); Define a branch handling interface:
@FunctionalInterface
public interface BranchHandle {
void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle);
}Utility method for branch handling:
public static BranchHandle isTrueOrFalse(boolean b) {
return (trueHandle, falseHandle) -> {
if (b) {
trueHandle.run();
} else {
falseHandle.run();
}
};
}Define an interface for present‑or‑else handling:
public interface PresentOrElseHandler<T> {
void presentOrElseHandle(Consumer<? super T> action, Runnable emptyAction);
}Utility method that returns the above interface:
public static PresentOrElseHandler<String> isBlankOrNoBlank(String str) {
return (consumer, runnable) -> {
if (str == null || str.length() == 0) {
runnable.run();
} else {
consumer.accept(str);
}
};
}Illustrative screenshots demonstrate invoking these utilities.
Conclusion
Java 8 functional interfaces are a powerful feature; using them effectively can greatly simplify code and improve readability.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
