Java Predicate is a functional interface as part of the java.util.function package which acts as a general predicate assigned as a target to the lambda expression for referencing any method or evaluating any Boolean based function with the Boolean values of true or false being assigned as a target for any lambda expression or for any method reference; hence it needs a proper evaluating condition based on the assignment of these predicates which helps them to evaluate any condition for specific implementation in terms of programming based on the use cases consisting of objects and the evaluation condition rules for implementation.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
The syntax flow for the predicate is as represented. If you traverse through the syntax, then it can be seemed that it is basically a functional interface that is used as a predicate to the method reference for verifying the testing condition of the body of the program.
package java.util.function; import java.util.Objects; @FunctionalInterface public interface Predicate{ boolean test(T t); // Body of the Program }
Explanation:While traversing through the syntax flow, it can seem that it is basically a functional interface that is used as a predicate to the method reference for verifying the testing condition of the body of the program.
The predicate in java is a savior to the programmers for making and creating codes in more clean and readable formats. It helps in making the test cases formatting better and enhances the test cases. In general terms, Predicate is just a statement in a Boolean format that helps evaluate conditions with constraints. Predicates in java are basically used for implementing the functional interface with the help of assigning the predicate as a value obtained from the java.util.function.package. It makes the package method a target for passing the object of the predicate package to get the Boolean values returned with true or false for the entire method or the function of the codebase. It consists of a test method used to evaluate the entire method with references and the respective functions. In Java, there is no concept of standalone functions; therefore, what it does is defining and creating objects from these interfaces. The Iterable.filter() method can be used in cooperation with the objects of the methods. Lambda Expression with predicate also plays a good role and works easily with the predicates.
There are numerous methods that make use of the Java predicate methods and are represented as follows:
Thus, using these methods with the predicate helps in evaluating any of the conditions defined with the predicate’s method types.
Below are examples mentioned:
This program demonstrates the creation of a simple Predicate and then calling that Predicate method later point of time for evaluation condition with a test method as shown.
Code:
import java.util.function.Predicate; public class Frst_Java_Predicate_Ex { public static void main(String[] args) { Predicateprdc = vl -> (vl > 20); System.out.println(prdc.test(80)); } }
Output:
This program demonstrates the predicate value with the boolean constraint evaluating the marks of the student by using a predicate where a constraint says that if the student secures marks more than 35, then that student passes; otherwise, the student will fail if it returns the value false.
Code:
import java.util.function.Predicate; public class Predict_Java_ex_2 { static Boolean checkMarks(int marks){ if(marks>35) return true; else return false; } public static void main(String[] args){ Predicatepred = Predict_Java_ex_2::checkMarks; boolean rslt = pred.test(15); System.out.println("If marks is more than 35 then only the student will get pass otherwise fail." + rslt); } }
Output:
Java Predicate is one of the new packages and utility being introduced in java 8, which is very flexible with the lambda expressions and helps the programmers create a neat and clean enhanced code for reference and understanding. A predicate with the values helps in the evaluation of the conditions with the return types and values.
The above is the detailed content of Java Predicate. For more information, please follow other related articles on the PHP Chinese website!