VOOZH about

URL: https://www.geeksforgeeks.org/java/java-8-predicate-with-examples/

⇱ Java 8 Predicate with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java 8 Predicate with Examples

Last Updated : 21 Oct, 2022
A Functional Interface is an Interface which allows only one Abstract method within the Interface scope. There are some predefined functional interface in Java like Predicate, consumer, supplier etc. The return type of a Lambda function (introduced in JDK 1.8) is a also functional interface. The Functional Interface PREDICATE is defined in the java.util.function package. It improves manageability of code, helps in unit-testing them separately, and contain some methods like:
  1. isEqual(Object targetRef) : Returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object).
    static Predicate isEqual(Object targetRef)
    Returns a predicate that tests if two arguments are 
    equal according to Objects.equals(Object, Object).
    T : the type of arguments to the predicate
    Parameters:
    targetRef : the object reference with which to 
    compare for equality, which may be null
    Returns: a predicate that tests if two arguments 
    are equal according to Objects.equals(Object, Object)
    
  2. and(Predicate other) : Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.
    default Predicate and(Predicate other)
    Returns a composed predicate that represents a 
    short-circuiting logical AND of this predicate and another.
    Parameters:
    other: a predicate that will be logically-ANDed with this predicate
    Returns : a composed predicate that represents the short-circuiting 
    logical AND of this predicate and the other predicate
    Throws: NullPointerException - if other is null
  3. negate() : Returns a predicate that represents the logical negation of this predicate.
    default Predicate negate()
    Returns:a predicate that represents the logical 
    negation of this predicate
  4. or(Predicate other) : Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.
    default Predicate or(Predicate other)
    Parameters:
    other : a predicate that will be logically-ORed with this predicate
    Returns:
    a composed predicate that represents the short-circuiting 
    logical OR of this predicate and the other predicate
    Throws : NullPointerException - if other is null
  5. test(T t) : Evaluates this predicate on the given argument.boolean test(T t)
    test(T t) 
    Parameters:
    t - the input argument
    Returns:
    true if the input argument matches the predicate, otherwise false

Examples

Example 1: Simple Predicate Output:
True
Example 2: Predicate Chaining Output:
True
False
Example 3: Predicate in to Function Output:
Number 10
Example 4: Predicate OR Output:
True
Example 5: Predicate AND Output:
False
True
Example 6: Predicate negate() Output:
False
Example 7: Predicate in Collection Output:
[User Name : John, Role :admin]
The same functionality can also be achieved by using Stream API and lambda functions offered since JDK 1.8 on top of the Collections API. The Stream API allows "streaming" of collections for dynamic processing. Streams allow concurrent and parallel computation on data (using internal iterations), to support database-like operations such as grouping and filtering the data (similar to GROUP BY and WHERE clause in SQL). This allows the developers to focus on "what data is needed" instead of "how data is needed" since streaming hides the details of the implementation and provides the result. This is done by providing predicates as inputs to functions operating at runtime upon the streams of collections. In the following example, we illustrate how Stream API can be used along with predicates to filter the collections of data as achieved in Example 7.
Output:
[User Name : John, Role :admin]
Comment
Article Tags:
Article Tags: