Learnitweb

Java 8 Predicate with example

In mathematics, a predicate is a Boolean-valued function P: X→ {true, false}, called a predicate on X. In Java, we can say that Predicate represents a boolean-valued function of one argument.

Predicate has a functional method test(Object).

Interface Predicate<T> is a predefined functional interface available in package java.util.function.

Predicate’s usage

Predicate can be used to evaluate a condition when evaluation of condition can result either in true or false. Predicate is a functional interface and can refer to lambda expressions. Following are some real world problems where predicate can be used:

  • Check if integer is even.
  • Collection is empty or not.
  • Gender of employee is male or not.
  • Salary of an employee is greater than certain amount or not.

Method summary

As per the Java documentation, following are the methods of Predicate Interface:

default Predicate<T> and(Predicate<? super T> other)Returns a composed predicate that represents a short-circuiting logical AND of this predicate and passed predicate.
default Predicate<T> negate()Returns a predicate that represents the logical negation of this predicate.
default Predicate<T> or(Predicate<? super T> other)Returns a composed predicate that represents a short-circuiting logical OR of this predicate and passed predicate.
boolean test(T t)Evaluates this predicate on the given argument.

A simple example

Let’s use Predicate in a simple example. We’ll check if the number is less than zero or not. The Predicate for this will be:

Predicate<Integer> predicate = i -> i < 0 ;

We’ll use test() method to evaluate this predicate on given argument(in this case, integer to be compared with zero).

import java.util.function.Predicate;

public class PredicateExample {
	public static void main(String args[]) {
		Predicate<Integer> predicate = i -> i < 0;
		System.out.println("9 is less than zero: " + predicate.test(9));
		System.out.println("-1 is less than zero: " + predicate.test(-1));
	}
}

Output:

9 is less than zero: false
-1 is less than zero: true

Predicate using anonymous class

The above predicate can also be written using anonymous class.

Predicate<Integer> predicate = new Predicate<Integer>() {
	@Override
	public boolean test(Integer i) {
		return i < 0 ;
	}
};

Passing predicate as an argument

You can pass Predicate as a function argument too.

public class Test {

	public static void main(String[] args) {
		int number = 9;
		boolean result = compareWithZero(number, i -> i > 100);
		System.out.println(number + " is less than zero :" + result);
	}

	public static boolean compareWithZero(int number, Predicate<Integer> predicate) {
		return predicate.test(number);
	}
}

and() method example

Returns a composed predicate that represents a short-circuiting logical AND of this predicate and passed predicate.

import java.util.function.Predicate;

public class PredicateAndExample {

	public static void main(String[] args) {
		int numberToCheck = 15;
		Predicate<Integer> predicate1 = i -> i > 10;
		Predicate<Integer> predicate2 = i -> i < 20;

		Predicate<Integer> andPredicate = predicate1.and(predicate2);
		boolean rangeCheck = andPredicate.test(numberToCheck);
		System.out.println(numberToCheck + " lies between 10 and 20: " + rangeCheck);
	}
}

Output:

15 lies between 10 and 20: true

or() method example

Returns a composed predicate that represents a short-circuiting logical OR of this predicate and passed predicate.

import java.util.function.Predicate;
 
public class PredicateOrExample {
	
	public static void main(String[] args) {
		String stringToCheck = "sydney";
		Predicate<String> predicate1 = str -> str.startsWith("s");
		Predicate<String> predicate2 = str -> str.startsWith("a");
		
		Predicate<String> andPredicate = predicate1.or(predicate2);
		boolean startsWithCheck = andPredicate.test(stringToCheck);
		System.out.println(stringToCheck + " starts with s or a: "+ startsWithCheck);
	}
}

Output:

sydney starts with s or a: true

negate() method example

Returns a predicate that represents the logical negation of this predicate.

import java.util.function.Predicate;

public class PredicateNegateExample {
	public static void main(String args[]) {
		Predicate<Integer> predicate = i -> i < 0 ;
		System.out.println("9 is less than zero: " + predicate.test(9));
		
		//negate
		Predicate<Integer> negate = predicate.negate();
		System.out.println("test with negate: " + negate.test(9));
	}
}

Output:

9 is less than zero: false
test with negate: true

isEqual() method example

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

import java.util.function.Predicate;

public class PredicateIsEqualExample {

	public static void main(String[] args) {

		Predicate<String> predicate = Predicate.isEqual("hello");

		System.out.println(predicate.test("hello"));
		System.out.println(predicate.test("hello world"));
	}
}

Output:

true
false

Predicate joining

We saw in above examples that two predicates are joined, for example in case of and() method. This is an example of predicate joining.