Predicate Functional Interface In Java 8

Predicate Interface is a Functional Interface whose functional method is test(Object). A Predicate takes an Object of any type and returns a boolean value. As this is too a Functional Interface and can therefore be used as the assignment target for a lambda expression or method reference. We also have BiPredicate Interface in Java 8, which takes two parameters instead of one and returns boolean value.

Note: Predicate Interface is used in filtering step of Map Filter Reduce algorithm. 

Predicate Interface Signature:

The Predicate interface test method takes an object of type T and return a boolean value.

Let’s look a simple example of Predicate Interface

As Predicate test method takes argument , our corresponding lambda expression takes an employee type as an argument. Predicate test method returns a boolean value so as our corresponding lambda expression statement employee.getAge() > 30 returns a boolean value and it can filter out all the Employee whose age is greater than 30 Years in a list of Employee.

1)  Simple Predicate Example

Output :

2) Chaining Predicate with the AND boolean operation Example 

To perform Boolean AND operation, we are using Predicate interfaces provided and() method, which is default method in Predicate Interface, as we already having a abstract method test() in Predicate Interface. For above example as add() method is default method in Predicate Interface, its implementation comes from there.

Output:

Now If we want to define our own Predicate Interface  and our own add()  method implementation (as it is default method, it should have its own implementation in that interface) for above programme. As by seeing above programme code Predicate<String> p3 = p1.and(p2), we know our add method should be default method, not a abstract or static one in Predicate Interface. We further know that add() method will take an Predicate Object as parameter and will return an Predicate Object of same type. So we can write our new Predicate Interface as below:

The above code will produce same output as earlier code. The important thing is to understanding the add() method return statement. We want to achieve AND boolean operation on two Predicate Type Object p1.and(p2), so p1 will call its own test() method, which return type is a boolean value, and p2 will do its own test() method call which return type is again boolean value, finally we are using short circuit && operation on both the result.

3) Chaining Predicate with the OR boolean operation Example 

To perform Boolean OR operation, we are using Predicate interfaces provided or() method, which is default method in Predicate Interface.

Output of the above programme. As this is boolean OR operation, so our output will come true in this case too.

Now If we want to define our own Predicate Interface  having own or()  method implementationfor above programme. By seeing programme line Predicate<String> p3 = p1.or(p2), we know our or method should be default method, not a abstract or static one in Predicate Interface. We further know that or() method will take an Predicate Object as parameter and will return an Predicate Object of same type. So we can write our new Predicate Interface as below:

The above code will produce same output as earlier code.

Note 2: The default method and static method can be added to any type of interfaces weather it is Functional Interface or not, and it gives us new opportunities for building new API for our old legacy code as well as for new API for our application.

 

Leave a Reply

Your email address will not be published. Required fields are marked *