Java8 - java.util.function.Predicate Interface tutorials Examples


The predicate is a functional interface introduced in java8. It has only one abstract method. Predicate interface is in java.util.function package and it takes single argument. It evaluates the predicate on given parameter

public interface Predicate<T> {  
 boolean test(T t)   
}  

T is input value test(T t) - Only single abstract method. return true if the given input value matches condition, else return false

Predicate Test method example

import java.util.function.Predicate;  
  
public class PredicateDemo {  
  
 public static void main(String[] args) {  
  Predicate<Integer> predicateFunction = num -> (num > 0);  
  System.out.println(predicateFunction.test(-1));  
  System.out.println(predicateFunction.test(50));  
 }  
}  

and output of the above code is

false true  

Predicate test static method Example

import java.util.function.Predicate;  
  
public class PredicateDemo {  
 static Boolean nullCheck(Object obj) {  
  if (obj == null)  
   return true;  
  else  
   return false;  
 }  
  
 public static void main(String[] args) {  
  // Predicate<Integer> predicateFunction = num -> (num > 0)  
  Predicate<Object> predicate = PredicateDemo::nullCheck;  
  
  System.out.println(predicate.test(null));  
  System.out.println(predicate.test(50));  
  
 }  
  
}  

and output is

true  
false  

Predicate Default methods

Method

Description

and()

return the result of Short circuit logical AND operator of given input parameters

negate()

Applies logical negate of the predicate

or()

return the result of Short circuit logical OR operator of given input parameters

Default methods Usage example

import java.util.function.Predicate;  
  
public class PredicateDemo {  
  
 public static void main(String[] args) {  
        Predicate<Integer> greaterThanFifity = (i) -> i > 50;  
  
        Predicate<Integer> lowerThanHundred = (i) -> i < 100;   
        boolean output = greaterThanFifity.and(lowerThanHundred).test(55);  
        System.out.println(output);  
   
        boolean output1 = greaterThanFifity.and(lowerThanHundred).negate().test(55);  
        System.out.println(output1);  
        boolean output2 = greaterThanFifity.or(lowerThanHundred).test(55);  
        System.out.println(output1);  
 }  
  
}  

and output is

true  
false  
false  

Predicate lambda express method reference example

Example1 is an exammple for usage of lambda expressions Example2 is an example of method reference. Methods are called using double colon:: symbol

package org.cloudhadoop.functions;  
import java.util.function.Predicate;  
public class MyPredicateExample {  
	public static Boolean isPositiveNumber(int number) {  
		return number > 0 ? true : false;  
	}  
	public static void main(String[] args) {  
                 // Example1  
		Predicate<Integer> lambdaPredicate = (n) -> MyPredicateExample.isPositiveNumber(n);  
		System.out.println(lambdaPredicate.test(45));  
                 // Example2  
		Predicate<Integer> methodRefPredicate = MyPredicateExample::isPositiveNumber;  
		System.out.println(methodRefPredicate.test(-35));  
	}  
}  

Output is

true  
false  

Similar Posts