BiPredicate is a functional interface that takes two arguments and returns a boolean.
public interface BiPredicate<T, U> {
/**
* Evaluates this predicate on the given arguments.
*
* @param t the first input argument
* @param u the second input argument
* @return {@code true} if the input arguments match the predicate,
* otherwise {@code false}
*/
boolean test(T t, U u);
....
}
Example 1 : test()
BiPredicate object can be implemented with Lambda, and if two arguments are passed to test()
, a boolean is returned.
import java.util.function.BiPredicate;
public class BiPredicateExample1 {
public static void main(String[] args) {
BiPredicate<Integer, Integer> biPredicate = (n1, n2) -> n1 > n2;
boolean result = biPredicate.test(10, 100);
System.out.println(result);
}
}
Output:
false
Example 2 : and()
and()
is a method that chains BiPredicates. AND the results of BiPredicates and return the result.
In the example below, biPredicate1.and(biPredicate2).test(11, 10)
returns true when both biPredicate1 and biPredicate2 are true. If either of them is false, false is returned.
import java.util.function.BiPredicate;
public class BiPredicateExample2 {
public static void main(String[] args) {
BiPredicate<Integer, Integer> biPredicate1 = (n1, n2) -> n1 > n2;
BiPredicate<Integer, Integer> biPredicate2 = (n1, n2) -> n1 * n2 > 100;
boolean result = biPredicate1.and(biPredicate2).test(11, 10);
System.out.println(result);
}
}
Output:
true
Example 3 : or()
or()
is a method that connects BiPredicates like and()
. However, or()
returns the result of the OR operation of BiPredicates.
In the example below, biPredicate1.or(biPredicate2).test(10, 9)
returns True if any one of biPredicate1 and biPredicate2 becomes True.
import java.util.function.BiPredicate;
public class BiPredicateExample3 {
public static void main(String[] args) {
BiPredicate<Integer, Integer> biPredicate1 = (n1, n2) -> n1 > n2;
BiPredicate<Integer, Integer> biPredicate2 = (n1, n2) -> n1 * n2 > 100;
boolean result = biPredicate1.or(biPredicate2).test(10, 9);
System.out.println(result);
result = biPredicate1.or(biPredicate2).test(9, 10);
System.out.println(result);
}
}
Output:
true
false
Example 4 : negate()
negate()
returns a Predicate that returns the opposite of the value returned by BiPredicate.
You can think of the logical operator NOT as predicated before BiPredicate. It can be used to create logically opposite BiPredicates.
import java.util.function.BiPredicate;
public class BiPredicateExample4 {
public static void main(String[] args) {
BiPredicate<Integer, Integer> biPredicate1 = (n1, n2) -> n1 > n2;
BiPredicate<Integer, Integer> biPredicate2 = biPredicate1.negate();
boolean result = biPredicate1.test(10, 9);
System.out.println(result);
result = biPredicate2.test(10, 9);
System.out.println(result);
}
}
Output:
true
false
References
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)