Hello readers, this tutorial explains the functional interface Predicate in Java, which has been newly introduced in the
java.util.function package. This package describes the Predicate’s usage with the help of multiple examples.
java.util.function.Predicate is the new functional interface defined in the
java.util.function package. The Predicate is used in all the contexts where an object needs to be evaluated for a given test condition and a boolean value (i.e.
false) needs to be returned.
Since Predicate is a functional interface, hence it can be used as the assignment target for the lambda expressions or the method references.
Here is a simple source code of
boolean test(T t): It is the abstract method which will define the signature of the lambda expression or the method reference and can be assigned to a target of type predicate. This abstract method will always return a Boolean value
Tis the type of the input to the predicate
boolean test(T t)returns
trueif the input argument matches the test condition, otherwise returns
Following are the default methods provided in the Predicate Functional Interface which enable developers to do various types of boolean operations such as AND, OR, NOT (i.e. Negate) with different instances of Predicate.
and(): It does the logical ‘AND ‘ of the predicate on which it is called with another predicate. For e.g.:
or(): It does the logical ‘OR ‘ of the predicate on which it is called with another predicate. For e.g.:
negate(): It does the boolean ‘negation’ of the predicate on which it is invoked. For e.g.:
predicate2 are instances of Predicate interface or Lambda Expression or the Method References. Now, open up the Eclipse Ide and let’s see a few examples of Predicates in Java!
2. Java 8 Predicate Example
2.1 Tools Used
We are using Eclipse Kepler SR2, JDK 8 and Maven.
2.2 Project Structure
Firstly, let’s review the final project structure, in case you are confused about where you should create the corresponding files or folder later!
2.3 Project Creation
This section will demonstrate on how to create a Java-based Maven project with Eclipse. In Eclipse IDE, go to
File -> New -> Maven Project.
In the New Maven Project window, it will ask you to select project location. By default, ‘Use default workspace location’ will be selected. Select the ‘Create a simple project (skip archetype selection)’ checkbox and just click on next button to proceed.
It will ask you to ‘Enter the group and the artifact id for the project’. We will input the details as shown in the below image. The version number will be by default:
Click on Finish and the creation of a maven project is completed. If you observe, it has downloaded the maven dependencies and a
pom.xml file will be created. It will have the following code:
Developers can start adding the dependencies that they want. Let’s start building the application!
3. Application Building
Below are the steps involved in developing this application.
3.1 Java Class Creation
Let’s create the required Java files. Right-click on the
New -> Package.
A new pop window will open where we will enter the package name as:
Once the package is created in the application, we will need to create the implementation class to illustrate the Java 8 Predicate examples. Right-click on the newly created package:
New -> Class.
A new pop window will open and enter the file name as:
PredicateExamples. The utility class will be created inside the package:
3.2.1 Implementation of Predicate Class
To illustrate the Predicate’s usage with the help of multiple examples, developers will have to use ‘ JDK 1.8 ‘. Let’s see the simple code snippet that follows this implementation.
Do remember, developers will have to use the ‘ JDK 1.8 ‘ dependency for implementing the Predicate’s usage in their applications.
4. Run the Application
To run the application, right-click on the
-> Run As -> Java Application. Developers can debug the example and see what happens after every step!
5. Project Demo
The application shows the following logs as output for the
PredicateExamples.java. This following example shows how to use the
test() method of the Predicate interface.
That’s all for this post. Happy Learning!!
That’s all for Java 8 Predicate interface and developers can use it to check whether a lambda expression condition is true or false. I hope this article served you whatever you were looking for.
7. Download the Eclipse Project
This was an example of Java Predicate for beginners.
You can download the full source code of this example here: Java 8 Predicate Example
Last updated on May 26th, 2020