What is Predicate Functional Interface in Java 8

In implementing Lambda Expression we need a Functional Interface that’s why a Predefined Function Interface is given by Java People from Java 8 onwards. This is available in the “java.util.function” package. A predefined Functional Interface is making Lambda Expression used in a common coding activity. Predicate Predefined  Functional Interface is used for conditional check and returns a  boolean result as an output.

Prototype:

interface Predicate<T>{
public boolean test(T t);

}

T – It is the type of input to the predicate.

Example:

import java.util.function.Predicate;

public class Test {

  public static void main(String[] args) {

    Predicate < Integer > p = i -> i % 2 == 0;

    System.out.println("Is No. Even: " + p.test(10));

    System.out.println("Is No. Even: " + p.test(15));

    System.out.println("Is No. Even: " + p.test(20));

    System.out.println("Is No. Even: " + p.test(5));

  }
}
  • In the above example, we can check if the input number is even or not by using Predicate.
  • See the lines of code are reduced than without implementation of Lambda expression.

Output:

Is No. Even: true
Is No. Even: false
Is No. Even: true
Is No. Even: false

Example: 

import java.util.function.Predicate;

public class Test {

  public static void main(String[] args) {

    String[] s = {
      "Ramesh",
      "Ravi",
      "Raju",
      "Raghav",
      "Dinesh"
    };

    Predicate < String > p = s1 -> s1.length() > 5;

    for (String name: s) {

      if (p.test(name)) {

        System.out.println(name);

      }
     }
   }
}
  • In the above example, we can check String length is greater than five by using Predicate.

Output:

Ramesh
Raghav
Dinesh

Example: 

package test;

import java.util.ArrayList;

import java.util.List;

import java.util.function.Predicate;

class Student {

  String name;

  int marks;

  public Student(String name, int marks) {

    this.name = name;

    this.marks = marks;

  }

}

public class Test {

  public static void main(String[] args) {

    List < Student > listStudnt = new ArrayList < Student > ();

    listStudnt.add(new Student("Raj", 70));

    listStudnt.add(new Student("Vineet", 60));

    listStudnt.add(new Student("Raghav", 40));

    listStudnt.add(new Student("Sham", 30));

    listStudnt.add(new Student("Dinesh", 50));

    Predicate < Student > p = stu -> stu.marks > 40;

    for (Student stdnt: listStudnt) {

      if (p.test(stdnt)) {

        System.out.println("Name: " + stdnt.name + " Marks: " + stdnt.marks);

      }
    }
  }
}
  • In the above example, we can use Predicate in our class.
  • We also achieve this task through an “ if ” conditional check. But in future we are required to check more conditions then we have to perform more time in the “ if ” conditional check that’s why it is better to use Predicate and save all conditions into Predicate.

Output:

Name: Raj Marks: 70
Name: Vineet Marks: 60
Name: Dinesh Marks: 50

If we want to check whether the number is even or not and also check whether the number is greater than 10. In this type of conditional checking by using a Predicate we need two Predicates like
p1—>checking for an even number
p2—>checking for numbers greater than 10
We achieve this from Predicate Joining like:
p1.and(p2).test(20)

Predicate Joining:

Multiple Predicates are combined to check complex conditional expressions. This is called Predicate Joining.

  • p1.and(p2)————Satisfies both conditions
  • p1.or(p2)—————Satisfies one condition or other
  • p1.negate()————–Opposite of p1

Example:

import java.util.function.Predicate;

public class Test {

public static void main(String[] args) {

int[] n = { 6, 8, 30, 25, 30, 45, 40, 50 };

Predicate<Integer> p1 = i -> i % 2 == 0;

Predicate<Integer> p2 = i -> i >=10;

System.out.println("The numbers are:");

for (int n1 : n) {

if (p1.and(p2).test(n1)) {

System.out.println(n1);
     }
  }
 }
}
  • In the above example, we can check whether the number is even and greater than or equal to 10  by using Predicate Joining.

Output:

The numbers are:
30
30
40
50

Example: 

import java.util.function.Predicate;

public class Test {

public static void main(String[] args) {

int[] n = {5,6, 8, 25, 30, 45, 40, 50 };

Predicate<Integer> p1 = i -> i % 2 == 0;

Predicate<Integer> p2 = i -> i >=10;

System.out.println("The numbers are:");

for (int n1 : n) {

if (p1.or(p2).test(n1)) {

System.out.println(n1);

}
}
}
}
  •  In the above example, we can check whether the number is even or greater than equal to 10  by using Predicate Joining.

Output:

The numbers are:
6
8
25
30
45
40
50

Example: 

import java.util.function.Predicate;

public class Test {

public static void main(String[] args) {

int[] n = {5,6, 8, 25, 30, 45, 40, 50 };

Predicate<Integer> p1 = i -> i % 2 == 0;

System.out.println("The numbers which are not even:");

for (int n1 : n) {

if (p1.negate().test(n1)) {

System.out.println(n1);

}
}
}
}
  • In the above example, we can check the number is not even by using Predicate Joining.

Output:

The numbers which are not even:
5
25
45

Conclusion:

This topic is explained What is Predicate? How to use Predicate? What is Predicate Joining? How to use Predicate Joining?

Leave a Comment