How to use BooleanSupplier in lambda expression in Java?

Java 8Object Oriented ProgrammingProgramming

BooleanSupplier is a functional interface defined in the "java.util.function" package. This interface can be used as an assignment target for a lambda expression or method reference. BooleanSupplier interface has only one method getAsBoolean() and returns a boolean result, true or false.


public interface BooleanSupplier {
   boolean getBoolean();


import java.util.function.BooleanSupplier;

public class BooleanSupplierLambdaTest {
   public static void main(String[] args) {
      BooleanSupplier Obj1 = () -> true;
      BooleanSupplier Obj2 = () -> 5 < 50; // lambda expression
      BooleanSupplier Obj3 = () -> "".equals("");
      System.out.println("Result of Obj1: " + Obj1.getAsBoolean());
      System.out.println("Result of Obj2: " + Obj2.getAsBoolean());
      System.out.println("Result of Obj3: " + Obj3.getAsBoolean());


Result of Obj1: true
Result of Obj2: true
Result of Obj3: false
Published on 03-Jan-2020 14:57:04