This post demonstrates how to reuse Predicate
@FunctionalInterface
implementations to reduce duplicate codes in your applications.
java.util.function.Predicate
This functional interface has a number of default methods defined that can be used to extend existing Predicates Lambdas.
67 | default Predicate<T> and(Predicate<? super T> other) { |
Example Codes
PersonBean
First we create a class called PersonBean
that represents a person and has the following few fields.
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 | public class PersonBean { private int age; private String name; private String countryCode; private String state; public PersonBean(int age, String name, String countryCode, String state) { super(); this.age = age; this.name = name; this.countryCode = countryCode; this.state = state; } 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 String getCountryCode() { return countryCode; } public void setCountryCode(String countryCode) { this.countryCode = countryCode; } public String getState() { return state; } public void setState(String state) { this.state = state; } @Override public String toString() { return "PersonBean [age=" + age + ", name=" + name + ", countryCode=" + countryCode + ", state=" + state + "]"; } } |
Main class
This is our main class.
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 | import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; public class CombinePredicateFunction { public static void main(String[] args) { List<PersonBean> personList = Stream.of( new PersonBean(12, "Lance", "MY", "MY-14"), new PersonBean(24, "Martin", "MY", "MY-06"), new PersonBean(25, "Luke", "MY", "MY-10"), new PersonBean(10, "AShley", "US", "GA"), new PersonBean(30, "Kurt", "US", "CA")).collect(Collectors.toList()); Predicate<PersonBean> usOnly = (p) -> p.getCountryCode().equals("US"); Predicate<PersonBean> stateCAOnly = (p) -> p.getState().equals("GA"); // Display all persons from US (country code only) System.out.println(personList.stream().filter(usOnly).collect(Collectors.toList())); // Create a new predicate from the combination of usOnly and stateCAOnly Predicate<PersonBean> usAndCa = usOnly.and(stateCAOnly); System.out.println(personList.stream().filter(usAndCa).collect(Collectors.toList())); // Original predicate not modified System.out.println(personList.stream().filter(usOnly).collect(Collectors.toList())); Predicate<PersonBean> myOnly = (p) -> p.getCountryCode().equals("MY"); Predicate<PersonBean> legalAge = (p) -> p.getAge() >= 18; // List all persons from Malaysia System.out.println(personList.stream().filter(myOnly).collect(Collectors.toList())); Predicate<PersonBean> malaysianLegalAge = myOnly.and(legalAge); // List all persons from Malaysia who are of legal age System.out.println(personList.stream().filter(malaysianLegalAge).collect(Collectors.toList())); } } |
Outputs:
1 2 3 4 5 | [PersonBean [age=10, name=AShley, countryCode=US, state=GA], PersonBean [age=30, name=Kurt, countryCode=US, state=CA]] [PersonBean [age=10, name=AShley, countryCode=US, state=GA]] [PersonBean [age=10, name=AShley, countryCode=US, state=GA], PersonBean [age=30, name=Kurt, countryCode=US, state=CA]] [PersonBean [age=12, name=Lance, countryCode=MY, state=MY-14], PersonBean [age=24, name=Martin, countryCode=MY, state=MY-06], PersonBean [age=25, name=Luke, countryCode=MY, state=MY-10]] [PersonBean [age=24, name=Martin, countryCode=MY, state=MY-06], PersonBean [age=25, name=Luke, countryCode=MY, state=MY-10]] |