Blog Image

Predicate Interface (Java 8 )  

The Functional Interface, PREDICATE is defined in the java.util.Function package.It improves manageability of code and helps in unit-testing them separately.
Syntxa
Interface Predicate<T>

Interface Methods

boolean test(T t)
It evaluates this predicate on the given argument.

default Predicate<T> and(Predicate<? super T> other)  
It returns a composed predicate that represents a short-circuiting logical AND of this predicate and another. When evaluating the composed predicate, if this predicate is false,then the other predicate is not evaluated.

default Predicate<T> negate() 
It returns a predicate that represents the logical negation of this predicate.

static <T> Predicate<T> isEqual(Object targetRef)  
  It returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object)

default Predicate<T> or(Predicate<? super T> other)
 It returns a composed predicate that represents a short-circuiting logical OR of this predicate and another. When evaluating the composed predicate, if this predicate is true,then the other predicate is not evaluated.
Example


/*Java program to demonstrate working of predicates
on collection. The program finds all admins in an arrayList of users.*/

package com.main.predicate;
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;
    }

    @Override
    public String toString() {
        return "User [name=" + name + ", role=" + role + "]";
    }

    public static List<User> process(List<User> users, Predicate<User> predicat) {
        List<User> result = new ArrayList<User>();
        for (User user : users)
            if (predicat.test(user)) {
                result.add(user);
            }
        return result;
    }

    public static void main(String args[]) {
        List<User> users = new ArrayList<User>();
        users.add(new User("Henry", "admin"));
        users.add(new User("Adam", "member"));
        users.add(new User("Peter", "admin"));
        users.add(new User("Scott", "member"));

        List<User> admins = process(users, (User u) -> u.getRole().equals("admin"));
        System.out.println(admins);
    }
}

OutPut

[User [name=Henry, role=admin], User [name=Peter, role=admin]]


Please find executable code predicate.zip in attachment

Attachments

About author

User Image
ShekhFiroz

Key Attributes: Quick Learner, Creative, Innovative, Flexible. Prefer to work in a competitive, motivating and challenging environment that enhances my skills and knowledge so that I am able to shape my creative ideas in the field of software development. Learning by doing in java technology stack :) Spring,Web Services and restfullservices.I am capable to write jobs in distributed framework Hadoop,Hive,Spark.

0

-Comments

Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.