Open In App

Java 8 Predicate with Examples

Last Updated : 21 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

Java




// Java program to illustrate Simple Predicate
  
import java.util.function.Predicate;
public class PredicateInterfaceExample1 {
    public static void main(String[] args)
    {
        // Creating predicate
        Predicate<Integer> lesserthan = i -> (i < 18); 
  
        // Calling Predicate method
        System.out.println(lesserthan.test(10)); 
    }
}



Output:

True

Example 2: Predicate Chaining

Java




// Java program to illustrate Predicate Chaining
  
import java.util.function.Predicate;
public class PredicateInterfaceExample2 {
    public static void main(String[] args)
    {
        Predicate<Integer> greaterThanTen = (i) -> i > 10;
  
        // Creating predicate
        Predicate<Integer> lowerThanTwenty = (i) -> i < 20
        boolean result = greaterThanTen.and(lowerThanTwenty).test(15);
        System.out.println(result);
  
        // Calling Predicate method
        boolean result2 = greaterThanTen.and(lowerThanTwenty).negate().test(15);
        System.out.println(result2);
    }
}



Output:

True
False

Example 3: Predicate in to Function

Java




// Java program to illustrate 
// passing Predicate into function
  
import java.util.function.Predicate;
class PredicateInterfaceExample3 {
    static void pred(int number, Predicate<Integer> predicate)
    {
        if (predicate.test(number)) {
            System.out.println("Number " + number);
        }
    }
    public static void main(String[] args)
    {
        pred(10, (i) -> i > 7);
    }
}



Output:

Number 10

Example 4: Predicate OR

Java




// Java program to illustrate OR Predicate
  
import java.util.function.Predicate;
class PredicateInterfaceExample4 {
    public static Predicate<String> hasLengthOf10 = new Predicate<String>() {
        @Override
        public boolean test(String t)
        {
            return t.length() > 10;
        }
    };
  
    public static void predicate_or()
    {
  
        Predicate<String> containsLetterA = p -> p.contains("A");
        String containsA = "And";
        boolean outcome = hasLengthOf10.or(containsLetterA).test(containsA);
        System.out.println(outcome);
    }
    public static void main(String[] args)
    {
        predicate_or();
    }
}



Output:

True

Example 5: Predicate AND

Java




// Java program to illustrate AND Predicate
  
import java.util.function.Predicate;
import java.util.Objects;
  
class PredicateInterfaceExample5 {
    public static Predicate<String> hasLengthOf10 = new Predicate<String>() {
        @Override
        public boolean test(String t)
        {
            return t.length() > 10;
        }
    };
  
    public static void predicate_and()
    {
        Predicate<String> nonNullPredicate = Objects::nonNull;
  
        String nullString = null;
  
        boolean outcome = nonNullPredicate.and(hasLengthOf10).test(nullString);
        System.out.println(outcome);
  
        String lengthGTThan10 = "Welcome to the machine";
        boolean outcome2 = nonNullPredicate.and(hasLengthOf10).
        test(lengthGTThan10);
        System.out.println(outcome2);
    }
    public static void main(String[] args)
    {
        predicate_and();
    }
}



Output:

False
True

Example 6: Predicate negate()

Java




// Java program to illustrate 
// negate Predicate
  
import java.util.function.Predicate;
class PredicateInterfaceExample6 {
    public static Predicate<String> hasLengthOf10 = new Predicate<String>() {
        @Override
        public boolean test(String t)
        {
            return t.length() > 10;
        }
    };
  
    public static void predicate_negate()
    {
  
        String lengthGTThan10 = "Thunderstruck is a 2012 children's "
                                + "film starring Kevin Durant";
  
        boolean outcome = hasLengthOf10.negate().test(lengthGTThan10);
        System.out.println(outcome);
    }
    public static void main(String[] args)
    {
        predicate_negate();
    }
}



Output:

False

Example 7: Predicate in Collection

Java




// Java program to demonstrate working of predicates
// on collection. The program finds all admins in an
// arrayList of users.
import java.util.function.Predicate;
import java.util.*;
class User
{
    String name, role;
    User(String a, String b) {
        name = a;
        role = b;
    }
    String getRole() { return role; }
    String getName() { return name; }    
    public String toString() {
       return "User Name : " + name + ", Role :" + role;
    }
  
    public static void main(String args[])
    {      
        List<User> users = new ArrayList<User>();
        users.add(new User("John", "admin"));
        users.add(new User("Peter", "member"));
        List admins = process(users, (User u) -> u.getRole().equals("admin"));
        System.out.println(admins);
    }
  
