Java 8 Examples

Java 8 BiPredicate Example

Posted On
Posted By admin

In this blog post, I will be explaining how the Java 8 functional interface BiPredicate works. To know more about functional interfaces, you can refer to this blog post.

The BiPredicate interface provides a method called test. This method accepts two parameters of any data type and returns a boolean. The BiPredicate interface is a specialization of the Predicate interface. While the Predicate interface accepts a single argument of any data type, the BiPredicate interface accepts two parameters of any data type. To see an example of the Predicate interface, refer to this blog post.

BiPredicate method with Two Integer arguments

Consider the following code snippet:


public class BiPredicateDemo {
public static void main(String[] args) {
BiPredicate<String,String> isSubString = (str1,str2) -> {return (str1.indexOf(str2)>0 || str1.startsWith(str2));};
System.out.println("Hello World has substring as Hello = "+isSubString.test("Hello World","Hello"));
System.out.println("Hello World has substring as Test = "+isSubString.test("Hello World","Test"));

}
}

Here, the BiPredicate.test method accepts two string arguments. It checks if the second String is a substring of the first string. If so, it returns true, otherwise, it returns a false. when the above code is executed, it will print the following output:

Hello World has substring as Hello = true
Hello World has substring as Test = false

BiPredicate method with a String and Integer argument

Consider the following code snippet:


public class BiPredicateDemo {
public static void main(String[] args) {
BiPredicate<String,Integer> checkStringLength = (str,length) -> {return (str.length() > length) ;};

System.out.println("Hello World has length greater than 5 = "+checkStringLength.test("Hello World",5));
System.out.println("Hello World has length greater than 12 = "+checkStringLength.test("Hello World",12));

}
}

Here, the BiPredicate.test method accepts a String and an Integer argument. It checks if the length of the input String is greater than the Integer argument. If so it returns true, otherwise, it returns false.  When the above code is executed, it will print the following output:

Hello World has length greater than 5 = true 
Hello World has length greater than 12 = false

You can get the source code for this example along with the code for other Java 8 examples at the Github repository here.


If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my new course Learn Java 8 New Features

If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment