Tuesday, May 23, 2023

Java 8 Predicate Functional Interface Example [Tutorial]

In Java 8, there is a defined functional interface which is called Predicate. The Predicate interface receives an argument and based on specific condition it returns Boolean value. It can be helpful in testing and it’s located in java.util.Function package. This is one of the many pre-defined or built-in functional interface which JDK provides. A couple of important ones are Supplier which can be used to produce a value of T (any object) and Consumer which can consume and project like takes data and print to console. forEach() is a good example of method accepting Consumer interface. Similarly many method accept a Predicate and when they do you can simply pass a lambda which generates a boolean by checking a condition. 

First let’s see an example:


public static void main(String[] args) {

    Predicate<Integer> predicate = n -> (n < 
20);
    
System.out.println(predicate.test(10));
   
}

 

Here, we send to Predicate an integer value of 10. Also, we provide the logic that the argument number which is represented by n should be less than 20 to be true otherwise it returns false. So obviously the Boolean value here is true. Let’s see the output:

 

true

 

 

Here are more examples:

 

public static void main(String[] args) {

    Predicate<Integer> predicate1 = n -> (n > 
20);
    
System.out.println(predicate1.test(10));

    
Predicate<Integer> predicate2 = n -> (n == 10);
    
System.out.println(predicate2.test(5));

    
Predicate<Integer> predicate3 = n -> (n < 10);
    
System.out.println(predicate3.test(2));

}

 

With the output:


 false
 false
 true

 

In the above examples, we are using the Predicate through the method test(). In general, Predicate has five methods: test() , isEqual() , and() , or() , negate() :

 

a-    test(): it returns true if the argument matches the condition otherwise false.

 

b-   isEqual:  returns true if two arguments are equal according to object’s equals() method otherwise false.

 

c-    and(): It is used when there are multiple logic conditions that are checked by the Predicate, it returns true only if all conditions are true otherwise false.

 

d-   or():  It is used when there are multiple logic conditions that are checked by the Predicate, it returns true if one of the conditions is true. If all the conditions are false then it returns false.

e-    negate(): It is contrary to test() method. It returns false if the argument matches the condition or returns true if the argument doesn’t match the condition


Java 8 Predicate Interface Example [Tutorial]

We saw an example for test(), let’s show the example of other methods:

 

v isEqual():

 

public static void main(String[] args) {

    Predicate<String> predic = Predicate.isEqual(
"Hello World");
   
System.out.println(predic.test("Hello World"));
   
System.out.println(predic.test("Hello"));
}

 

 

Output:

true
false

 

 

v and():

 

public static void main(String[] args) {

    Predicate<Integer> predicate1 = n -> (n >
20);
   
Predicate<Integer> predicate2 = n -> (n == 25);

   
Predicate<Integer> andPredicate = predicate1.and(predicate2);
   
System.out.println(andPredicate.test(30));
   
System.out.println(andPredicate.test(25));

}

 

 

Output:

 

 false
 true

 

v or():

 

public static void main(String[] args) {

    Predicate<Integer> predicate1 = n -> (n >
20);
   
Predicate<Integer> predicate2 = n -> (n == 25);

   
Predicate<Integer> andPredicate = predicate1.or(predicate2);
   
System.out.println(andPredicate.test(30));
   
System.out.println(andPredicate.test(25));
   
System.out.println(andPredicate.test(15));

}

 

Output:

 

true
true
false

 

v negate():

 

public static void main(String[] args) {

    Predicate<Integer> predicate1 = i -> i >
15;

   
Predicate<Integer> negate = predicate1.negate();

   
System.out.println(negate.test(30));
   
System.out.println(negate.test(10));

}

 

Output:

 

false
true

 

No comments:

Post a Comment