Java provides the functional interface called predicate that represents a Boolean-valued function predicate with a single argument. The java.util.function package supports the predicate interface and comprises the functional method test(). The predicate functional interface enhances the readability and scalability of the software and simplifies it to divide up units for testing. The prototype of the predicate interface in Java is like this “Predicate<T>” where “T” is denoted as a single parameter producing a Boolean result. Java predicate interface has some methods explored below with the example implementation.
The creation of the predicate interface of Java is demonstrated in the following example. Java Predicates has an abstract method called test(Object) that assesses the predicate on an object as input.
We have first defined a predicate interface with the keyword “predicate” that takes the “integer” type as a single argument. Then, we created the object “p” and specified the predicate condition, which will verify that the given value meets with the predicate and returns the Boolean type. Next, within the predicate “test()” method, the integer value is assigned to evaluate the predicate on this value.
The “false” is output because the predicate is “12>15”.
The aforementioned program explains the creation of predicate in Java. Now, the simple chain of the predicate is implemented in the following example program that filters numbers using the “greater than” and “less than” comparison operators:
We have created the chain of the predicate interface by declaring its objects “p1” and “p2”. The “p1” is given a predicate that will evaluate for greater than the operator. The “p2” object is assigned a less-than-operator sign to evaluate. After that, we have defined an object “res1” where the value is specified inside the test() method to generate the Boolean result after evaluating with both specified predicates. Java’s print operation will print the Boolean-value results.
The first predicate output is true as the predicate value is greater than the predicate test value. The next predicate output has the “false” value as “7” is less than “10”.
Java predicate interface is also provided with different methods. The predicate isEqual() method is utilized here, which checks whether the two objects are equivalent to a value specified as an argument and returns the predicate according to the condition.
We have defined the predicate interface where the object “pr” is declared. The object “pr” is initialized with the Predicate.isEqual() method. The string value is kept inside the isEqual() method to be compared with the other predicate value. For this, we have called the test() method, where the second value is passed. This value will be compared with the predicate value passed as an argument in the isEqual() method and generates the results of comparison as a Boolean value.
Note that the result of the previous program is the “false” predicate because the predicate method is case-sensitive.
There is another Predicate interface method called the predicate.or(). The predicate.or() method works similarly to the “OR” operator. This will return true even if one or two predicate values are true and the other is false.
We created the predicate object “Element1” and “Element2”, where the string predicate is given. We have used the contains() method to specify the strings inside these predicate objects. Then, we called the predicate.or() method with the test() method where the string phrase is set. From these string phrases, the predicate.or() method finds the substrings specified in the predicate objects and generates the Boolean values.
The following predicate output is obtained from the predicate.or() operation:
The stream of elements from the given predicate value can be filtered out with the filter() method of the predicate Interface. This method only accepts the predicate value as an argument and creates a new stream containing the initial stream elements that match the given predicate.
We have created the List object “MyList” and set the list of arrays that contain random integer values. After that, we called the predicate stream() method for getting the stream element from the array, which is greater than a predicate value. The predicate is passed as an input in the filter() method. The stream of filtered elements will be collected, and the toList() method transforms these elements into the list format, which will be printed out on the screen.
The stream of integer values is obtained as an output from the predicate filter() method.
Like the predicate.or() method, Java predicate also provides the predicate.and() method. It provides a composite predicate that logically ANDs the results of both predicates.
We have set the predicate interface, which takes the argument type “integer”. Then, we have defined a predicate interface object “GreaterThan” and the “LessThan” where the predicate is set for the greater than operation and the less than operation. After that, we composed the previous two predicates using the and() method. The and() method is used with the test() method to test with the value specified as a parameter.
The and() method returns the true value of the boolean because the value “30” is greater than “15” and less than “15”.
Now, the predicate negate() method is used to collect all values that don’t fulfill the predetermined criteria.
We have created a predicate variable “startsWithLetter” and provided the predicate where the startsWith() is being called. This method will not return the element that starts with the letter “J”. Then, we established a list of upper-case characters. Then, we invoked the predicate negate() method, which collects the characters that don’t start with the letter “J” and generates the stream of remaining characters in the form of a list.
The output list shows those values started with another character except the character “J”.
Java predicate interface is a crucial component of software testing. Developers mainly use predicates interfaces for any activity which involves analyzing objects by predetermined standards. We have used different predicate interface methods and showcased how these methods performed in Java. These methods include the default logical method and some other predicate functional method.