Guest User

Untitled

a guest
Apr 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. public class PredicateDemo {
  2.  
  3. static class Person {
  4. String name;
  5. int age;
  6. String gender;
  7.  
  8. public Person(String name, int age, String gender) {
  9. this.name = name;
  10. this.age = age;
  11. this.gender = gender;
  12. }
  13.  
  14. @Override
  15. public String toString() {
  16. return "Person{" +
  17. "name='" + name + '\'' +
  18. ", age=" + age +
  19. ", gender=" + gender +
  20. '}';
  21. }
  22.  
  23. public static void showPersons(List<Person> list, Predicate<Person> condition) {
  24. for (Person p : list)
  25. if (condition.test(p)) System.out.println(p.toString());
  26. }
  27. }
  28.  
  29. public static void main(String... args) {
  30. List<Person> personList = new ArrayList<>();
  31. personList.add(new Person("Mark Zuckerberg", 33, "M"));
  32. personList.add(new Person("Bill Gates", 62, "M"));
  33. personList.add(new Person("Marissa Mayer", 42, "F"));
  34. personList.add(new Person("Satya Nadella", 50, "M"));
  35. personList.add(new Person("Sundar Pichai", 45, "M"));
  36. personList.add(new Person("Sergey Brin", 44, "M"));
  37. personList.add(new Person("Larry Page", 45, "M"));
  38.  
  39. Predicate<Person> isFemale = (p) -> p.gender.equals("F");
  40. Predicate<Person> isYoung = (p) -> p.age <= 35;
  41. Predicate<Person> isAged = (p) -> p.age >= 45;
  42.  
  43. System.out.println("Female persons: ");
  44. showPersons(personList, isFemale);
  45.  
  46. System.out.println("Young persons: ");
  47. showPersons(personList, isYoung);
  48.  
  49. System.out.println("Aged persons: ");
  50. showPersons(personList, isAged);
  51. }
  52. }
Add Comment
Please, Sign In to add comment