Today,the Java 8 Predicate Example walks you through step by step to use java.util.function.Predicate is a functional interface introduced in Java 8 and can therefore be used as the assignment target for a lambda expression or method reference. Functional method of Predicate is test(Object) which returns Boolean value.
Using Predicate offers some good advantages:
a) Predicate classes are easy to test and change
b) Optimize the re-usability of source code and, reduce maintenance efforts
c) Separate business layer from operational concerns
Let’s dig deeper via some examples
1 |
Predicate<People> predicateAdultMale = p -> p.getAge() > 18 && p.getGender().equalsIgnoreCase("M"); |
Or
1 |
Predicate<People> predicateWithParam = p -> p.getAge() > age; |
First expression checks for people who are male and greater than 21 year olds. Second expression checks for people who are greater than specified age.
Demo Java 8 Predicate Example
We have a People class like below:
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 |
public class People { public int age; public String gender; public String name; public People(int age, String gender,String name) { this.age = age; this.gender = gender; this.name = name; } public int getAge() { return age; } public String getGender() { return gender; } public String getName() { return name; } @Override public String toString() { return this.name+" Gender:"+this.gender.toString() +" "+this.age+" year olds"; } } |
Let’s build some predicates in util class. Moving your predicates to a Predicate util class offers some good advantages in the long run
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class PredicateUtils { //People are male and age public static Boolean predicateAdultMale(People p1) { Predicate<People> adultMalep = p -> p.getAge() > 18 && p.getGender().equalsIgnoreCase("M"); return adultMalep.test(p1); } //People are male and age public static Predicate<People> predicateAdultMale() { return p -> p.getAge() > 18 && p.getGender().equalsIgnoreCase("M"); } //People are greater than a specified age public static Predicate<People> predicateAgeGreaterThan(int age) { return p -> p.getAge() > age; } //Filter returns a new collection satisfying the predicate public static List<People> filterPeoples(List<People> peoples, Predicate<People> predicate) { return peoples.stream().filter(predicate).collect(Collectors.<People>toList()); } } |
Create main class as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class PredicateDemo { public static void main(String[] args) { //use Predicate on a object People p1 = new People(23,"M","Mr A"); if (predicateAdultMale(p1)) { System.out.println(p1); } People p2 = new People(18,"M","Mr B"); People p3 = new People(27,"M","Mr C"); People p4 = new People(14,"M","Mr D"); //Use Predicate on a collection List<People> peoples = new ArrayList<People>(); peoples.addAll(Arrays.asList(new People[]{p1,p2,p3,p4})); System.out.println(filterPeoples(peoples, predicateAdultMale())); System.out.println(filterPeoples(peoples, predicateAgeGreaterThan(18))); } } |
Run this program and see the output like this:
That’s all for Java 8 Predicate Example.
Reference
Predicate (Java Platform SE 8)
Download complete source code, click link below
Java-8-Predicate-Example.zip (315 downloads)