DEV Community

Cover image for Using Predicate from the Functional Interface Package in Java
omoluabidotcom
omoluabidotcom

Posted on

Using Predicate from the Functional Interface Package in Java

The advent of Java 8 brought about java.util.function package in Java. It's a package that contains defined functional interfaces usually used with Lambda Expression.

I'm assuming that you have basic knowledge of Lambda Expression if not go ahead and read this article here and then come back to this one.

This is the first of a series of articles I will be publishing on how to make use of Functional Interfaces from the package.

Let's get started!!!

Predicate

Predicate according to JDK 1.8 documentation "represents a predicate(Boolean-valued function) of one argument"; what this refers to basically is that you make use of Predicate whenever you need to check certain conditions that need to return a Boolean.

Predicate Interface has five methods, a single abstract method as expected; three default methods, and a static method.

Methods from the Predicate Interface

test() method

test() method is also referred to as a functional method of the functional interface Predicate. test() method takes in a single argument evaluates this predicate on the given argument and returns a Boolean.

Excerpt from documentation

Sample code


// predicate interface type assigned to a field
Predicate<Integer> p = n -> (n>20);
// calling the functional method test on the predicate field p
System.out.println(p.test(40)); // output = true

Enter fullscreen mode Exit fullscreen mode

Above you see we created a field p of type Predicate. We assigned a lambda expression that checks if the argument passed is greater than 20 to the field p. When the functional method test() is called on any of the methods the parameter passed in is inferred and used in place of n from the lambda expression, in the case (i.e. the example) the answer is true since 40 is greater than 20.

and() method

One of the three default methods from the Predicate Interface is and() method. It takes in one argument of type Predicate while the other is chained with the and() method using the dot operator like so.


// syntax to use and() method
P1.and(p2);

Enter fullscreen mode Exit fullscreen mode

and() method "returns a composed predicate that represents a shorts-circuiting logical AND of his predicate and another". What this simply means is that and() method evaluates two predicates by combining their result together using the AND logical expression to get an output.
While evaluating if any of the predicates is false, then the other is not evaluated as both have to be true; therefore in this particular case false is the output.

Excerpt from documentation

Sample code


// array of number
int[] numArray = {2, 5, 10, 20, 30, 40, 45, 47, 50, 56, 60, 63, 70};
// to check if a number is even
Predicate<Integer> p1= n -> (n%2==0);
// to check id number is greater than 55
Predicate<Integer> p2= n -> (n>55);
// looping through number array
for (int num : numArray) {
// using and() method to check for two conditions and calling the functional method test()
if(p1.and(p2).test(num)) {
// print numbers that are even and greater than 55
System.out.println(num);
}
}

Enter fullscreen mode Exit fullscreen mode

or() method

or() method in many ways is similar to and() method. It takes in one argument of type Predicate while the other is combine with it using the dot operator like so.


// syntax to use or() method
p1.or(p2);

Enter fullscreen mode Exit fullscreen mode

According to Java 8 documentation "It returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.
What this simply means is that or() method evaluates predicates by combining their result to give an output.
While evaluating the Predicate using the or method if the first gives a true the other one is not evaluated since or() only needs one of the logic to be true.

Excerpt from documentation

Sample code


// array of numbers
int[] numArray = {2, 5, 10, 20, 30, 40, 45, 47, 50, 56, 60, 63, 70};
// to check if a number is even
Predicate<Integer> p1= n -> (n%2==0);
// to check id number is greater than 55
Predicate<Integer> p2= n -> (n>55);
// looping through array
for (int num : numArray) {
// using or() method to check for two conditions and calling the functional method test()
if(p1.or(p2).test(num)) {
// print numbers that are either even or greater than 55
System.out.println(num);
}
}

Enter fullscreen mode Exit fullscreen mode

negate() method

negate() method returns the opposite of the Predicate result, negate() is straightforward. According to documentation "Returns a predicate that represents the logical negation of this predicate"

Sample code


// array of numbers
int[] numArray = {2, 5, 10, 20, 30, 40, 45, 47, 50, 56, 60, 63, 70};
// to check if a number is even
Predicate<Integer> p1= n -> (n%2==0);
// to check id number is greater than 55
Predicate<Integer> p2= n -> (n>55);
// looping through array
for (int num : numArray) {
// using negate() method get negation of two conditions and calling the functional method test()
if(p1.negate(p2).test(num)) {
// print numbers that are neither even nor greater than 55
System.out.println(num);
}
}

Enter fullscreen mode Exit fullscreen mode

isEqual() method

This method function like the Objects.equals(Object, Object) method to compare two objects and check if they are equal. Just like the rest of the method apart from the functional method test. isEqual() method returns a predicate that represents if the two objects are equal or not.

Excerpt from documentation

Sample code


// predicate type field p1 using the isEqual() method
Predicate<String> p1 = Predicate.isEqual("Prince");
// invocating the isEqual() using the functional method test()
System.out.println(p1.test("Prince"));

Enter fullscreen mode Exit fullscreen mode

Summary

Predicate Interface has five methods including the functional method (i.e. test() method) which is the single abstract method, whose function is to invoke any method used from the Predicate interface.
Other methods like or() perform OR logic, and() method perform AND logic, negate() method perform NOT logic and isEqual() method compare two Predicate object and give a Boolean output which reflects the object equality.

Thank you for your time and catch me on the next one, connect with me on LinkedIn.

Credit

Top comments (0)