Java 8 - Predefined Functional Interface - Predicate - Example
Predefined Functional Interface - Predicate:
* Performed some conditional check and produce the output as true or false ?
* Predicate is a boolean value function.
eg. check employee salary >10000000 or not
* Predicate contain the only one abstract method. method is: test()
interface Predicate<T>
{
boolean test(T t);
}
How to use this in our application:
Case 1: Integer Check
public boolean test(Integer I){
if(I >10){
return true;
}else{
return false;
}
}
Using Lambda :
(Integer I) -> {
if(I >10)
return true;
else
return false;
}
or
I -> I >10;
Final Code:
Predicate<Integer> p = I -> I >10;
System.out.println (p.test(100)); // true
System.out.println (p.test(5)); // false
Example Program:
import java.util.function.*; // import java.util.function.Predicate;
Class Test{
public static void main(String args[]){
Predicate<Integer> p = I -> I >10;
System.out.println (p.test(100)); // true
System.out.println (p.test(5)); // false
}
}
Case 2: String length check
Example Program:
import java.util.function.*; // import java.util.function.Predicate;
Class Test{
public static void main(String args[]){
Predicate<String> p = s -> s.length() > 5;
System.out.println (p.test("ABCDEF")); // true
System.out.println (p.test("ABC")); // false
}
}
ArrayList is Empty or not ?
Example Program:
import java.util.function.*; // import java.util.function.Predicate;
Class Test{
public static void main(String args[]){
Predicate<Collection> p = c -> c.isEmpty();
ArrayList<String> list = new ArrayList<>();
System.out.println (p.test(list)); // true
list.add("A");
System.out.println (p.test(list)); // false
}
}
Predicate Joining:
* Join multiple predicate into single predicate
1. Given no >10 or not.
2. Given no even or not.
Note:
Reverse check : p1.negate()
and condition : p1.and(p2)
or condition : p1.or(p2)
Example Program:
Class Test{
public static void m1(Predicate<Integer> p , int[] x)
{
for(int x1 : x)
{
if(p.text(x1)){
System.out.println(x1);
}
}
}
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 are:");
m1(p1,x);
System.out.println("The Even numbers are:");
m1(p2,x);
System.out.println("The numbers not greater than 10 are:");
m1(p1.negate(),x);
System.out.println("The numbers greater than 10 are AND Even Numbers:");
m1(p1.and(p2),x);
System.out.println("The numbers greater than 10 are OR Even Numbers:");
m1(p1.or(p2),x);
}
}
Output:
The numbers greater than 10 are:
15
20
25
30
The Even numbers are:
0
10
20
30
The numbers not greater than 10 are:
0
5
10
The numbers greater than 10 AND Even Numbers:
20
30
The numbers greater than 10 OR Even Numbers:
0
10
15
20
25
30
Write a program to display Name start with "K"
class Test
{
public static void main(String[] args)
{
String[] names={"Sunny","Kajal","Mallika","Katrina","Kareena"};
Predicate<String> startsWithK=s->s.charAt(0)=='K';
System.out.println("The Names starts with K are:");
for(String s: names)
{
if(startsWithK.test(s))
{
System.out.println(s);
}
}
}
}
User Authentication with Predicate:
Class User{
private String username;
private Stirng password;
User(String username, String password){
this.username = username;
this.password = password;
}
}
class Test
{
public static void main(String[] args)
{
Predicate<User> p = u->u.username.equals("vinoth")&& u.pwd.equals("vino123");
Scanner sc= new Scanner(System.in);
System.out.println("Enter User Name:");
String username=sc.next();
System.out.println("Enter Password:");
String pwd=sc.next();
User user=new User(username,pwd);
if(p.test(user))
{
System.out.println("Valid user and can avail all services");
}
else
{
System.out.println("invalid user you cannot avail services");
}
}
}
Software Enigneer is allowed inside the pub or not ?
Condition:
1. He should be more than 18
2. He shoud have the girl friend.
class SoftwareEngineer
{
String name;
int age;
boolean isHavingGf;
SoftwareEngineer(String name,int age,boolean isHavingGf)
{
this.name=name;
this.age=age;
this.isHavingGf=isHavingGf;
}
public String toString()
{
return name;
}
}
class Test
{
public static void main(String[] args)
{
SoftwareEngineer[] list={ new SoftwareEngineer("Durga",60,false),
new SoftwareEngineer("Sunil",25,true),
new SoftwareEngineer("Sayan",26,true),
new SoftwareEngineer("Subbu",28,false),
new SoftwareEngineer("Ravi",17,true)
};
Predicate<SoftwareEngineer> allowed= se -> se.age>= 18 && se.isHavingGf;
System.out.println("The Allowed Members into Pub are:");
for(SoftwareEngineer se : list)
{
if(allowed.test(se))
{
System.out.println(se);
}
}
}
}
Employee Management System Using Predicate:
class Employee
{
String name;
String designation;
double salary;
String city;
Employee(String name,String designation,double salary,String city)
{
this.name=name;
this.designation=designation;
this.salary=salary;
this.city=city;
}
public String toString()
{
String s=String.format("[%s,%s,%.2f,%s]",name,designation,salary,city);
return s;
}
public boolean equals(Object obj)
{
Employee e=(Employee)obj;
if(name.equals(e.name)&&designation.equals(e.designation)&&salary==e.salary && city==e.city)
{
return true;
}
else
{
return false;
}
}
}
class Test
{
public static void main(String[] args)
{
ArrayList<Employee> list= new ArrayList<Employee>();
populate(list);
Predicate<Employee> p1=emp->emp.designation.equals("Manager");
System.out.println("Managers Information:");
display(p1,list);
Predicate<Employee> p2=emp->emp.city.equals("Bangalore");
System.out.println("Bangalore Employees Information:");
display(p2,list);
Predicate<Employee> p3=emp->emp.salary<20000;
System.out.println("Employees whose slaray <20000 To Give Increment:");
display(p3,list);
System.out.println("All Managers from Bangalore city for Pink Slip:");
display(p1.and(p2),list);
System.out.println("Employees Information who are either Managers or salary <2000
display(p1.or(p3),list);
System.out.println("All Employees Information who are not managers:");
display(p1.negate(),list);
Predicate<Employee> isCEO=Predicate.isEqual(new Employee("Durga","CEO",30000,"rabad"));
Employee e1=new Employee("Durga","CEO",30000,"Hyderabad");
Employee e2=new Employee("Sunny","Manager",20000,"Hyderabad");
System.out.println(isCEO.test(e1));//true
System.out.println(isCEO.test(e2));//false
}
public static void populate(ArrayList<Employee> list)
{
list.add(new Employee("Durga","CEO",30000,"Hyderabad"));
list.add(new Employee("Sunny","Manager",20000,"Hyderabad"));
list.add(new Employee("Mallika","Manager",20000,"Bangalore"));
list.add(new Employee("Kareena","Lead",15000,"Hyderabad"));
list.add(new Employee("Katrina","Lead",15000,"Bangalore"));
list.add(new Employee("Anushka","Developer",10000,"Hyderabad"));
list.add(new Employee("Kanushka","Developer",10000,"Hyderabad"));
list.add(new Employee("Sowmya","Developer",10000,"Bangalore"));
list.add(new Employee("Ramya","Developer",10000,"Bangalore"));
}
public static void display(Predicate<Employee> p,ArrayList<Employee> list)
{
for (Employee e: list )
{
if(p.test(e))
{
System.out.println(e);
}
}
System.out.println("**************************************************");
}
}
Ouput:
Managers Information: [Sunny,Manager,20000.00,Hyderabad] [Mallika,Manager,20000.00,Bangalore] ************************************************** Bangalore Employees Information: [Mallika,Manager,20000.00,Bangalore] [Katrina,Lead,15000.00,Bangalore] [Sowmya,Developer,10000.00,Bangalore] [Ramya,Developer,10000.00,Bangalore] ************************************************** Employees whose slaray <20000 To Give Increment: [Kareena,Lead,15000.00,Hyderabad] [Katrina,Lead,15000.00,Bangalore] [Anushka,Developer,10000.00,Hyderabad] [Kanushka,Developer,10000.00,Hyderabad] [Sowmya,Developer,10000.00,Bangalore] [Ramya,Developer,10000.00,Bangalore] **************************************************
All Managers from Bangalore city for Pink Slip: [Mallika,Manager,20000.00,Bangalore] ************************************************** Employees Information who are either Managers or salary <20000 [Sunny,Manager,20000.00,Hyderabad] [Mallika,Manager,20000.00,Bangalore] [Kareena,Lead,15000.00,Hyderabad] [Katrina,Lead,15000.00,Bangalore] [Anushka,Developer,10000.00,Hyderabad] [Kanushka,Developer,10000.00,Hyderabad] [Sowmya,Developer,10000.00,Bangalore] [Ramya,Developer,10000.00,Bangalore] **************************************************
All Employees Information who are not managers: [Durga,CEO,30000.00,Hyderabad] [Kareena,Lead,15000.00,Hyderabad] [Katrina,Lead,15000.00,Bangalore] [Anushka,Developer,10000.00,Hyderabad] [Kanushka,Developer,10000.00,Hyderabad] [Sowmya,Developer,10000.00,Bangalore] [Ramya,Developer,10000.00,Bangalore] ************************************************** true false
Predicate interface isEqual():
* It is a static method in the predicate interface.
Eg.
Class Test{
public static void main(String args[]){
Predicate<String> p = Predicate.isEqual("Vinoth");
System.out.println(p.test("Vinoth")); // true
System.out.println(p.test("Kumar")); // false
}
}
* Performed some conditional check and produce the output as true or false ?
* Predicate is a boolean value function.
eg. check employee salary >10000000 or not
* Predicate contain the only one abstract method. method is: test()
interface Predicate<T>
{
boolean test(T t);
}
How to use this in our application:
Case 1: Integer Check
public boolean test(Integer I){
if(I >10){
return true;
}else{
return false;
}
}
Using Lambda :
(Integer I) -> {
if(I >10)
return true;
else
return false;
}
or
I -> I >10;
Final Code:
Predicate<Integer> p = I -> I >10;
System.out.println (p.test(100)); // true
System.out.println (p.test(5)); // false
Example Program:
import java.util.function.*; // import java.util.function.Predicate;
Class Test{
public static void main(String args[]){
Predicate<Integer> p = I -> I >10;
System.out.println (p.test(100)); // true
System.out.println (p.test(5)); // false
}
}
Case 2: String length check
Example Program:
import java.util.function.*; // import java.util.function.Predicate;
Class Test{
public static void main(String args[]){
Predicate<String> p = s -> s.length() > 5;
System.out.println (p.test("ABCDEF")); // true
System.out.println (p.test("ABC")); // false
}
}
ArrayList is Empty or not ?
Example Program:
import java.util.function.*; // import java.util.function.Predicate;
Class Test{
public static void main(String args[]){
Predicate<Collection> p = c -> c.isEmpty();
ArrayList<String> list = new ArrayList<>();
System.out.println (p.test(list)); // true
list.add("A");
System.out.println (p.test(list)); // false
}
}
Predicate Joining:
* Join multiple predicate into single predicate
1. Given no >10 or not.
2. Given no even or not.
Note:
Reverse check : p1.negate()
and condition : p1.and(p2)
or condition : p1.or(p2)
Example Program:
Class Test{
public static void m1(Predicate<Integer> p , int[] x)
{
for(int x1 : x)
{
if(p.text(x1)){
System.out.println(x1);
}
}
}
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 are:");
m1(p1,x);
System.out.println("The Even numbers are:");
m1(p2,x);
System.out.println("The numbers not greater than 10 are:");
m1(p1.negate(),x);
System.out.println("The numbers greater than 10 are AND Even Numbers:");
m1(p1.and(p2),x);
System.out.println("The numbers greater than 10 are OR Even Numbers:");
m1(p1.or(p2),x);
}
}
Output:
The numbers greater than 10 are:
15
20
25
30
The Even numbers are:
0
10
20
30
The numbers not greater than 10 are:
0
5
10
The numbers greater than 10 AND Even Numbers:
20
30
The numbers greater than 10 OR Even Numbers:
0
10
15
20
25
30
Write a program to display Name start with "K"
class Test
{
public static void main(String[] args)
{
String[] names={"Sunny","Kajal","Mallika","Katrina","Kareena"};
Predicate<String> startsWithK=s->s.charAt(0)=='K';
System.out.println("The Names starts with K are:");
for(String s: names)
{
if(startsWithK.test(s))
{
System.out.println(s);
}
}
}
}
User Authentication with Predicate:
Class User{
private String username;
private Stirng password;
User(String username, String password){
this.username = username;
this.password = password;
}
}
class Test
{
public static void main(String[] args)
{
Predicate<User> p = u->u.username.equals("vinoth")&& u.pwd.equals("vino123");
Scanner sc= new Scanner(System.in);
System.out.println("Enter User Name:");
String username=sc.next();
System.out.println("Enter Password:");
String pwd=sc.next();
User user=new User(username,pwd);
if(p.test(user))
{
System.out.println("Valid user and can avail all services");
}
else
{
System.out.println("invalid user you cannot avail services");
}
}
}
Software Enigneer is allowed inside the pub or not ?
Condition:
1. He should be more than 18
2. He shoud have the girl friend.
class SoftwareEngineer
{
String name;
int age;
boolean isHavingGf;
SoftwareEngineer(String name,int age,boolean isHavingGf)
{
this.name=name;
this.age=age;
this.isHavingGf=isHavingGf;
}
public String toString()
{
return name;
}
}
class Test
{
public static void main(String[] args)
{
SoftwareEngineer[] list={ new SoftwareEngineer("Durga",60,false),
new SoftwareEngineer("Sunil",25,true),
new SoftwareEngineer("Sayan",26,true),
new SoftwareEngineer("Subbu",28,false),
new SoftwareEngineer("Ravi",17,true)
};
Predicate<SoftwareEngineer> allowed= se -> se.age>= 18 && se.isHavingGf;
System.out.println("The Allowed Members into Pub are:");
for(SoftwareEngineer se : list)
{
if(allowed.test(se))
{
System.out.println(se);
}
}
}
}
Employee Management System Using Predicate:
class Employee
{
String name;
String designation;
double salary;
String city;
Employee(String name,String designation,double salary,String city)
{
this.name=name;
this.designation=designation;
this.salary=salary;
this.city=city;
}
public String toString()
{
String s=String.format("[%s,%s,%.2f,%s]",name,designation,salary,city);
return s;
}
public boolean equals(Object obj)
{
Employee e=(Employee)obj;
if(name.equals(e.name)&&designation.equals(e.designation)&&salary==e.salary && city==e.city)
{
return true;
}
else
{
return false;
}
}
}
class Test
{
public static void main(String[] args)
{
ArrayList<Employee> list= new ArrayList<Employee>();
populate(list);
Predicate<Employee> p1=emp->emp.designation.equals("Manager");
System.out.println("Managers Information:");
display(p1,list);
Predicate<Employee> p2=emp->emp.city.equals("Bangalore");
System.out.println("Bangalore Employees Information:");
display(p2,list);
Predicate<Employee> p3=emp->emp.salary<20000;
System.out.println("Employees whose slaray <20000 To Give Increment:");
display(p3,list);
System.out.println("All Managers from Bangalore city for Pink Slip:");
display(p1.and(p2),list);
System.out.println("Employees Information who are either Managers or salary <2000
display(p1.or(p3),list);
System.out.println("All Employees Information who are not managers:");
display(p1.negate(),list);
Predicate<Employee> isCEO=Predicate.isEqual(new Employee("Durga","CEO",30000,"rabad"));
Employee e1=new Employee("Durga","CEO",30000,"Hyderabad");
Employee e2=new Employee("Sunny","Manager",20000,"Hyderabad");
System.out.println(isCEO.test(e1));//true
System.out.println(isCEO.test(e2));//false
}
public static void populate(ArrayList<Employee> list)
{
list.add(new Employee("Durga","CEO",30000,"Hyderabad"));
list.add(new Employee("Sunny","Manager",20000,"Hyderabad"));
list.add(new Employee("Mallika","Manager",20000,"Bangalore"));
list.add(new Employee("Kareena","Lead",15000,"Hyderabad"));
list.add(new Employee("Katrina","Lead",15000,"Bangalore"));
list.add(new Employee("Anushka","Developer",10000,"Hyderabad"));
list.add(new Employee("Kanushka","Developer",10000,"Hyderabad"));
list.add(new Employee("Sowmya","Developer",10000,"Bangalore"));
list.add(new Employee("Ramya","Developer",10000,"Bangalore"));
}
public static void display(Predicate<Employee> p,ArrayList<Employee> list)
{
for (Employee e: list )
{
if(p.test(e))
{
System.out.println(e);
}
}
System.out.println("**************************************************");
}
}
Ouput:
Managers Information: [Sunny,Manager,20000.00,Hyderabad] [Mallika,Manager,20000.00,Bangalore] ************************************************** Bangalore Employees Information: [Mallika,Manager,20000.00,Bangalore] [Katrina,Lead,15000.00,Bangalore] [Sowmya,Developer,10000.00,Bangalore] [Ramya,Developer,10000.00,Bangalore] ************************************************** Employees whose slaray <20000 To Give Increment: [Kareena,Lead,15000.00,Hyderabad] [Katrina,Lead,15000.00,Bangalore] [Anushka,Developer,10000.00,Hyderabad] [Kanushka,Developer,10000.00,Hyderabad] [Sowmya,Developer,10000.00,Bangalore] [Ramya,Developer,10000.00,Bangalore] **************************************************
All Managers from Bangalore city for Pink Slip: [Mallika,Manager,20000.00,Bangalore] ************************************************** Employees Information who are either Managers or salary <20000 [Sunny,Manager,20000.00,Hyderabad] [Mallika,Manager,20000.00,Bangalore] [Kareena,Lead,15000.00,Hyderabad] [Katrina,Lead,15000.00,Bangalore] [Anushka,Developer,10000.00,Hyderabad] [Kanushka,Developer,10000.00,Hyderabad] [Sowmya,Developer,10000.00,Bangalore] [Ramya,Developer,10000.00,Bangalore] **************************************************
All Employees Information who are not managers: [Durga,CEO,30000.00,Hyderabad] [Kareena,Lead,15000.00,Hyderabad] [Katrina,Lead,15000.00,Bangalore] [Anushka,Developer,10000.00,Hyderabad] [Kanushka,Developer,10000.00,Hyderabad] [Sowmya,Developer,10000.00,Bangalore] [Ramya,Developer,10000.00,Bangalore] ************************************************** true false
Predicate interface isEqual():
* It is a static method in the predicate interface.
Eg.
Class Test{
public static void main(String args[]){
Predicate<String> p = Predicate.isEqual("Vinoth");
System.out.println(p.test("Vinoth")); // true
System.out.println(p.test("Kumar")); // false
}
}
Comments
Post a Comment