    public static List<User> process(List<User> users, 
                            Predicate<User> predicate)
    {
        List<User> result = new ArrayList<User>();
        for (User user: users)        
            if (predicate.test(user))            
                result.add(user);
        return result;
    }
}



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.

Java




// Java program to demonstrate working of predicates
// on collection. The program finds all admins in an
// arrayList of users.
import java.util.function.Predicate;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
  
class User
{
    String name, role;
    User(String a, String b) {
        name = a;
        role = b;
    }
    String getRole() { return role; }
    String getName() { return name; } 
    public String toString() {
    return "User Name : " + name + ",
    Role :" + role;
    }
  
    public static void main(String args[])
    
        List<User> users = 
            new ArrayList<User>();
        users.add(new User("John", "admin"));
        users.add(new User("Peter", "member"));
          
    // This line uses Predicates to filter
    // out the list of users with the role "admin".
    // List admins = process(users, (User u) -> 
    // u.getRole().equals("admin"));
  
    // Replacing it with the following line 
    // using Stream API and lambda functions 
    // produces the same output
      
    // the input to the filter() is a lambda 
    // expression that returns a predicate: a 
    // boolean value for each user encountered 
    // (true if admin, false otherwise)
    List admins = users.stream()
    .filter((user) -> user.getRole().equals("admin"))
    .collect(Collectors.toList());
          
    System.out.println(admins);
    }
}


Output:

[User Name : John, Role :admin]


Previous Article
Next Article

Similar Reads

Stream iterate(T,Predicate,UnaryOperator) method in Java with examples
The iterate(T, java.util.function.Predicate, java.util.function.UnaryOperator) method allows us to iterate stream elements till the specified condition. This method returns a sequential ordered Stream produced by iterative application of the given next function to an initial element, conditioned on satisfying hasNext predicate passed as parameter.
2 min read
Predicate.not() Method in Java with Examples
In order to negate an existing predicate, the Predicate.not() static method added to Java 11. The Predicate class is present in java.util.function package. Syntax: negate = Predicate.not( positivePredicate ); Parameters: Predicate whose negate is required Return Type: Return type not() method is Predicate. Approach: Create one predicate and initial
1 min read
Remove elements from a List that satisfy given predicate in Java
Below are the methods to efficiently remove elements from a List satisfying a Predicate condition: p ==&gt; Predicate, specifying the condition l ==&gt; List, from which element to be removed Using iterator Below program demonstrates the removal of null elements from the list, using the Predicate Java Code // Java Program to remove nulls // from a
5 min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
Java 8 | Consumer Interface in Java with Examples
The Consumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one argument and produces a result. However these kind of functions don't return any value.Hence this functional interface which takes in one generic namely:-
4 min read
Java 8 | ArrayDeque removeIf() method in Java with Examples
The removeIf() method of ArrayDeque is used to remove all those elements from ArrayDeque which satisfies a given predicate filter condition passed as a parameter to the method. This method returns true if some element are removed from the Vector. Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition che
3 min read
Java lang.Long.lowestOneBit() method in Java with Examples
java.lang.Long.lowestOneBit() is a built-in method in Java which first convert the number to Binary, then it looks for first set bit present at the lowest position then it reset rest of the bits and then returns the value. In simple language, if the binary expression of a number contains a minimum of a single set bit, it returns 2^(first set bit po
3 min read
Java lang.Long.numberOfTrailingZeros() method in Java with Examples
java.lang.Long.numberOfTrailingZeros() is a built-in function in Java which returns the number of trailing zero bits to the right of the lowest order set bit. In simple terms, it returns the (position-1) where position refers to the first set bit from the right. If the number does not contain any set bit(in other words, if the number is zero), it r
3 min read
Java lang.Long.numberOfLeadingZeros() method in Java with Examples
java.lang.Long.numberOfLeadingZeros() is a built-in function in Java which returns the number of leading zero bits to the left of the highest order set bit. In simple terms, it returns the (64-position) where position refers to the highest order set bit from the right. If the number does not contain any set bit(in other words, if the number is zero
3 min read
Java lang.Long.highestOneBit() method in Java with Examples
java.lang.Long.highestOneBit() is a built-in method in Java which first convert the number to Binary, then it looks for the first set bit from the left, then it reset rest of the bits and then returns the value. In simple language, if the binary expression of a number contains a minimum of a single set bit, it returns 2^(last set bit position from
3 min read
Article Tags :
Practice Tags :