Predicate Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function expect an input but produces a boolean value as an output. As it is a functional interface, it can be used assignment target for lambda expression or method reference. In this post, we are going to see several implementations of Predicate Interface by using different examples.
Predicate Interface in Java 8 with Examples...!!! Click To Tweet
Look at Predicate Javadoc description below:
Predicate Interface contains 5 methods:
test
and
or
negate
isEqual
Let’s discuss these methods:
test
This method performs operation on the given argument and results in a boolean value.
1 | boolean test(T t); |
and
This method 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.
1 2 3 4 | default Predicate<T> and(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) && other.test(t); } |
or
This method 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.
1 2 3 4 | default Predicate<T> or(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) || other.test(t); } |
negate
This method returns a predicate that represents the logical negation of this predicate.
1 2 3 | default Predicate<T> negate() { return (t) -> !test(t); } |
isEqual
This method returns a predicate that tests if two arguments are equal.
1 2 3 4 5 | static <T> Predicate<T> isEqual(Object targetRef) { return (null == targetRef) ? Objects::isNull : object -> targetRef.equals(object); } |
Lets understand above mentioned methods thorough various examples:
Example 1. Simple Predicate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.function.Predicate; public class PredicateInterfaceJava8Example1 { public static void main(String[] args) { System.out.println("Predicate Interface - Java 8 - Example : Simple Predicate"); Predicate <Integer> p = i -> i == 10; System.out.println("Is number matching: " + p.test(5)); } } |
Example 2. Predicate Chaining with ‘and’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.function.Predicate; public class PredicateInterfaceJava8Example2 { public static void main(String[] args) { System.out.println("Predicate Interface - Java 8 - Example : Predicate Chaining"); Predicate <Integer> valueNotMatches10 = i -> i != 10; Predicate <Integer> valueGreatedThan5 = i -> i > 5; //Predicate chaining : Example with 'and' predicate boolean result = valueNotMatches10.and(valueGreatedThan5).test(10); System.out.println(result); } } |
Example 3. Passing Predicate into a Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import java.util.function.Predicate; public class PredicateInterfaceJava8Example3 { public static void main(String[] args) { System.out.println("Predicate Interface - Java 8 - Example : Passing Predicate into a Function"); Predicate <Integer> p = i -> i == 10; checkIfValueMatches(5, p); } private static void checkIfValueMatches(int i, Predicate <Integer> p) { System.out.println("Does value matching: " + p.test(i)); } } |
Example 4. ‘or’ with Anonymous/Lambda Predicate Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import java.util.function.Predicate; public class PredicateInterfaceJava8Example4 { public static void main(String[] args) { System.out.println("Predicate Interface - Java 8 - Example : OR & Anonymous/Lambda Predicate Implementations"); //Predicate anonymous implementation Predicate <String> startsWithSpecificString = new Predicate <String> () { @Override public boolean test(String t) { return t.startsWith("is"); } }; //Predicate lambda implementation Predicate <String> containsSpecificString = s -> s.contains("dur"); //OR condition boolean result = startsWithSpecificString.or(containsSpecificString).test("isha durani"); System.out.println(result); } } |
Example 5. Predicate ‘negate’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.function.Predicate; public class PredicateInterfaceJava8Example7 { public static void main(String[] args) { Predicate <String> predicate = s -> s.equals("Hello Predicate"); //NEGATE operation Predicate <String> predicateNegate = predicate.negate(); System.out.println(predicateNegate.test("Hello Predicate")); } } |
Example 6. Predicate List Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; public class PredicateInterfaceJava8Example5 { public static void main(String[] args) { System.out.println("Predicate Interface - Java 8 - Example : Predicate List Example\n"); Predicate <List<Integer>> listPredicateContains5 = a -> a.contains(5); List <Integer> l = new ArrayList <Integer> (); l.add(19); l.add(4); l.add(83); l.add(5); l.add(63); checkIfValuePresentInList(l, listPredicateContains5); } public static void checkIfValuePresentInList(List <Integer> list, Predicate <List<Integer>> p) { if (p.test(list)) { System.out.println("Success: List contains the expected value which is number 5"); } else { System.out.println("Failure: List doens't contains the expected value which is number 5"); } } } |
Example 7. with custom Class object (like ‘Employee’ in this case)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import java.util.function.Predicate; public class PredicateInterfaceJava8Example6 { public static void main(String[] args) { System.out.println("Predicate Interface - Java 8 - Example : Predicate Custom Class Object Example\n"); Predicate <Employee> employeePredicate = p -> p.getAge() > 50; Employee e1 = new Employee(15, "Tom Marks"); Employee e2 = new Employee(6, "Tom Marks"); Employee e3 = new Employee(88, "Tom Marks"); System.out.println("Doesn age of First Employee greater than 50? : " + employeePredicate.test(e1)); System.out.println("Doesn age of Second Employee greater than 50? : " + employeePredicate.test(e2)); System.out.println("Doesn age of Third Employee greater than 50? : " + employeePredicate.test(e3)); } } class Employee { int age; String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Employee(int age, String name) { this.age = age; this.name = name; } } |
Predicate Interface in Java 8 with Examples...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Supplier Interface in Java 8 with Examples