How to use BooleanSupplier in lambda expression in Java?


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

Updated on: 13-Jul-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements