Home> Java> javaTutorial> body text

Java Predicate

王林
Release: 2024-08-30 15:58:16
Original
947 people have browsed it

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

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 }
Copy after login

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.

  • package java.util.function:This represents the package for the function used to support the methods and functions related to the predicate function.
  • import java.util.Objects:This imported package is responsible for supporting the objects created with respect to the functional interface.
  • @FunctionalInterface: This represents the annotation with respect to the spring boot application that the application or the interface support it makes use of is a predicate which is the functional interface.
  • public interface Predicate : This represents that the interface type supporting the current class or the method is a predicate interface.
  • boolean test(T t):This method is part of the predicate interface. It is an abstract method used for defining and evaluating the significance of the lambda expression or the method reference used for assigning a target of type predicate. This method will have a Boolean as a return type. Also, T is the argument passed which is the input type for the predicate as an interface. Overall this test method will return true or false based on the satisfaction of the evaluation condition.

How does Predicate work in Java?

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.

Methods in Java Predicate

There are numerous methods that make use of the Java predicate methods and are represented as follows:

  • boolean test(T t ):This method is used for evaluating the present predicate based on the passed argument t.
  • default Predicate and(Predicate other): The return type of this method is the composed predicate based on the AND logical operator of short-circuiting that will predicate and then the value for the other predicate if throws the value as false then there will be no evaluation with any argument or other logical operators.
  • default Predicate negate(): Any negated value, i.e. a negative value, will get returned using the default Predicate negate method whenever defined within the package.
  • default Predicate or(Predicate other): This behavior is also the same as and default predicate with a mere difference that the short-circuit will predicate and follow the value for the other predicate and is any of the value gets out to be true then only the other value will become false.
  • static Predicate isEqual(Object targetRef): This method returns a predicate value which will be used for testing whether the two objects have similar values or both have some transformed values.

Thus, using these methods with the predicate helps in evaluating any of the conditions defined with the predicate’s method types.

Examples to Implement Java Predicate

Below are examples mentioned:

Example #1

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) { Predicate prdc = vl -> (vl > 20); System.out.println(prdc.test(80)); } }
Copy after login

Output:

Java Predicate

Example #2

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){ Predicate pred = 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); } }
Copy after login

Output:

Java Predicate

Conclusion

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!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!