Predicate Functional Interface

Predicate Functional Interface

In this tutorial, we will discuss Predicate Functional Interface in Java. The term “predicate” implies a statement that evaluates whether a value is true or false.

Predicate Functional Interface
  • A predicate is a function with a single argument and returns a boolean value.
  • To implement predicate functions in Java, Oracle introduced the Predicate interface in the 1.8 version (i.e., Predicate<T>).
  • Predicate Functional interface present in java.util.function package.
  • It is a functional interface and it contains only one method i.e., test().
  • The prototype of the predicate interface in Java is like this “Predicate” where “T” is denoted as a single parameter producing a Boolean result.
interface Predicate<T> {
   public boolean test(T t);
}

As the predicate functional interface is a functional interface and hence it can refer to lambda expression.

The predicate functional interface in Java is a savior to programmers for making and creating codes in more clean and readable formats. It helps in making the test case formatting better and enhances the test cases.

Example

1. Write a predicate to check whether the given integer is greater than 100 or not.

public boolean test(Integer i) { 
   if (i >100) {
      return true;
   } else {
      return false;
   }
}

// This can be written as
(Integer i) -> {
   if(i > 100)
      return true;
   else
      return false;
}

// This can be written as,
i -> (i>100);

// This can be written as,
Predicate<Integer> p = i -> (i >100);
System.out.println (p.test(150)); // true
System.out.println (p.test(50)); // false

Program

import Java.util.function.*;
/**
 * 
 * @author ashok.mariyala
 *
 */
class Test {
   public static void main(String[] args) {
      Predicate<Integer> p = i -> (i >100);
      System.out.println (p.test(150));
      System.out.println (p.test(50)); 
   }
}

Output

true
false

2. Predicate to check the length of the given string is greater than 5 or not.

import Java.util.function.*;
/**
 * 
 * @author ashok.mariyala
 *
 */
class Test {
    public static void main(String[] args) {
       Predicate<String> p = s -> (s.length() > 5);
       System.out.println (p.test(“Ashok Kumar”)); // true
       System.out.println (p.test(“Vinod”)); // false
   }
}

3. Predicate to check whether the given collection is empty or not.

Predicate<Collection> p = c -> c.isEmpty();
Predicate joining

It’s possible to join predicates into a single predicate by using the following methods.

  • and()
  • or()
  • negate()

these are exactly the same as logical AND, OR complement operators.

import java.util.function.*; 
/**
 * 
 * @author ashok.mariyala
 *
 */
class PredicateJoinTest { 
   public static void main(String[] args) {
      int[] x = {0, 5, 10, 15, 20, 25, 30}; 
      Predicate<Integer> p1 = i -> i>10;
      Predicate<Integer> p2 = i -> i%2==0;
      System.out.println("The Numbers Greater Than 10 :");
      m1(p1, x);
 
      System.out.println("The Even Numbers Are:");
      m1(p2, x);
   
      System.out.println("The Numbers Not Greater Than 10 :");
      m1(p1.negate(), x);
   
      System.out.println("The Numbers Greater Than 10 And Even Are :"); 
      m1(p1.and(p2), x);
   
      System.out.println("The Numbers Greater Than 10 OR Even : ");
      m1(p1.or(p2), x);
   }
   
   public static void m1(Predicate<Integer> p, int[] x) {
      for(int x1:x) {
         if(p.test(x1)) {
            System.out.println(x1);
         }
      }
   }
}

Output

The Numbers Greater Than 10 :
15
20
25
30
The Even Numbers Are:
0
10
20
30
The Numbers Not Greater Than 10 :
0
5
10
The Numbers Greater Than 10 And Even Are :
20
30
The Numbers Greater Than 10 OR Even : 
0
10
15
20
25
30

Display names starts with ‘A’ by using Predicate

package com.ashok.java8.predicate;

import java.util.function.Predicate;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class PredicateNamesTest {
   public static void main(String[] args) {
      String[] names = { "Ashok", "Ajay", "Vinod", "Dillesh", "Srinu","Amrutha" };
      Predicate<String> startsWithA = s -> s.charAt(0) == 'A';
      System.out.println("The Names starts with A are:");
      for (String s : names) {
         if (startsWithA.test(s)) {
            System.out.println(s);
         }
      }
   }
}

Output

The Names starts with A are:
Ashok
Ajay
Amrutha

User Authentication by using Predicate

package com.ashok.java8.predicate;

import java.util.Scanner;
import java.util.function.Predicate;
/**
 * 
 * @author ashok.mariyala
 *
 */
class User {
   String username;
   String pwd;

   User(String username, String pwd) {
      this.username = username;
      this.pwd = pwd;
   }
}

public class AuthenticationTest {
   public static void main(String[] args) {
      try(Scanner scan = new Scanner(System.in)) {
         Predicate<User> p = u -> u.username.equals("ashok_mariyala") && u.pwd.equals("Ashok@12345");
         System.out.println("Enter User Name:");
         String username = scan.next();
         System.out.println("Enter Password:");
         String pwd = scan.next();
         User user = new User(username, pwd);
         if (p.test(user)) {
            System.out.println("Valid user and can available all services");
         } else {
            System.out.println("invalid user you cannot available services");
         }
      } 
   }
}

That’s all about the Predicate Functional Interface in Java 8 Features. If you have any queries or feedback, please write us at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!

Predicate Functional Interface
Scroll to top