Java 8 Predicate Example

Java 8 Predicate

1. Introduction

Java 8 predicate is functional interface introduced in java 8. This feature is a part of the java.util.function package, which is dedicated to functional interfaces. The primary goal of using predicates is to filter or match objects based on specific criteria.

For example, if we have a box of apples and we want to pick out only the red ones, we could use a Predicate to check each apple and see if it’s red. If the answer is “yes,” we keep the apple; if it’s “no,” we put it back. This makes it easier to work with lists of things in our programs, allowing us to sort, filter, and choose items based on whatever rules we set up.

2. Understanding the Predicate

A Predicate is a single-argument functional interface that returns a boolean value, either true or false. It accepts one argument and yields a result as either true or false.

Here is the definition of Predicate interface.

3. Predicate Methods Example

There are multiple methods in Predicate interface. Let’s go through them one by one.

3.1 test()

This is abstract method of Predicate interface. It evaluates true if predicate matches the input argument.

Here is simple example of test() method which checks if input argument is greater than 100 or not.

Output:

is 200 greater than 100: true

You can pass Predicate as a function argument too.

3.2 and()

The and() is a default method that returns a composite predicate, denoting the logical AND of this predicate and the one passed as an argument.

Output:

200 lies between 100 and 300: true

We have created two predicates and checked the logical AND between predicate1 and predicate2.

3.3 or()

The or() is a default method that returns a composite predicate, indicating the logical OR of this predicate and the one passed as an argument.

Output:

(30 > 100) or (30 < 50) returns: true

We have created two predicates and checked logical or between predicate1 and predicate2

3.4 negate()

The negate() method is a default method that returns a predicate, representing the logical negation of this predicate.

Output:

30 is less than 100: true

We have used negate() method to change predicate(integer greater than 100) to negatePredicate (integer lesser than 100).

3.5 isEqual()

The isEqual() is astatic method that returns a predicate, determining if two arguments are equal based on the object’s equals() method. This method is particularly useful when we need to evaluate objects in a functional style, for instance, when filtering a collection.

Imagine we have a list of strings and we want to find which ones are equal to a given string, say “Java”. We can use the isEqual() method to create a predicate for this comparison and then apply it to our list.

Output:

[Java, Java]

4. Filtering List Using Predicate

In Java 8, the stream’s filter method accepts a Predicate as an argument, enabling us to filter lists using predicates. This mechanism allows for the application of specific conditions to streamline the process of selecting elements from collections based on our defined criteria, enhancing both the functionality and readability of our code.
Lets say you have student class as below:

Lets create a function which will be used to filter students based on predicates:

Lets create a main class as below:

When you run above program, you will get below output:

Male students having age > 18 are :[Student [id=1, name=Arpit, gender=M, age=19], Student [id=4, name=Martin, gender=M, age=21]] Female students having age < 18 are :[Student [id=3, name=Mary, gender=F, age=14], Student [id=5, name=Monica, gender=F, age=16]]

5. Conclusion

Predicates in Java 8 offer a robust and flexible mechanism for encapsulating conditional logic. Through the use of lambda expressions and stream operations, predicates enhance the readability and maintainability of code, particularly when dealing with collection filtering and matching operations. When used wisely, especially with an understanding of their performance implications in different scenarios, predicates can be a powerful tool in any Java developer’s toolkit.

Was this post helpful?

Leave a Reply

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