Predicate

The java.util.function.Predicate<T> Interface has one abstract method named test that accepts an object of generic type T and returns a boolean

@FunctionalInterface
public interface Predicate<T> {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the 
     * predicate,
     * otherwise {@code false}
     */
    boolean test(T t);
}

Usage:

It can be used to represent a boolean expression that uses object of type T. 

Example:

Here we have implemented Predicate Interface by lambda implementation and passed Predicate implementation in method, the method validateSymbol will check “name” argument value using the implementation passed as an argument and will evaluate to true or false.

Using Predicate we can decouple the logic to test and pass it as an argument as we are seeing here.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *