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.

Syntax

@FunctionalInterface
public interface BooleanSupplier {
   boolean getBoolean();
}

Example

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 = () -> "tutorialspoint.com".equals("tutorix.com");
      System.out.println("Result of Obj1: " + Obj1.getAsBoolean());
      System.out.println("Result of Obj2: " + Obj2.getAsBoolean());
      System.out.println("Result of Obj3: " + Obj3.getAsBoolean());
   }
}

Output

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