Home core java Java predicate with examples

Java predicate with examples

1. What is Java Predicate?

A java Predicate is a functional interface whose functional method is test(Object)that takes an argument and returns boolean value. This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

Simple use case of predicate can be identifying the given number is even or odd, let’s have a look into example without Predicate and with Predicate.

2. Example without Predicate

public class NoPredicateTest {
    
    boolean test(int number) {
        
        if(number % 2 == 0)
            return true;
        
        return false;
    }
    
    public static void main(String[] args) {
        
        NoPredicateTest nptest = new NoPredicateTest();
        System.out.println(nptest.test(10));//true
        System.out.println(nptest.test(11));//false
    }
}

Output :

true
false

3. Examples with Java Predicate

public class PredicateTest {
 
    public static void main(String[] args) {
        
        Predicate<Integer> p = number -> (number % 2 == 0);
        System.out.println(p.test(10));//true
        System.out.println(p.test(11));//false
    }
}

Output :

true
false

Following example shows you filter Stream elements using Predicate and iterate list items using forEach method.. Stream filter method takes predicate argument as input.

public class PredicateStreamFilterTest {
 
    public static void main(String[] args) {
        
    	List<String> names = Arrays.asList(
				 "Peter",
				 "Martin",
				 "Alex",
				 "Philip",
				 "Piyush",
				 "Mike"
				);
    	
        Predicate<String> p = name -> name.startsWith("P");
        
        names.stream()
             .filter(p)
             .forEach(System.out::println);
    }
}

Output :

Peter
Philip
Piyush

4. Java Predicate methods

Modifier and TypeMethod and Description
default Predicateand(Predicate<? super T> other) – Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.
static  PredicateisEqual(Object targetRef) – Returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object).
default Predicatenegate() – Returns a predicate that represents the logical negation of this predicate.
default Predicateor(Predicate<? super T> other) – Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.
booleantest(T t) – Evaluates this predicate on the given argument.

4.1. Predicate.and example

Following example demonstrates that, return true if given number divisible by 2 and greater than 10 using Predicate and also prints names from list if name starts with “P” and does not end with “p”.

public class PredicateAndTest {
 
    public static void main(String[] args) {
    	
    	Predicate<Integer> p1 = number -> (number % 2 == 0);
        Predicate<Integer> p2 = number -> (number > 10);
        
        // number should be divisible by 2 and greater than 10
        System.out.println(p1.and(p2).test(10));//false
        System.out.println(p1.and(p2).test(12));//true
        
        List<String> names = Arrays.asList(
				 "Peter",
				 "Martin",
				 "Alex",
				 "Philip",
				 "Piyush",
				 "Mike"
				);
        
        Predicate<String> p3 = name -> name.startsWith("P");
        Predicate<String> p4 = name -> !name.endsWith("p");
        
        // find a name starts with "P" and not ends with "p"
        names.stream()
             .filter(p3.and(p4))
             .forEach(System.out::println);
    }
}

Output :

false
true
Peter
Piyush

4.2. Predicate.isEqual example

Following example demonstrates that how predicate isEqual(Object) method works.

public class PredicateIsEqualTest {
 
    public static void main(String[] args) {
    
        System.out.println(Predicate.isEqual(10).test(12)); // false
        System.out.println(Predicate.isEqual(10).test(10)); // true
        
        List<String> names = Arrays.asList(
				 "Peter",
				 "Martin",
				 "Alex",
				 "Philip",
				 "Piyush",
				 "Mike"
				);
        
        String MATCH = "Mike";
        
        // find a name that is equals "Mike"
        names.stream()
             .filter(Predicate.isEqual(MATCH))
             .forEach(System.out::println);
    }
}

Output :

false
true
Mike

4.3. Predicate.negate example

Following example demonstrates how Predicate negate() method works.

public class PredicateNegateTest {
 
    public static void main(String[] args) {
    
    	Predicate<Integer> p1 = number -> (number % 2 == 0);
        
        // number should not divisible by 2
        System.out.println(p1.negate().test(10));//false
        System.out.println(p1.negate().test(13));//true
        
        List<String> names = Arrays.asList(
				 "Peter",
				 "Martin",
				 "Alex",
				 "Philip",
				 "Piyush",
				 "Mike"
				);
        
        Predicate<String> p2 = name -> name.startsWith("P");
     
        // find a name not starts with "P"
        names.stream()
             .filter(p2.negate())
             .forEach(System.out::println);
    }
}

Output :

false
true
Martin
Alex
Mike

4.4. Predicate.or example

Following example shows you how java Predicate or method works.

public class PredicateOrTest {
 
    public static void main(String[] args) {
    	
    	Predicate<Integer> p1 = number -> (number % 2 == 0);
        Predicate<Integer> p2 = number -> (number > 10);
        
        // number should be divisible by 2 or greater than 10
        System.out.println(p1.or(p2).test(11)); // true
        System.out.println(p1.or(p2).test(12)); // true
        
        List<String> names = Arrays.asList(
				 "Peter",
				 "Martin",
				 "Alex",
				 "Philip",
				 "Piyush",
				 "Mike"
				);
        
        Predicate<String> p3 = name -> name.startsWith("A");
        Predicate<String> p4 = name -> name.endsWith("p");
        
        // find a name starts with "A" or not ends with "p"
        names.stream()
             .filter(p3.or(p4))
             .forEach(System.out::println);
    }
}

Output :

Rtrue
true
Alex
Philip

References

  1. Java documentation

Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories