BiPredicate Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function expect two parameters as an input and produces a boolean value as an output. As it is a functional interface, it can be used assignment target for lambda expression or method reference. In this post, we are going to see several implementations of BiPredicate Interface by using different examples.

BiPredicate_Interface_Java8_Techndeck

BiPredicate Interface in Java 8 with Examples...!!! Share on X

Look at BiPredicate Javadoc description below:

BiPredicate Interface contains 4 methods:

  1. test

  2. and

  3. or

  4. negate

Let’s discuss these methods:

test 

This method performs operation on the given two arguments and results in a boolean value.

boolean test(T t, U u);

and 

This method returns a composed predicate that represents a short-circuiting logical AND of this predicate and another. When evaluating the composed
predicate, if this predicate is false, then the other predicate is not evaluated.

default BiPredicate<T, U> and(BiPredicate<? super T, ? super U> other) {
        Objects.requireNonNull(other);
        return (T t, U u) -> test(t, u) && other.test(t, u);
    }

or 

This method returns a composed predicate that represents a short-circuiting logical OR of this predicate and another. When evaluating the composed predicate, if this predicate is true, then the other predicate is not evaluated.

default BiPredicate<T, U> or(BiPredicate<? super T, ? super U> other) {
        Objects.requireNonNull(other);
        return (T t, U u) -> test(t, u) || other.test(t, u);
    }

negate 

This method returns a predicate that represents the logical negation of this predicate.

default BiPredicate<T, U> negate() {
        return (T t, U u) -> !test(t, u);
    }

Lets understand above mentioned methods thorough various examples:

Example 1. Simple Predicate

import java.util.function.BiPredicate;

public class BiPredicateInterfaceJava8Example1 {

    public static void main(String[] args) {

        System.out.println("BiPredicate Interface - Java 8 - Example : Simple BiPredicate");

        BiPredicate <Integer, Integer> biPredicateObj = (i1, i2) -> i1 == i2;

        System.out.println("Are numbers matching: " + biPredicateObj.test(5, 10));
    }

}

Example 2. Predicate Chaining with ‘and’

import java.util.function.BiPredicate;

public class BiPredicateInterfaceJava8Example2 {

    public static void main(String[] args) {

        System.out.println("BiPredicate Interface - Java 8 - Example : BiPredicate Chaining");

        BiPredicate <Integer, Integer> valuesNotMatching = (i1, i2) -> i1 != i2;

        BiPredicate <Integer, Integer> valueOneGreaterThanValueTwo = (i1, i2) -> i1 > i2;

        //BiPredicate chaining : Example with 'and' predicate
        boolean result = valuesNotMatching.and(valueOneGreaterThanValueTwo).test(5, 10);

        System.out.println(result);

    }

}

Example 3. Passing Predicate into a Function 

import java.util.function.BiPredicate;

public class BiPredicateInterfaceJava8Example3 {

    public static void main(String[] args) {

        System.out.println("BiPredicate Interface - Java 8 - Example : Passing BiPredicate into a Function");

        BiPredicate <Integer, Integer> biPredicateObj = (i1, i2) -> i1 == i2;

        checkIfValueMatches(5, biPredicateObj);

    }

    private static void checkIfValueMatches(int i, BiPredicate <Integer, Integer> bip) {
        System.out.println("Does value matching: " + bip.test(i, 10));
    }

}

Example 4. ‘or’ with Anonymous/Lambda Predicate Implementation

import java.util.function.BiPredicate;

public class BiPredicateInterfaceJava8Example4 {

    public static void main(String[] args) {

        System.out.println("BiPredicate Interface - Java 8 - Example : OR & Anonymous/Lambda BiPredicate Implementations");

        //BiPredicate anonymous implementation
        BiPredicate <String, String> startsWithSpecificString = new BiPredicate <String, String> () {
            @Override
            public boolean test(String s1, String s2) {
                return s1.startsWith(s2);
            }
        };

        //Predicate lambda implementation
        BiPredicate <String, String> containsSpecificString = (s1, s2) -> s1.contains(s2);

        //OR condition
        boolean result = startsWithSpecificString.or(containsSpecificString).test("ish", "isha durani");

        System.out.println("Result: " + result);

    }

}

Example 5. Predicate ‘negate’ 

import java.util.function.BiPredicate;

public class BiPredicateInterfaceJava8Example5 {

    public static void main(String[] args) {

        BiPredicate <String, String> predicate = (s1, s2) -> s1.equals(s2);

        //NEGATE operation 
        BiPredicate <String, String> predicateNegate = predicate.negate();

        System.out.println(predicateNegate.test("Hello Predicate", "Hello"));

    }

}

Example 6. Predicate List Example

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

public class BiPredicateInterfaceJava8Example6 {

    public static void main(String[] args) {

        System.out.println("BiPredicate Interface - Java 8 - Example : BiPredicate List Example\n");

        BiPredicate <Integer, List <Integer>> listPredicateContainsSpecificInteger = (i, l) -> l.contains(i);

        List <Integer> list = new ArrayList <Integer> ();

        list.add(19);
        list.add(4);
        list.add(83);
        list.add(5);
        list.add(63);

        checkIfValuePresentInList(list, listPredicateContainsSpecificInteger);

    }

    public static void checkIfValuePresentInList(List <Integer> list, BiPredicate <Integer, List <Integer>> bip) {

        if (bip.test(5, list)) {
            System.out.println("Success: List contains the expected value which is number 5");
        } else {
            System.out.println("Failure: List doens't contains the expected value which is number 5");
        }

    }

}

Example 7. with custom Class object (like ‘Employee’ in this case)

import java.util.function.BiPredicate;

public class BiPredicateInterfaceJava8Example7 {

    public static void main(String[] args) {

        System.out.println("BiPredicate Interface - Java 8 - Example : BiPredicate Custom Class Example\n");

        BiPredicate <Integer, Employees> employeePredicate = (i, emp) -> emp.getAge() > i;

        Employees e1 = new Employees(15, "Tom");
        Employees e2 = new Employees(6, "Sam");
        Employees e3 = new Employees(88, "Mark");

        System.out.println("Age of First Employee greater than 33? : " + employeePredicate.test(33, e1));
        System.out.println("Age of Second Employee greater than 15? : " + employeePredicate.test(15, e2));
        System.out.println("Age of Third Employee greater than 45? : " + employeePredicate.test(45, e3));
    }

}


class Employees {

    int age;
    String name;

    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Employees(int age, String name) {
        this.age = age;
        this.name = name;
    }
}

Java 8 BiPredicate Interface is an absolute useful addition as part of ‘Functional Interfaces’ and can serve variety of purposes. It is quite powerful as it can be used as a higher order functions through lambda functions and above examples could help you to get better idea on how to implement it.

BiPredicate Interface in Java 8 with Examples...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts