Using Java 8 Functional Interfaces to Replace if…else Statements
This article explains how to leverage Java 8's @FunctionalInterface, including Supplier, Consumer, Runnable, and Function, to eliminate repetitive if…else branches, handle exceptions, and simplify branch logic with concise lambda‑based utilities.
During development, excessive if...else... statements for exception throwing and branch handling can make code look messy; Java 8's Function interface can be used to eliminate these repetitive patterns.
Function Functional Interface
By annotating an interface with @FunctionalInterface , you declare it as a functional interface that contains exactly one abstract method. Common 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 regarded as a conversion function.
Supplier (Supply‑type function)
A Supplier does not accept parameters and only returns data.
Consumer (Consume‑type function)
A Consumer is the opposite of Supplier: it receives one argument and returns nothing.
Runnable (No‑arg, no‑return function)
Runnable represents a function with neither parameters nor a return value.
Function is a special form that accepts one argument and returns a value; Supplier, Consumer, and Runnable can be seen as particular variants of Function.
Tip: Handling if that Throws an Exception
1. Define a functional interface
/**
* Throw exception interface
*/
@FunctionalInterface
public interface ThrowExceptionFunction {
/**
* Throw an exception with the given message
* @param message exception message
*/
void throwMessage(String message);
}2. Write a helper method
/**
* Returns a ThrowExceptionFunction that throws when the parameter is true
* @param b condition
* @return ThrowExceptionFunction
*/
public static ThrowExceptionFunction isTrue(boolean b) {
return (errorMessage) -> {
if (b) {
throw new RuntimeException(errorMessage);
}
};
}3. Usage
Call the utility method and then invoke throwMessage with the desired error text; the exception is thrown only when the supplied boolean is true.
Handling if‑branch operations
1. Define a functional interface
/**
* Branch handling interface
*/
@FunctionalInterface
public interface BranchHandle {
/**
* Execute different actions for true/false
* @param trueHandle action when condition is true
* @param falseHandle action when condition is false
*/
void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle);
}2. Write a helper method
/**
* Returns a BranchHandle that runs the appropriate Runnable based on the boolean
* @param b condition
* @return BranchHandle
*/
public static BranchHandle isTrueOrFalse(boolean b) {
return (trueHandle, falseHandle) -> {
if (b) {
trueHandle.run();
} else {
falseHandle.run();
}
};
}3. Usage
When the parameter is true, trueHandle runs; otherwise falseHandle runs.
If value present, execute consumer; otherwise execute runnable
1. Define a functional interface
/**
* Handles present or empty values
*/
public interface PresentOrElseHandler
{
/**
* Execute consumer when value is present, otherwise run the fallback action
* @param action consumer for non‑null value
* @param emptyAction runnable for null/empty value
*/
void presentOrElseHandle(Consumer
action, Runnable emptyAction);
}2. Write a helper method
/**
* Returns a handler that distinguishes between blank and non‑blank strings
* @param str the string to test
* @return PresentOrElseHandler
*/
public static PresentOrElseHandler
isBlankOrNoBlank(String str) {
return (consumer, runnable) -> {
if (str == null || str.length() == 0) {
runnable.run();
} else {
consumer.accept(str);
}
};
}3. Usage
Call the utility, then invoke presentOrElseHandle with a Consumer for the non‑empty case and a Runnable for the empty case.
Conclusion
The Function functional interface is a core feature of Java 8; using it effectively can greatly simplify code by removing repetitive if…else structures.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.