Predicate
In scientific logic, a function that accepts an argument and, in return, generates a boolean value as an answer is known as a predicate. Similarly, in the Java programming language, a predicate functional interface of Java is a type of function that accepts a single value or argument and does some sort of processing on it, and returns a boolean (True/ False) answer. The implementation of the Predicate functional interface also encapsulates the logic of filtering (a process that is used to filter stream components on the base of a provided predicate) in Java.
Just like the Consumer functional interface, Predicate functional interface also has some extensions. These are IntPredicate, DoublePredicate, and LongPredicate. These types of predicate functional interfaces accept only primitive data types or values as arguments.
Bi-Predicate – Bi-Predicate is also an extension of the Predicate functional interface, which, instead of one, takes two arguments, does some processing, and returns the boolean value.
Syntax of Predicate Functional Interface –
public interface Predicate<T> { boolean test(T t); }
The predicate functional interface can also be implemented using a class. The syntax for the implementation of predicate functional interface using a class is given below –
public class CheckForNull implements Predicate { @Override public boolean test(Object o) { return o != null; } }
The Java predicate functional interface can also be implemented using Lambda expressions. An example of the implementation of the Predicate functional interface is given below –
Predicate predicate = (value) -> value != null;
This implementation of functional interfaces in Java using Java Lambda expressions is more manageable and effective than the one implemented using a class as both the implementations are doing the same work, i.e., returning the same output.
Functional Interfaces in Java
Java has forever remained an Object-Oriented Programming language. By object-oriented programming language, we can declare that everything present in the Java programming language rotates throughout the Objects, except for some of the primitive data types and primitive methods for integrity and simplicity. There are no solely functions present in a programming language called Java. Functions in the Java programming language are part of a class, and if someone wants to use them, they have to use the class or object of the class to call any function.