Recent Posts

Tuesday, 30 October 2018

Predicate Functional Interface


☀ A predicate is a function with a single argument and returns boolean value.
☀ To implement predicate functions in Java, Oracle people introduced Predicate interface in 1.8 version (i.e.,Predicate<T>).
☀ Predicate interface present in Java.util.function package.
☀ It is a functional interface and it contains only one method i.e., test().
interface Predicate<T> {
   public boolean test(T t);
}
     As predicate is a functional interface and hence it can refers lambda expression.
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 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.
1. and()
2. or()
3. negate()
these are exactly 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;

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");
         }
      } 
   }
}

Next Tutorial : Function Functional Interface

 Previous Tutorial : Static methods inside interface
 

No comments:

Post a Comment