Predicates and BiPredicate Functional Interface
In my previous
blog, I’ve already talked about Lambda Expression and Functional Interface. In
this blog, I’ll talk about predefined Functional Interface which has been introduced
in JAVA 8 under Java.util.Function.
Predicate is
a Functional Interface which accept single input and return response in either
True or False. It is very similar to Predicates that we have learnt in School i.e.
which takes a value and return only Boolean value. The functional method of
Predicate is test(Object).
@FunctionalInterface
Public interface
Predicate<T>
Here is a simple source code of java.util.function.Predicate
package
java.util.function;
import
java.util.Objects;
@FunctionalInterface
public interface
Predicate<T> {
boolean test(T t);
}
Where
Test (T t) is an abstract method where T is the type of the input to the
predicate and will always return a Boolean Value.
Let’s understand this with examples
Example 1:
Example 2:
Example 3:
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class PredicateExmaple2 {
public static void main(String[] args) {
PredicateExmaple2 predicate = new PredicateExmaple2();
Predicate<Student> p3 = student -> student.getAge() > 30;
Predicate<Student> p4 = student -> student.getName().startsWith("A");
List<Student> students = predicate.populateStudentList();
for (Student student : students) {
if (p3.test(student) && p4.test(student)) {
System.out.println("Student Name: " + student.getName());
System.out.println("Student Age: " + student.getAge());
}
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public List<Student> populateStudentList() {
List<Student> studentList = Arrays.asList(new Student("Abdul", 31), new Student("Waheed", 29),
new Student("DummyUser", 20));
return studentList;
}
}
Output:
Student
Name: Abdul
Student Age: 31
Apart from test (T t) method , Predicate also has 3 default method and
one static method.
Default Method Name
|
Explanation
|
and()
|
It does logical AND of the
predicate on which it is called with another predicate. Example: predicate1.and(predicate2)
|
or()
|
It does logical OR of the predicate
on which it is called with another predicate. Example: predicate1.or(predicate2)
|
negate()
|
It does boolean negation of the
predicate on which it is invoked. Example: predicate1.negate()
|
isEqual()
|
Compare 2 instances of Predicate
functional interface.
|
Lets understand it with few more example
package
com.waheedtechblog.functionalinterface;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class PredicateExmaple2 {
public static void main(String[] args) {
PredicateExmaple2 predicate = new PredicateExmaple2();
Predicate<Student> p3 = student -> student.getAge() > 30;
Predicate<Student> p4 = student -> student.getName().startsWith("A");
Predicate<Student> p5 = Predicate.isEqual(new PredicateExmaple2().new Student("Abdul", 31));
List<Student> students = predicate.populateStudentList();
for (Student student : students) {
if (p3.test(student) && p4.test(student)) {
System.out.println("Student Name: " + student.getName());
System.out.println("Student Age: " + student.getAge());
}
}
System.out.println("\n");
System.out.println("=============================");
System.out.println("=====USING AND PREDICATE======");
// Using AND
PREDICATE
for (Student student : students) {
if (p3.and(p4).test(student)) {
System.out.println("Student Name: " + student.getName());
System.out.println("Student Age: " + student.getAge());
}
}
System.out.println("\n");
System.out.println("=============================");
System.out.println("=====USING OR PREDICATE======");
// Using OR
PREDICATE
for (Student student : students) {
if (p3.or(p4).test(student)) {
System.out.println("Student Name: " + student.getName());
System.out.println("Student Age: " + student.getAge());
}
}
System.out.println("\n");
System.out.println("=============================");
System.out.println("=====USING NEGATE PREDICATE======");
for (Student student : students) {
if (p3.negate().test(student)) {
System.out.println("Student Name: " + student.getName());
System.out.println("Student Age: " + student.getAge());
}
}
System.out.println("\n");
System.out.println("=============================");
System.out.println("=====USING ISEQUAL PREDICATE======");
//Override
equals method in Student class
for (Student student : students) {
if (p5.test(student)) {
System.out.println("Student Name: " + student.getName());
System.out.println("Student Age: " + student.getAge());
}
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean equals(Object obj) {
Student student = (Student) obj;
if ((this.getName().equals(student.getName()) && this.getAge() == student.getAge())) {
return true;
} else {
return false;
}
}
}
public List<Student> populateStudentList() {
List<Student> studentList = Arrays.asList(new Student("Abdul", 31), new Student("Waheed", 29),
new Student("DummyUser", 20), new Student("Adam", 25));
return studentList;
}
}
Output:
Student
Name: Abdul
Student
Age: 31
=============================
=====USING
AND PREDICATE======
Student
Name: Abdul
Student
Age: 31
=============================
=====USING
OR PREDICATE======
Student
Name: Abdul
Student
Age: 31
Student
Name: Adam
Student
Age: 25
=============================
=====USING
NEGATE PREDICATE======
Student
Name: Waheed
Student
Age: 29
Student
Name: DummyUser
Student
Age: 20
Student
Name: Adam
Student
Age: 25
=============================
=====USING
ISEQUAL PREDICATE======
Student
Name: Abdul
Student
Age: 31
BiPredicate Functional Interface
It is very similar to Predicate
Functional Interface. The only difference is just that it will accept two
argument instead of one and as usual will return the Boolean Value.
@FunctionalInterface
public interface BiPredicate<T,U>
Let’s see how can we achieve Example 4
using BiPredicate Interface
import java.util.Arrays;
import java.util.List;
import java.util.function.BiPredicate;
public class BiPredicateExample {
public static void main(String[] args) {
BiPredicateExample predicate = new BiPredicateExample();
BiPredicate<Integer, String> b1 = (age, name) -> age > 30 && name.startsWith("A");
List<Student> students = predicate.populateStudentList();
for (Student student : students) {
if (b1.test(student.getAge(), student.getName())) {
System.out.println("Student Name: " + student.getName());
System.out.println("Student Age: " + student.getAge());
}
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean equals(Object obj) {
Student student = (Student) obj;
if ((this.getName().equals(student.getName()) && this.getAge() == student.getAge())) {
return true;
} else {
return false;
}
}
}
public List<Student> populateStudentList() {
List<Student> studentList = Arrays.asList(new Student("Abdul", 31), new Student("Waheed", 29),
new Student("DummyUser", 20), new Student("Adam", 25));
return studentList;
}
}
Output:
Student
Name: Abdul
Student
Age: 31
That’s it for Predicate and
BiPredicate Functional Interface, Will talk about other Predefined Functional
Interface in my next blog.
Happy Coding…!!!
No comments:
Post a Comment