Home core java Java BiPredicate guide + Examples

Java BiPredicate guide + Examples

Java BiPredicate represents a Predicate (boolean-valued function) of two arguments and returns boolean value. This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. BiPredicate functional method is test(Object, Object)

A Basic BiPredicate example :

public class BiPredicateTest {

	public static void main(String[] args) {

		// A basic BiPredicate usage
		BiPredicate<Integer, Integer> p = (n1, n2) -> n1 > n2;

		System.out.println(p.test(20, 10));// true
		System.out.println(p.test(12, 24));// false

	}
}

Output :

true
false

1. Java BiPredicate methods and Examples

  1. boolean test(T t, U u) – Evaluates this predicate on the given arguments.
  2. default BiPredicate or(BiPredicate other) – Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.
  3. default BiPredicate negate() – Returns a predicate that represents the logical negation of this predicate.
  4. default BiPredicate and(BiPredicate other) – Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another

1.1. BiPredicate and() example

The following basic example demonstrates how BiPredicate and() method works.

public class BiPredicateAndTest {

	public static void main(String[] args) {

		BiPredicate<Integer, Integer> bp1 = (n1, n2) -> (n1 % n2 == 0);
		BiPredicate<Integer, Integer> bp2 = (n1, n2) -> (n1 * n2 > 100);

		// n1 should divisible by n2 and n1*n2 greater than 100
		System.out.println(bp1.and(bp2).test(120, 6)); // flase

		BiPredicate<String, String> bp3 = (s1, s2) -> s1.startsWith(s2);
		BiPredicate<String, Integer> bp4 = (s1, s2) -> s1.length() > s2;
		BiPredicate<String, String> bp5 = (s1, s2) -> s1.endsWith(s2);

		// System.out.println(bp3.and(bp4).test("ANAND", "D")); // incompatible types

		// s1 should starts with "P" and ends with "P"
		System.out.println(bp3.and(bp5).test("Peter", "P")); // false

		// s1 should starts with "A" and end with "A"
		System.out.println(bp3.and(bp5).test("ASHJA", "A")); // true
	}
}

Output :

true
false
true

1.2. BiPredicate or() example

The following basic example demonstrates how BiPredicate or() method works.

public class BiPredicateOrTest {

	public static void main(String[] args) {

		BiPredicate<Integer, Integer> bp1 = (n1, n2) -> (n1 % n2 == 0);
		BiPredicate<Integer, Integer> bp2 = (n1, n2) -> (n1 * n2 > 100);

		// n1 should be divisible by n2 or greater than 100
		System.out.println(bp1.or(bp2).test(120, 7)); // true
		System.out.println(bp1.or(bp2).test(13, 5)); // false

		BiPredicate<String, String> bp3 = (s1, s2) -> s1.startsWith(s2);
		BiPredicate<String, Integer> bp4 = (s1, s2) -> s1.length() > 5;
		BiPredicate<String, String> bp5 = (s1, s2) -> s1.endsWith(s2);

		// name starts with "A" or not ends with "p"
		// System.out.println(bp3.or(bp4)); // bp3 and bp4 are incompatible types

		System.out.println(bp3.or(bp5).test("Peter", "r")); // true
		System.out.println(bp3.or(bp5).test("Anand", "A")); // true
	}
}

Output :

true
false
true
true

1.3. BiPredicate negate() example

The following basic example demonstrates how BiPredicate negate() method works.

public class BiPredicateNegateTest {

	public static void main(String[] args) {

		BiPredicate<Integer, Integer> bp1 = (n1, n2) -> (n1 % n2 == 0);
		BiPredicate<Integer, Integer> bp2 = (n1, n2) -> (n1 * n2 > 100);

		// n1 should not be divisible by n2 and n1*n2 not greater than 100
		System.out.println(bp1.negate().test(120, 6)); // flase
		System.out.println(bp2.negate().test(12, 7)); // true

		BiPredicate<String, String> bp3 = (s1, s2) -> s1.startsWith(s2);
		BiPredicate<String, Integer> bp4 = (s1, s2) -> s1.length() > s2;
		BiPredicate<String, String> bp5 = (s1, s2) -> s1.endsWith(s2);

		// s1 should not starts with s2
		System.out.println(bp3.negate().test("ANAND", "D")); // true

		// s1 lengthe should not greater than s2
		System.out.println(bp4.negate().test("PETER", 5)); // true

		// s1 should not ends with s2
		System.out.println(bp5.negate().test("ASHJA", "A")); // false
	}
}
false
true
true
true
false

Conclusion

BiPredicate is a built-in java functional interface which takes two arguments and return a boolean value. In this article we have covered about Java BiPredicate interface and methods of it with basic examples.

References

  1. Java document
  2. Java Predicate interface
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories