A Brief Introduction about Predicate function
Posted By : Vakeel Malik | 08-Apr-2018
What is predicate function:-
A Predicate is a function with an argument which is return boolean value. when we use predicate function we need to import java.util.function.*; of Java 1.8 an Interface by name Predicate<T>. Here T is a type of input taken by predicate function.
this predicate interface is a functional interface that can refer to the lambda expression. the predicate function contains test() method that returns the boolean value.
Syntax of predicate function as :
Predicate<T> gt = (i) -> (i)>10 ? return true:false;
In above code of predicate function ckecking the value is greate than 10 or not, if greater than return true otherwise false
Since java compiler knows the values of returned . we can reduce code as:
Predicate<T> =(i) -> (i)>10;
This is simple code to check the number is greater than 10 or not.
int n=9;
if(n>10)
System.out.println("number is greate than 10");
else
System.out.println("number is less than 10");
Simple program to find the value is greate than 10 or not without predicat function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class Test
{
Boolean testNumber(int n)
{
if(n>10)
{
return true;
}
else
{return false; }
}
public static void main(String arg[])
{
Test ob=new Test();
int n=9;
Boolean t=ob.testNumber(n);
if(t)
System.out.println("number is greated than 10 = " +t);
else
System.out.println("number is less than 10 = "+t);
}
}
|
Output -: number is less than 10 false
Since Above program with predicate function as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import java.util.function.*;
class TestNumberByPredicat
{
public static void main(String arg[])
{
Predicate<Integer> gt=(n) -> (n)>10;
int num = 10;
Boolean b = gt.test(num);
if(b)
{System.out.println("Numner is greate than 10"); }
else
{System.out .println("Number is less than 10"); }
}
}
|
Output -: number is less than 10
Joining predicate function:-
It is possible joining two dedicated function using and(), or(), and negate() method . when we use and() method of predicate function it will return the value of logical AND operator . or method returns the value of logical OR operator and negative() method negate the value means when the value is true return false and vice versa.
Syntax of methods as:
1
2
|
Predicat<Integer> gt= (n) -> (n)>10;
Predicat<Integer> ls= (n) -> (n)<15;
|
gt.and(ls);
this will return true if n is greater than 10 and less than 15. otherwise false
gt.or(ls);
this will return true if neither greater than 10 or less then 15.
Simple program of join predicat-:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import java.util.function.*;
class JoinPredicatDemo
{
public static void main(String arg[])
{
Integer []ar={4,5,7,10,15,11,12,20,25,30};
Predicate<Integer> gt, ls;
gt= (n) -> (n)>10;
ls= (n) -> (n)<15;
// to check the nubmer is greater than 10 and lass than 15
System.out.println("Number greater than 10 less tha 15");
display(gt.and(ls), ar);
// to check the number is less than 10 and greater than 15
System.out.println("Number less than 10 greater tha 15");
display(gt.and(ls).negate(), ar);
}
static void display(Predicate<Integer> p, Integer []ar)
{
for(Integer i: ar)
{
if(p.test(i)){
System.out.println(i+" ");
}
}
}
}
|
Output -:
Number greater than 10 and less than 15
11
12
Number less than 10 and greater than 15
4
5
7
10
15
20
25
30
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Vakeel Malik
Vakeel is a bright Java Developer working in Oodles.