In this lab you will convert some code from normal iterative pre-java-8 Java to code using lambdas and streams. Before you begin, review the Java tutorials on Lambdas and Streams.
Begin by placing the following code into a file called Person.java in a new Gradle project.
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 |
import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.Collections; import java.util.Set; import java.util.HashSet; import java.util.Comparator; 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); } 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; } public static void printMaleNames(List<Person> persons){ for (Person p : persons) { if (p.getSex() == Person.Sex.MALE) { System.out.println(p.getName()); } } } public static void printUniqueFemaleNamesByLength(List<Person> persons){ // Use a Set to get uniqueness. Set<String> names = new HashSet<>(); for (Person p : persons) if (p.getSex() == Person.Sex.FEMALE) names.add(p.getName()); // Move to List for sorting (sets don't have order!) List<String> uniqueNames = new ArrayList<>(names); Collections.sort(uniqueNames, new Comparator<String>() { public int compare(String s1, String s2) { return s1.length() - s2.length(); }}); // Output for (String s : uniqueNames) System.out.println(s); } 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 julie = new Person("Julie", Sex.FEMALE, 35, "julue@google.com"); Person julie2 = new Person("Julie", Sex.FEMALE, 19, "juliethesecond@facebook.com"); Person alexis = new Person("Alexis", Sex.FEMALE, 44, "alexis@alexis.jp"); Person evie = new Person("Evie", Sex.FEMALE, 25, "evie1997@yahoo.com"); List<Person> persons = new ArrayList<>(); persons.add(dan); persons.add(phil); persons.add(julie); persons.add(julie2); persons.add(alexis); persons.add(evie); System.out.println("--- Male Names ---"); printMaleNames(persons); System.out.println("--- Unique, Sorted by Length, Female Names ---"); printUniqueFemaleNamesByLength(persons); } } |
1. Write the enhanced for statement in the printMaleNames method as a stream pipeline with lambda expressions. Hint: Use the filter intermediate operation and the forEach terminal operation.
2. Convert code in printUniqueFemaleNamesByLength into a new implementation that uses lambda expressions and aggregate operations instead of multiple for loops. Hint: Make a pipeline that invokes the filter, map, distinct, sorted, and forEach operations, in that order.
Once you’ve finished the lab, show it to the TA so that she can mark you as having completed it.