Adapted heavily from Java Lambda tutorials
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.Collections; // We used this interface before settling on a functional interface // interface CheckPerson { // public boolean test(Person p); // } public class Person { public enum Sex { MALE, FEMALE } String name; int age; Sex sex; String emailAddress; public int getAge() { return age; } public void printPerson() { System.out.println("Name: " + name + " Sex: " + sex + " Age: " + age + " Email: " + emailAddress); } @Override public String toString(){ return name; } public Sex getSex(){ return sex; } public String getName() { return name; } public Person(String name, Sex sex, int age, String emailAddress){ this.name = name; this.age = age; this.sex = sex; this.emailAddress = emailAddress; } /************************/ /*** Original Version ***/ /************************/ /*public static void printPersonsOlderThan(List<Person> roster, int age){ for (Person p : roster) if (p.getAge() > age) p.printPerson(); } public static void printPersonsAgeBetween(List<Person> roster, int lowage, int highage){ for (Person p : roster) if (p.getAge() > lowage && p.getAge() < highage) p.printPerson(); }*/ /************************************/ /*** Functional Interface Version ***/ /************************************/ public static void printPersonsWithCondition(List<Person> roster, Predicate<Person> checker){ for (Person p : roster) if (checker.test(p)) p.printPerson(); } public static void main(String[] args){ Person dan = new Person("Dan", Sex.MALE, 31, "daniel.schlegel@oswego.edu"); Person phil = new Person("Phil", Sex.MALE, 22, "phil@philsdomain.org"); Person alexis = new Person("Alexis", Sex.FEMALE, 44, "alexis@alexis.jp"); Person julie = new Person("Julie", Sex.FEMALE, 35, "julue@google.com"); List<Person> persons = new ArrayList<>(); persons.add(dan); persons.add(phil); persons.add(alexis); persons.add(julie); System.out.println("Entire List:"); for (Person p : persons) p.printPerson(); /* Original */ // Persons over 30: // System.out.println("Older than 30:"); // printPersonsOlderThan(persons, 30); // System.out.println("Between 18 and 40:"); // printPersonsAgeBetween(persons, 18, 40); /* Lambda */ // Persons over 30: // System.out.println("Older than 30:"); // Predicate<Person> over30 = p -> p.getAge() > 30; // printPersonsWithCondition(persons, over30); // System.out.println("Between 18 and 40:"); // printPersonsWithCondition(persons, // p -> p.getAge() > 18 && p.getAge() < 40); /* Stream */ // Names of persons over 30 System.out.println("Names of persons over 30:"); // Uses peek to check on our intermediate progress. List<String> outlist = persons.stream() .filter(p -> p.getAge() > 30) .peek(Person::printPerson) .map(p -> p.getName()) .peek(System.out::println) .collect(Collectors.toList()); } } |