Signup/Sign In

Java Predicate

In Java, Predicate is an interface that is located in java.util.function package. It is used as an assignment target in lambda expressions and functional interfaces.

In other words, we can say that it represents a boolean value function that returns a boolean value either true or false.

It improves manageability of code, and contain some methods like test(), isEqual(), and(), or(), etc.

We hope, you have the idea of Java lambda expression and functional interface because this topic is co-related, else we recommend you to first read about lambda expression and functional interface from our technical portal.

Declaration

@FunctionalInterface public interface Predicate<T>

Predicate Methods

The following table contains the methods of the Predicate interface. It contains single abstract method test() and other are default and static methods.

Methods

Description

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.

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.

static <T> Predicate<T> isEqual(Object targetRef)

It returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object).

Example:

Let's take an example to assign a lambda expression and call by using the predefined test() method. Since Predicate is a functional interface so it can be represented with a lambda expression. Here, we have two lambda expressions first takes a string argument, and second takes an integer value.

import java.util.function.Predicate;
public class Main {
	public static void main(String[] args){
		Predicate <String> pr = a -> (a == "India");
		Predicate <Integer> pr2 = a -> (a > 21);
		boolean b = pr.test("India");
		System.out.println(b);
		b = pr2.test(20);
		System.out.println(b);
	}
}


true
false

Example: Filter Collection using Predicate

Let's take another example in which we are filtering a list of data using the filter() method. the filter() method belongs to stream and takes a Predicate as an argument. So here, we are passing a predicate as a lambda expression to filter the elements.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
	public static void main(String[] args){
        List<Integer> list = Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80, 90, 100);
        // Predicate in filter() method
        list = list.stream().filter(n->n>60).collect(Collectors.toList());
        System.out.println(list);
	}
}


[70, 80, 90, 100]

Example: Predicate As Method Reference

Since the predicate is a functional interface so we can use it to refer a method as well that is called method reference. To know about method reference, read our post on Java Method Reference.

import java.util.function.Predicate;
public class Main {
	
	static boolean isEligible(int age) {
		if(age>=18)
			return true;
		return false;
	}
	
	public static void main(String[] args){
        Predicate <Integer> voter = Main::isEligible;
        System.out.println(voter.test(16));
	}
}


false

Example: isEqual() Method

It is a static method of the Predicate interface that is used to test the equality of two objects. The object can be a string, integer, or a class object. See the example below.

import java.util.function.Predicate;
public class Main {
	
	public static void main(String[] args){
        Predicate <String> isEqual = Predicate.isEqual("Studytonight");
        System.out.println(isEqual.test("Studytonight"));
        System.out.println(isEqual.test("india"));
	}
}


true
false

Example: And() Method

This method is used to compose two lambda expression that represents a short-circuiting logical AND of this predicate. We can use it to perform logical operations by combining two lambda expressions. See the example below. Here, we used and() method to check whether a number lies between two numeric range or not.

import java.util.function.Predicate;
public class Main {
	
	public static void main(String[] args){
		Predicate<Integer> val1 = x -> (x > 10);
        Predicate<Integer> val2 = x -> (x < 50); 
        System.out.println(val1.and(val2).test(100));  
        System.out.println(val1.and(val2).test(40));
	}
}


false
true



About the author:
I am a Java developer by profession and Java content creator by passion. I have over 5 years of experience in Java development and content writing. I like writing about Java, related frameworks, Spring, Springboot, etc.