In the below example 1st argument is the string and 2nd argument is the threshold length of the string.
If length of the string is less than the threshold mentioned in the 2nd argument return true or else false.
1
2
3
4
5
6
7
8
9
10
importjava.util.function.BiPredicate;publicclassExamples{publicstaticvoidmain(String[]args){BiPredicate<String,Integer>checkLength=(x,y)->x.length()<y;System.out.println(checkLength.test("harish",10));// true
System.out.println(checkLength.test("this is a very long string",10));//false
}}
Example 3
In the below we check if 1 string is substring of another.
1
2
3
4
5
6
7
8
9
10
importjava.util.function.BiPredicate;publicclassExamples{publicstaticvoidmain(String[]args){BiPredicate<String,String>checkSubstring=(x,y)->x.contains(y);System.out.println(checkSubstring.test("Hello World","lo"));// true
System.out.println(checkSubstring.test("this is a very long string","ong"));//true
}}
Conclusion
I hope this clarifies how to use BiPredicate<T, U> in Java.