How do I use the BooleanSupplier functional interface in Java?

The BooleanSupplier interface in Java is a functional interface introduced in Java 8 as part of the java.util.function package. It is used as a supplier of boolean values, meaning it provides a single method to return a boolean value without taking any input parameters.

Here’s how we can use the BooleanSupplier interface:

Key Features:

  • Functional Interface: It has a single abstract method:
boolean getAsBoolean();
  • Commonly used in lambda expressions or method references when we need a function to produce (supply) a boolean value.

Example 1: Simple BooleanSupplier with a Lambda Expression

Here’s a simple example where the BooleanSupplier returns a true value:

package org.kodejava.util.function;

import java.util.function.BooleanSupplier;

public class BooleanSupplierExample {
    public static void main(String[] args) {
        BooleanSupplier alwaysTrue = () -> true;

        // Output: true
        System.out.println("BooleanSupplier result: " + alwaysTrue.getAsBoolean());
    }
}

Example 2: BooleanSupplier with Conditional Logic

We can use conditional logic inside the lambda body:

package org.kodejava.util.function;

import java.util.function.BooleanSupplier;

public class ConditionalLogic {
    public static void main(String[] args) {
        int number = 10;

        // A BooleanSupplier that checks if the number is greater than 5
        BooleanSupplier isGreaterThanFive = () -> number > 5;

        // Execute the BooleanSupplier
        // Output: true
        System.out.println("Is number greater than 5? " + isGreaterThanFive.getAsBoolean());
    }
}

Example 3: BooleanSupplier with Method References

If we already have a method that produces a boolean, we can use it with a method reference:

package org.kodejava.util.function;

import java.util.function.BooleanSupplier;

public class MethodReference {
    public static void main(String[] args) {
        BooleanSupplier isDayTime = MethodReference::checkDayTime;

        System.out.println("Is it daytime? " + isDayTime.getAsBoolean());
    }

    // A method that checks if the current hour is during 
    // daytime (6 AM - 6 PM)
    private static boolean checkDayTime() {
        int hour = java.time.LocalTime.now().getHour();
        // True if the hour is between 6 and 18
        return hour >= 6 && hour < 18;
    }
}

Example 4: Reusable Suppliers in Applications

BooleanSupplier can be used for reusable checks, like ensuring a certain condition is met before running some logic:

package org.kodejava.util.function;

import java.util.function.BooleanSupplier;

public class ReusableCheck {
    public static void main(String[] args) {
        boolean isConnected = false; // Example condition

        // Create a supplier that checks if the system is connected
        BooleanSupplier canProceed = () -> isConnected;

        if (canProceed.getAsBoolean()) {
            System.out.println("Proceed with the operation!");
        } else {
            System.out.println("Cannot proceed, system is not connected.");
        }
    }
}

Use Cases of BooleanSupplier

  1. Conditional Execution: Checking preconditions in a functional and reusable way before executing logic.
  2. Lazy Evaluation: Deferring the evaluation of a condition until it’s actually needed.
  3. Testing Utilities: Can be used in test cases to pass logic or mocks for condition evaluation.
Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.