Java 8: Predicate – Built-In Functional Interfaces

Predicate Interface is another Built-In Functional Interface of java.util.function package of Java 8. Predicate Interface can be used to evaluate the expression which returns the boolean result like true or false of the given expression. 

Example:

				
					Predicate<Integer> logic = X -> X>10
				
			

In the above statement logic holds the boolean expression and we can pass this logic variable to any filter() method of stream API method or we can use test() to execute it.

Predicate Interface Methods

Since Predicate is a functional interface it has only one abstract method and also some default methods. Below is the list of methods:

				
					boolean test(T t) // abstract method
default Predicate<T> and(Predicate<? super T> other)
default Predicate<T> or(Predicate<? super T> other)
default Predicate<T> negate()
static <T> Predicate<T>	isEqual(Object targetRef)
				
			

Example: Predicate test() Method

test() method used to evaluate the expression with given data. Let’s see the below example of the test() method.

				
					public class PredicateExample
{
    public static void main(String[] args) {
       Predicate<Integer> checkOddEven = x -> x%2==0;
       
       System.out.println("Number 14 is EVEN: "+checkOddEven.test(14));
       System.out.println("Number 18 is ODD: "+checkOddEven.test(18));
       System.out.println("Number 21 is EVEN: "+checkOddEven.test(21));
    }
}
				
			

Output:

				
					Number 14 is EVEN: true
Number 18 is ODD: true
Number 21 is EVEN: false

				
			

Example: Predicate and() Method

and() method is a default method of Predicate interface. and() Method accepts another Predicate as a parameter. It is the replacement of && Operator. In Simple words, if you have multiple Predicate expressions and you want to check if all the Predicate return true.

Example:  Check Number is within the range of 1 to 100

				
					public class PredicateExample
{
    public static void main(String[] args) {
       Predicate<Integer> condition1 = x -> x>=1;
       Predicate<Integer> condition2 = x -> x<=100;
       
       System.out.println("Number 14 : "+condition1.and(condition2).test(14));
       System.out.println("Number 8 : "+condition1.and(condition2).test(8));
       System.out.println("Number 201 : "+condition1.and(condition2).test(201));
    }
}
				
			

Output:

				
					Number 14 : true
Number 8 : true
Number 201 : false

				
			

Example: Predicate or() Method

or() method is the default method of Predicate Interface. Like and() method, this also accepts another Predicate as a parameter. It is the replacement of || operator. when we want any of the expressions to be returned true then we can use or() method.

Example: Check the String starts with “A” or “B”

				
					public class PredicateExample
{
    public static void main(String[] args) {
       Predicate<String> condition1 = x -> x.startsWith("A");
       Predicate<String> condition2 = x -> x.startsWith("B");
       
       System.out.println("Japan : "+condition1.or(condition2).test("Japan"));
       System.out.println("Africa : "+condition1.or(condition2).test("Africa"));
       System.out.println("Brazil : "+condition1.or(condition2).test("Brazil"));
    }
}
				
			

Output:

				
					Japan : false
Africa : true
Brazil : true
				
			

Example: Predicate negate() Method

negate() method is the default method of Predicate Interface. it doesn’t accept any parameter. It is the replacement of ! (not) operator

Example: Check the String that not starts with “A”

				
					public class PredicateExample
{
    public static void main(String[] args) {
       Predicate<String> condition1 = x -> x.startsWith("A");

       System.out.println("Japan : "+condition1.negate().test("Japan"));
       System.out.println("Africa : "+condition1.negate().test("Africa"));
       System.out.println("Brazil : "+condition1.negate().test("Brazil"));
    }
}
				
			

Output:

				
					Japan : true
Africa : false
Brazil : true

				
			

Example: Predicate isEqual() Method

isEqual() method is a static method of Predicate interface and it accepts an object as a parameter and we can use this method to compare two objects are the same or not.

				
					public class PredicateExample
{
    public static void main(String[] args) {
       Predicate<String> condition1 = Predicate.isEqual("Hello");
    
       System.out.println("Hi : "+condition1.test("Hi"));
       System.out.println("Hello : "+condition1.test("Hello"));
       System.out.println("Thank You : "+condition1.test("Thank You"));
    }
}
				
			

Output:

				
					Hi : false
Hello : true
Thank You : false
				
			

Conclusion

Predicate Interface are very useful while working with Stream APIs and other stuffs. In this article we have discussed what is predicate? and examples of predicate methods.

Java 8 Stream APIs

Java 8: Introduction to Streams API

Java 8 got a brand new API called streams. it makes every developer’s life easy. Stream APIs are used to process the group of data. mostly streams are applied on collection objects but we can use stream with any group

Read More »