BiPredicate Simple Example

This post explains BiPredicate functional interface. BiPredicate is similar to Predicate functional interface except BiPredicate takes two arguments instead of one.

In the below example we will filter list of Person entities based on first and last name. The predicate class name will be “NamePredicate”.

NamePredicate


package Function;

import java.util.function.BiPredicate;

public class NamePredicate implements BiPredicate {
    private String fname;
    private String lname;
    
    @Override
    public boolean test(String fname, String lname) {
        if(fname.equals(this.fname) && lname.equals(this.lname)) {
            return true;
        }
        return false;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }
}

As you can see in the test method of NamePredicate. It takes two arguments.

The class structure of person entity is as shown below

Person


package Function;

public class Person {
    private String ssn;
    private String fname;
    private String lname;
    private String description;
    private int age;
    
    public String getSsn() {
        return ssn;
    }
    public void setSsn(String ssn) {
        this.ssn = ssn;
    }
    public String getFname() {
        return fname;
    }
    public void setFname(String fname) {
        this.fname = fname;
    }
    public String getLname() {
        return lname;
    }
    public void setLname(String lname) {
        this.lname = lname;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(fname).append(",");
        sb.append(lname);
        
        return sb.toString();
    }
}

The main class

Main


package Function;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiPredicate;

public class BiPredicateDemo {
    public static void main(String[] args) {
        List list = new ArrayList(10);
        for(int i = 0 ; i < 10; i++) {
            Person person = new Person();
            person.setSsn("ssn" + i);
            person.setFname("fname" + i);
            person.setLname("lname" + i);
            person.setDescription("description" + i);
            person.setAge(i);
            
            list.add(person);
        }
        
        NamePredicate namePredicate = new NamePredicate();
        namePredicate.setFname("fname0");
        namePredicate.setLname("lname0");
        List list1 = filter(namePredicate, list);
        display("Name Predicate", list1);
    }
    
    public static List filter(BiPredicate predicate, List list) {
        List filteredList = new ArrayList();
        for(Person person : list) {
            if(predicate.test(person.getFname(), person.getLname())) {
                filteredList.add(person);
            }
        }
        return filteredList;
    }
    
    public static void display(String queryName, List list) {
        System.out.println(queryName);
        for(Person person : list) {
            System.out.println(person);
        }
    }
}

Output

Name Predicate
fname0,lname0

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s