Skip to content

What Is a Predicate Interface In Java?

Gopi Gorantala
Gopi Gorantala
2 min read

Table of Contents

It takes an object and checks to see if that object satisfies some criteria. We use this in matching/filtering data.

The Predicate takes one argument, and returns a Boolean.

The Predicate interface is a functional interface defined in the java.util.function package. It represents a function that takes an input of one type and returns a boolean value, indicating whether the input satisfies a certain condition.

The Predicate interface is commonly used for filtering, testing, or validating elements in collections or streams.

The Predicate interface declares a single abstract method called test(), which takes an argument of the input type and returns a boolean value.

Here's the method signature of the test() method:

boolean test(T t);

The type parameter T represents the type of input to be tested. It can be any valid Java type, including primitive types and objects.

To use the Predicate interface, you can create an instance of it using a lambda expression or method reference that implements the test() method. Here's an example that demonstrates how to use a Predicate to filter even numbers from a list of integers:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class PredicateExample {
  public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

    Predicate<Integer> evenNumberPredicate = num -> num % 2 == 0;

    // Filtering even numbers from the list
    List<Integer> evenNumbers = filter(numbers, evenNumberPredicate);
    System.out.println("Even numbers: " + evenNumbers);
  }

  public static List<Integer> filter(List<Integer> list, Predicate<Integer> predicate) {
    List<Integer> filteredList = new ArrayList<>();
    for (Integer num : list) {
      if (predicate.test(num)) {
        filteredList.add(num);
      }
    }
    return filteredList;
  }
}

In the example, we create a Predicate<Integer> using a lambda expression that tests whether a given integer is even. We then use the filter() method, which takes a list of integers and a Predicate as arguments, and returns a new list containing only the elements that satisfy the predicate.

The Predicate interface is widely used in functional programming, especially in combination with collections, streams, and filtering operations. It allows for expressive and flexible filtering of elements based on various conditions.

Note that the Predicate interface is part of the Java 8 functional interfaces introduced to support lambda expressions and functional programming constructs. It is often used alongside other functional interfaces like Consumer, Function, and Supplier to build complex functional pipelines and perform advanced data processing tasks.

Java Streams APIJava

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Differences Between JDK, JRE, and JVM?

Short answer JDK, JRE, and JVM are essential components of the Java platform, each serving a distinct purpose. Here are the key differences between them: 1. JDK (Java Development Kit): The JDK is used by developers to write, compile, and debug Java code. 2. JRE (Java Runtime Environment): End-users use

Members Public

Difference Between String and char[] in Java

Short answer Strings String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters. A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify

Members Public

What is an Object class in Java?

Short answer Object class is the super class of every class you can create. In Java, a class extends another class using the keyword extends. If you don't have any other class to extend, that's fine. The compiler will make your class extend the Object class.