- Predicate is a mathematics term to introduce java 8 as a functional interface. version (i.e.,Predicate<T>).
- Predicate is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
- A predicate is a function with a single argument and returns boolean value.
- Predicate interface present in Java.util.function package.
- It’s a functional interface and it contains only one method i.e., test()
Syntex.
interface Predicate<T> {
public boolean test(T t);
}
Parameters
Returns
T
Write a predicate to check whether the given integer is greater than 10 or not.
public boolean test(Integer I) {
if (I >20) {
⇩⇩ return true;
}
else {
return false;
}
}
⇓
(Integer I) -> {
if(I > 20 )
return true;
else
return false;
}
⇓
I ->(I > 20);
⇓
predicate<Integer> p = I (I >20);
System.out.println (p.test(100)); true
System.out.println (p.test(17)); false
Example 1:
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(17));
System.out.println(p.test(true)); //Compile time error }
}
Example 2.Write a predicate to check the length of given string is greater than 5 or not.
Example 3: Write a predicate to check whether the given collection is empty or not.
Predicate Joining:-
It’s possible to join predicates into a single predicate by using the following methods.
Example 4: How to use Predicate in Functional interface
Output:-
The Numbers Greater Than 10:
15
20
25
30
The Even number are :
0
10
20
30
The number not Greater then 10:
0
5
10
The number Greater then 10 and Even are:
20
30
The number Greater then or Even are
0
10
15
20
25
30
Example 4: To Display names starts with 'K' by using Predicate.
Output:-
The name Start with K are :
Kareena
Kajal
Kareena
Katrina
Example 5: To remove null values and empty strings from the given list.
Output:- The List of valid Name : [Sudhir, Ram, Kajal, Monika, Kareena, Katrina]
Example 6: User Authentication by using Predicate
Output:-
Enter the Username :
Sudhir
Enter the Password :
sudhir
Valid user and can avail all services
Example 7: To check whether SoftwareEngineer is allowed into pub or not by using Predicate
Output:-
The Allowed number into Pub are :
Sudhir
Sudhir
Asha
Aarti
Example 8: Employee Management Application.
Output:-
Manager Information :
[Rakest,Developer,40000.00,Bangalore]
**************************************************
Bangalore Employees Information:
[Sudhir,Team Lead,30000.00,Bangalore]
[Aarti,Test Engineer,30000.00,Bangalore]
[Rakest,Developer,40000.00,Bangalore]
**************************************************
[[Sudhir,Team Lead,30000.00,Bangalore], [Aarti,Test Engineer,30000.00,Bangalore], [Rakest,Developer,40000.00,Bangalore], [Manish,Software Engineer,20000.00,Hyderabad], [Neha,Operation Manager,50000.00,Hyderabad], [Ram,ECO,300000.00,Patna]]
Method Detail
public boolean test(T t); Parameters
T
- the type of arguments to the predicate
t
- the input argument.Returns
T
rue
if the input argument matches the predicate, otherwise false.
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 10 or not.
public boolean test(Integer I) {
if (I >20) {
⇩⇩ return true;
}
else {
return false;
}
}
⇓
(Integer I) -> {
if(I > 20 )
return true;
else
return false;
}
⇓
I ->(I > 20);
⇓
predicate<Integer> p = I (I >20);
System.out.println (p.test(100)); true
System.out.println (p.test(17)); false
Example 1:
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(17));
System.out.println(p.test(true)); //Compile time error }
}
Example 2.Write a predicate to check the length of given string is greater than 5 or not.
Example 3: Write a predicate to check whether the given collection is empty or not.
Predicate Joining:-
It’s possible to join predicates into a single predicate by using the following methods.
- and()
- or()
- negate()
Example 4: How to use Predicate in Functional interface
Output:-
The Numbers Greater Than 10:
15
20
25
30
The Even number are :
0
10
20
30
The number not Greater then 10:
0
5
10
The number Greater then 10 and Even are:
20
30
The number Greater then or Even are
0
10
15
20
25
30
Example 4: To Display names starts with 'K' by using Predicate.
Output:-
The name Start with K are :
Kareena
Kajal
Kareena
Katrina
Example 5: To remove null values and empty strings from the given list.
Output:- The List of valid Name : [Sudhir, Ram, Kajal, Monika, Kareena, Katrina]
Example 6: User Authentication by using Predicate
Output:-
Enter the Username :
Sudhir
Enter the Password :
sudhir
Valid user and can avail all services
Example 7: To check whether SoftwareEngineer is allowed into pub or not by using Predicate
Output:-
The Allowed number into Pub are :
Sudhir
Sudhir
Asha
Aarti
Example 8: Employee Management Application.
Output:-
Manager Information :
[Rakest,Developer,40000.00,Bangalore]
**************************************************
Bangalore Employees Information:
[Sudhir,Team Lead,30000.00,Bangalore]
[Aarti,Test Engineer,30000.00,Bangalore]
[Rakest,Developer,40000.00,Bangalore]
**************************************************
[[Sudhir,Team Lead,30000.00,Bangalore], [Aarti,Test Engineer,30000.00,Bangalore], [Rakest,Developer,40000.00,Bangalore], [Manish,Software Engineer,20000.00,Hyderabad], [Neha,Operation Manager,50000.00,Hyderabad], [Ram,ECO,300000.00,Patna]]
useful for java 1.8 features..please provide the more details..
ReplyDeleteThanks for appreciation, will get back to you with more details soon. :)
Delete