Master Java 8 Functional Interfaces: Simplify If‑Else with Supplier, Consumer & Runnable

This article explains how Java 8 functional interfaces such as Supplier, Consumer, Runnable and custom @FunctionalInterface types can replace verbose if‑else and exception handling code, providing clear examples, utility methods, and branch‑handling patterns for cleaner backend development.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Master Java 8 Functional Interfaces: Simplify If‑Else with Supplier, Consumer & Runnable

Function Functional Interface

Java 8 introduces @FunctionalInterface to mark interfaces that contain a single abstract method, enabling lambda expressions. Common built‑in functional interfaces include Supplier (no arguments, returns a value), Consumer (accepts an argument, no return), and Runnable (no arguments, no return).

Supplier – supply a value

A Supplier provides data without needing input parameters.

Consumer – consume a value

A Consumer processes an input argument without returning a result.

Runnable – execute a task

A Runnable runs code without parameters or a return value.

Tip: Handling Exceptions in if Statements

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. Create a utility method

public static ThrowExceptionFunction isTrue(boolean b) {
    return (errorMessage) -> {
        if (b) {
            throw new RuntimeException(errorMessage);
        }
    };
}

3. Use the method

Call throwMessage with an error message; the exception is thrown only when the condition is true.

Branch Handling with Functional Interfaces

1. Define a branch‑handling interface

/**
 * Branch handling interface
 */
@FunctionalInterface
public interface BranchHandle {
    /**
     * Execute true or false handling logic
     * @param trueHandle  operation when condition is true
     * @param falseHandle operation when condition is false
     */
    void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle);
}

2. Implement the utility method

public static BranchHandle isTrueOrFalse(boolean b) {
    return (trueHandle, falseHandle) -> {
        if (b) {
            trueHandle.run();
        } else {
            falseHandle.run();
        }
    };
}

3. Use the method

Pass two Runnable instances; the appropriate one runs based on the boolean value.

Present‑Or‑Else Handling

1. Define the interface

/**
 * Handle present or empty values
 */
public interface PresentOrElseHandler<T> {
    /**
     * Execute action when value is present, otherwise run emptyAction
     * @param action      Consumer for non‑null value
     * @param emptyAction Runnable for null/empty value
     */
    void presentOrElseHandle(Consumer<? super T> action, Runnable emptyAction);
}

2. Implement utility method

public static PresentOrElseHandler<String> isBlankOrNoBlank(String str) {
    return (consumer, runnable) -> {
        if (str == null || str.isEmpty()) {
            runnable.run();
        } else {
            consumer.accept(str);
        }
    };
}

3. Use the method

Provide a Consumer to process a non‑empty string and a Runnable for the empty case.

Conclusion

Java 8 functional interfaces are powerful tools that can dramatically simplify conditional logic, exception handling, and branch processing, leading to cleaner and more maintainable backend code.

Supplier illustration
Supplier illustration
Consumer illustration
Consumer illustration
Runnable illustration
Runnable illustration
BranchHandle illustration
BranchHandle illustration
PresentOrElseHandler illustration
PresentOrElseHandler illustration
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaLambdaFunctional Interfacejava8
Java Backend Technology
Written by

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!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.