Java BiPredicate Interface with examples

 327 total views,  1 views today

Welcome to Java 8 Series. In this tutorial, you will learn about the Java BiPredicate Interface with examples.

BiPredicate is a Functional interface introduced as part of Java 8. It is available in java.util.function package.

What is the use of BiPredicate?

This interface is mainly used for evaluating two types of inputs and returns an output of type boolean. Consider a case where we need to write a lambda expression which accepts one input of any type and perform an operation on those inputs and return a boolean.

Example

public class StaticReference {

	public static void main(String[] args) {
		Operation ops = (boolean flag, boolean flag2) -> {
			return flag & flag2;
		};
		boolean result = ops.validate(true, true);
		System.out.println(result);
	}

	interface Operation {
		boolean validate(boolean flag, boolean flag2);
	}

}

When you look at the above example, we have a function interface with an abstract method which accepts two arguments of type boolean return a boolean value. So a lambda expression needs a target type which needs to be a functional interface and in this example, it is Operation interface.

Instead of creating our own function interface for this scenario, Java has provided us a BiPredicate interface. Let now see in-depth details on Java BiPredicate Interface with examples.

BiPredicate Interface

@FunctionalInterface
public interface BiPredicate<T,U>

BiPredicate interface contains an operation that accepts two input arguments of any type and returns a boolean value

It is a functional interface and the functional method is test(Object, Object).

Method description

boolean test(T t,U u)
t - the first input argument
u - the second input argument
Returns:
    true if the input arguments match the predicate, otherwise false

Example of BiPredicate interface using lambda

public class StaticReference {

  public static void main(String[] args) {
      BiPredicate<String, String> biPredicate = (String flag, String flag2) -> {
		return flag.equalsIgnoreCase(flag2);
		};
		boolean result = biPredicate.test("apple", "APPLE");
		System.out.println(result);
	}

}

Example of BiPredicate interface using Method reference

public class StaticReference {

	public static void main(String[] args) {
		BiPredicate<String, String> biPredicate = StaticReference::validate;
		boolean result = biPredicate.test("apple", "APPLE");
		System.out.println(result);
	}

	static boolean validate(String flag, String flag2) {
		return flag.equalsIgnoreCase(flag2);
	}
}

If you want to write a lambda expression with two arguments of any type and boolean result, please use Java BiPredicate interface.

References

Java 8 BiPredicate official documentation

Thanks for reading. Please a comment and give us a thumbs up.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.