Java : BooleanSupplier with Examples

BooleanSupplier (Java SE 21 & JDK 21) with Examples.
You will find code examples on most BooleanSupplier methods.


Summary

Represents a supplier of boolean-valued results. This is the boolean-producing primitive specialization of Supplier.

Class diagram

public void test(BooleanSupplier supplier) {
    System.out.println("getAsBoolean = " + supplier.getAsBoolean());
}
final var supplier = new BooleanSupplier() {
    @Override
    public boolean getAsBoolean() {
        return false;
    }
};

test(supplier);  // getAsBoolean = false

// An example with a lambda expression.
test(() -> true); // getAsBoolean = true

Methods

boolean getAsBoolean ()

Gets a result.

public void test(BooleanSupplier supplier) {
    System.out.println("getAsBoolean = " + supplier.getAsBoolean());
}
final var supplier = new BooleanSupplier() {
    @Override
    public boolean getAsBoolean() {
        return false;
    }
};

test(supplier);  // getAsBoolean = false

// An example with a lambda expression.
test(() -> true); // getAsBoolean = true

Related posts

To top of page