The Functional Intrerface Predicate from the Java 8 java.util.function package has an abstract function test(T t) which takes in only one argument and returns a boolean value. If you have a requirement where you want Predicate to work with two arguments, then you should go for BiPredicate.
BiPredicate is a type of predicate (boolean-valued function) of two arguments. It is the two-arity specialization of Predicate.
It also has the same function called test but takes in two generic type arguments T and U.
Syntax: /**
* Evaluates this predicate on the given arguments.
*
* @param t the first input argument
* @param u the second input argument
* @return: if the input arguments match the predicate,
* otherwise false
*/
boolean test(T t, U u);
Let's take a look at an example of how to work with two arguments/parameters with this predicate.
package org.code2care.examples;
import java.util.function.BiPredicate;
public class PredicateWithTwoArguments {
public static void main(String... args) {
int no1 = 10;
int no2 = 10;
BiPredicate<Integer, Integer> checkEqual = Integer::equals;
if (checkEqual.test(no1, no2)) {
System.out.println(no1 + " and " + no2 + " are equal");
} else {
System.out.println(no1 + " and " + no2 + " are not equal");
}
}
}
Output:
Facing issues? Have Questions? Post them here! I am happy to answer!
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
- Android Launch! The connection to adb is down, and a severe error has occured - Android
- Add Custom External Link to SharePoint Site Navigation - SharePoint
- Visual Studio Code available for Apple Mac ARM64 build for native M1 processor support - Microsoft
- How to Turn on Light/Dark Mode on Xcode IDE - MacOS
- How to open CMD for current file/folder location in Notepad++ - NotepadPlusPlus
- How to uninstall Microsoft Outlook on Mac - MacOS
- How to rename a file using PowerShell - Powershell
- How to Exit a File in Terminal (Bash/Zsh) - Linux