Predefined Functional Interface - Predicate -Java
Predefined Functional Interface
Predicate
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’s a functional
interface and it contains only one method i.e., test()
Ex:
interface
Predicate<T> {
public boolean
test(T t);
}
As predicate is a
functional interface and hence it can refers lambda expression
Ex:1 Write a
predicate to check whether the given integer is greater than 10 or
not.
Ex:
OPTION 1:
public boolean
test(Integer I) {
if (I >10) {
return true;
}
else {
return false;
}
}
OPTION 2:
(Integer I) ->{
if(I > 10)
return true;
else
return false;
}
OPTION 3:
I ->(I>10);
Predicate<Integer> p = I ->(I >10);
System.out.println (p.test(100)); true
System.out.println (p.test(7)); false
import
Java.util.function.*;
class Test {
public static
void main(String[] args) {
Predicate<Integer>
p = I ->(i>10);
System.out.println(p.test(100));
System.out.println(p.test(7));
System.out.println(p.test(true));
//CE
}
}
# 1 Write a
predicate to check the length of given string is greater than 3 or
not.
Predicate<String>
p = s ->(s.length() > 3);
System.out.println
(p.test(“rvkb”)); true
System.out.println
(p.test(“rk”)); false
#-2 write a
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
same as logical AND ,OR complement operators
Ex:
import
Java.util.function.*;
class test {
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);
}
}
Program to
display names starts with 'K' by using Predicate:
import
java.util.function.Predicate;
class Test
{
public static void
main(String[] args)
{
String[]
names={"Sunny","Kapil","Mohan","Kat","Kapoor"};
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);
}
}
}
}
Output:
The Names starts
with K are:
Kapil
Kat
Kapoor
Predicate Example
to remove null values and empty strings from the given list:
import
java.util.List;
import
java.util.function.Predicate;
class Test
{
public static void
main(String[] args)
{
String[]
names={"Durga","",null,"Ravi","","Shiva",null};
Predicate<String>
p=s-> s != null && s.length()!=0;
List<String>
list=new ArrayList<String>();
for(String s :
names)
{
if(p.test(s))
{
list.add(s);
}
}
System.out.println("The
List of valid Names:");
System.out.println(list);
}
}
Output:
The List of valid
Names:
[Durga, Ravi, Shiva]
Program for User
Authentication by using Predicate:
import
java.util.function.Predicate;
import
java.util.Scanner;
class User
{
String username;
String pwd;
User(String
username,String pwd)
{
this.username=username;
this.pwd=pwd;
}
}
class Test
{
public static void
main(String[] args)
{
Predicate<User>
p = u->u.username.equals("mps")&&
u.pwd.equals("java");
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");
}
}
}
java Test
Enter User Name:
durga
Enter Password:
java
Valid user and can
avail all services
java Test
Enter User Name:
ravi
Enter Password:
java
invalid user you
cannot avail services
Program to check
whether SoftwareEngineer is allowed into pub or not by using
Predicate?
import
java.util.function.Predicate;
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);
}
}
}
}
Output:
The allowed members
into Pub are:
Sunil
Sayan
Employee
Management Application:
import
java.util.function.Predicate;
import
java.util.ArrayList;
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
&& c
ity==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
0");
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,"
Hyderabad"));
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("**************************************************");
}
Output:
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
Q1. Which of the
following abstract method present in Predicate
interface?
A. test()
B. apply()
C. get()
D. accept()
Answer: A
Explanation:
Predicate functional interface contains only one abstract method:
test()
Q2. Which of the
following is the static method present in Predicate
interface?
A. test()
B. and()
C. or()
D. isEqual()
Answer: D
Explanation:
Predicate functional interface contains only one static method:
isEqual()
Q3. Which of the
following default methods present in Predicate
interface?
A. and()
B. or()
C. negate()
D. All the above
Answer: D
Explanation:
Predicate Functional interface contains the following 3 default
methods:
and(),or(),not()
Q4. Which of the
following is Predicate interface declaration?
A.
interface
Predicate<T>
{
public boolean
test(T t);
}
B.
interface
Predicate<T>
{
public boolean
apply(T t);
}
C.
interface
Predicate<T,R>
{
public R test(T t);
}
D.
interface
Predicate<T,R>
{
public R apply(T
t);
}
Answer: A
Explanation:
interface
Predicate<T>
{
public boolean
test(T t);
}
Predicate interface
can take only one Type parameter which represents only input type. We
are
not required to
specify return type because return type is always boolean type.
Q5. Which of the
following is valid Predicate to check whether the
given Integer is
divisible by 10 or not?
A.
Predicate<Integer> p = i -> i%10 == 10;
B.
Predicate<Integer,Boolean> p =i->i%10==0;
C.
Predicate<Boolean,Integer> p =i->i%10==0;
D. None of the above
Answer: A
Explanation:
1) interface
Predicate<T>
2) {
3)
public boolean
test(T t);
4)
}
Predicate interface
can take only one Type parameter which represents only input type. We
are
not required to
specify return type because return type is always boolean type.
Q6. Which of the
following is valid regarding Predicate functional
interface?
A. Predicate
Functional interface present in java.util.function package
B. It is introduced
in java 1.8 version
C. We can use
Predicate to implement conditional checks
D. It is possible to
join 2 predicates into a single predicate also.
E. All the above
Answer: E
Q7. Which of the
following is valid Predicate to check whether the
given user is admin
or not?
A. Predicate<User>
p=user->user.getRole().equals("Admin");
B.
Predicate<Boolean> p=user->user.getRole().equals("Admin");
C. Predicate<User>
p=(user,s="admin")->user.getRole().equals(s);
D. None of the above
Answer: A
Explanation:
interface
Predicate<T>
{
public boolean
test(T t);
}
Predicate interface
can take only one Type parameter which represents only input type. We
are
not required to
specify return type because return type is always boolean type.
Q8. Consider the
following Predicates
Predicate<Integer>
p1=i->i%2==0;
Predicate<Integer>
p1=i->i>10;
Which of the
following are invalid ?
A. p1.and(p2)
B. p1.or(p2)
C. p1.negate(p2)
D. p1.negate()
Answer: C
Explanation:
negate() method won't take any argument
Comments
Post a Comment