Java 8 BiPredicate Interface

7 months ago codedestine 1

In this tutorial, we will learn about Java 8 BiPredicate interface.

Java 8 BiPredicate Interface

BiPredicate Interface :-

BiPredicate ( java.util.function.BiPredicate ) is a functional interface that has one abstract method and three default method declared in it.

Abstract method :- 

boolean  test ( T  t, U u ) :- The abstract method test ( T t, U u ) is a functional method. It accepts two parameter as an argument and return boolean value. It represents a conditional operation, where one passed argument is tested against a specific value or against a second passed argument to give a boolean ( True or False ) result.

Default method :-  

  1. default  BiPredicate<T>  and ( BiPredicate <? super T, ? super U> other ) :- It returns a biPredicate that represents short circuiting logical AND of this biPredicate ( biPredicate on which method is called ) and another ( passed as an argument ).
  2. default  BiPredicate<T>  or ( BiPredicate <? super T, ? super U > other ) :- It returns a biPredicate that represents short circuiting logical OR of this biPredicate ( biPredicate on which method is called ) and another ( passed as an argument ).
  3. default  BiPredicate<T>  negate (  ) :- It returns a biPredicate that represents logical negation of this biPredicate ( biPredicate on which method is called ).

Reference :-  BiPredicate Interface JavaDocs 

Example – 1 :- 

This example will show you, how to create and call different methods of a biPredicate interface.

Employee ( POJO ) :-  Used in our example.


  public class Employee {

    private long empId;
    private String name;
    private int age;
    private String designation;

    public Employee(long empId, String name, int age, String designation) {
      super();
      this.empId = empId;
      this.name = name;
      this.age = age;
      this.designation = designation;
    }

    public long getEmpId() {
      return empId;
    }
    public void setEmpId(long empId) {
      this.empId = empId;
    }
    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 String getDesignation() {
      return designation;
    }
    public void setDesignation(String designation) {
      this.designation = designation;
    }

 }

Main method :- This method has bipredicate example.

  public static void main(String[] args){ 
 
    Employee emp = new Employee(1,"Robert",35,"Manager");
 
    BiPredicate<Employee,String> biPredicate1 = ( x , y ) -> {
                                                              if( x.getDesignation().equalsIgnoreCase(y)){
                                                                  return true;
                                                              }else{
                                                                  return false;
                                                              }};
    // Example of test method
    boolean result = biPredicate1.test(emp,"Manager");
    System.out.println(result);
 
    // Example of negate method
    boolean result3 = (biPredicate1.negate()).test(emp,"Manager");
    System.out.println(result3);
 
    BiPredicate<Employee,String> biPredicate2 = ( x , y ) -> {
                                                              if( x.getDesignation().equalsIgnoreCase(y)){
                                                                  return true;
                                                              }else{
                                                                  return false;
                                                              }};
 
    // Example of and method
    boolean result1 = biPredicate1.and(biPredicate2).test(emp,"Manager");
    System.out.println(result1);
 
    // Example of or method
    boolean result2 = biPredicate1.or(biPredicate2).test(emp,"General Manager");
    System.out.println(result2);
 
 }    

Result :- 

true
false
true
false

That’s all for Java 8 BiPredicate Interface. If you liked it, please share your thoughts in comments section and share it with others too.