Java 8 – BiPredicate | Code Factory


Index Page : Link

Donate : Link

Medium Blog : Link

Applications : Link

Normal Predicate can take only one input argument and perform some conditional check. Sometimes our programming requirement is we have to take 2 input arguments and perform some conditional check, for this requirement we should go for BiPredicate.

BiPredicate is exactly same as Predicate except that it will take 2 input arguments.

@FunctionalInterface
public interface BiPredicate<T,U> {
    boolean test(T t, U u);
    default BiPredicate<T,U> and(BiPredicate<? super T,? super U> other) { }
    default BiPredicate<T,U> negate() { }
    default BiPredicate<T,U> or(BiPredicate<? super T,? super U> other) { }
}

To check the sum of 2 given integers is even or not by using BiPredicate :

package com.codeFactory.bipredicate;

import java.util.function.BiPredicate;

public class Test {
	public static void main(String... args) {
		BiPredicate<Integer, Integer> p = (a, b) -> ((a+b) % 2 == 0);
		System.out.println(p.test(10, 20));
		System.out.println(p.test(7, 10));
	}
}

Output :

true
false

4 thoughts on “Java 8 – BiPredicate | Code Factory”

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 )

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