Java 8 BooleanSupplier Example

In this blog post, I will be explaining how the Java 8 functional interface BooleanSupplier works. To know more about functional interfaces, you can refer this blog post.

Edit

The  BooleanSupplier interface provides a method called getAsBoolean. This method does not accept any arguments. It returns a boolean value. The BooleanSupplier interface is a specialization of the Supplier interface that returns a boolean. To see an example of the Supplier interface, refer to this blog post.

BooleanSupplier example

Consider the following code snippet:

public class BooleanSupplierExample {public static void main(String[] args) {BooleanSupplier booleanSupplier = () -> true;System.out.println("Boolean flag = "+booleanSupplier.getAsBoolean());}}

Here, we have implemented the getAsBoolean method using a lambda expression. This simply returns the value true. So when this code is executed, it will print the following output:

Boolean flag = true

You can get the source code for this example along with other code for other Java 8 examples at the Github repository here.

Comments

Popular posts from this blog

How to use logging in SpringBoot with code samples

Python While Loop with code samples

How to convert a List to a Set