StreamArrayListsProcessing.java
|
1
2 package arraylistplay;
3
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.stream.Collectors;
7
8 public class StreamArrayListsProcessing {
9 public static void main(String[] args) {
10
11 List<String> names = new ArrayList<>();
12 names.add("Holiday, Billie");
13 names.add("Claudel, Camllie");
14 names.add("Picasso, Pablo");
15 names.add("Gallen-Kallela, Akseli");
16 names.add("Zotto, Osvaldo");
17 names.add("Sahalie, Sullivan");
18 names.add("Edward, Sheldon");
19
20 // POINT B: Use map and the toList collector to create an ArrayList of just the first names
21 // of the names ArrayList.
22 // Use a for loop to print out the names, separated by spaces.
23
24
25 List<String> firstNameList = names.stream().map(n -> firstName(n)).collect(Collectors.toList());
26 System.out.print("First names: ");
27 for (String firstName : firstNameList) {
28 System.out.print(firstName + " ");
29 }
30 System.out.println();
31
32
33 // POINT C: Use map and the joining collector to created a String of just the first names
34 // of the names ArrayList
35 // with each name separated by a comma. Print it.
36
37 String firstNames = names.stream().map(n -> firstName(n)).collect(Collectors.joining(", "));
38 System.out.println("First names: " + firstNames);
39
40
41 // POINT D: By analogy from point C, print a comma-separated list of the last names in
42 // the names ArrayList.
43
44 List<String> lastNameList = names.stream().map(n -> lastName(n)).collect(Collectors.toList());
45 System.out.print("Last names: ");
46 for (String lastName : lastNameList) {
47 System.out.print(lastName + " ");
48 }
49 System.out.println();
50 String lastNames = names.stream().map(n -> lastName(n)).collect(Collectors.joining(", "));
51 System.out.println("Last names: " + lastNames);
52
53
54 // POINT E: Print a comma-separated list of all uppercase first names from the list of names
55 // in the names ArrayList.
56 String upperCaseFirstNames = names.stream().map(n -> firstName(n)).map(n -> n.toUpperCase()).collect(Collectors.joining(", "));
57 System.out.println("Uppercase first names: " + upperCaseFirstNames);
58
59
60 // POINT F: Print a comma-separated list of all hyphenated last names from the list of names in
61 // the names ArrayList.
62 String hyphenatedLastNames = names.stream().map(n -> lastName(n)).filter(n -> n.contains("-")).collect(Collectors.joining(", "));
63 System.out.println("HyphenatedLastNames: " + hyphenatedLastNames);
64
65
66 // POINT G: Print the integer value of the total length of all names in the names ArrayList.
67 int totalLength = names.stream().map(n -> n.length()).reduce(0, (n1, n2) -> n1 + n2);
68 System.out.println("Total Length: " + totalLength);
69
70
71 // POINT H: Print the integer value of the total length of all first names in the
72 // names ArrayList.
73 int firstNameTotalLength = firstNameList.stream().map(n -> n.length()).reduce(0, (n1, n2) -> n1 + n2);
74 System.out.println("Total first names length: " + firstNameTotalLength);
75
76
77 // POINT I: Print the integer value of the product of the length of all first names
78 // in the names ArrayList.
79 int namesProductLength = firstNameList.stream().map(n -> n.length()).reduce(1, (n1, n2) -> n1 * n2);
80 System.out.println("Total product of names length: " + namesProductLength);
81 }
82
83 private static String lastName(String directoryStyleName) {
84 int lastNameComma = directoryStyleName.indexOf(",");
85 String lastName = directoryStyleName.substring(0, lastNameComma);
86 return lastName;
87 }
88
89 private static String firstName(String directoryStyleName) {
90 int firstNameComma = directoryStyleName.indexOf(",");
91 String firstName = directoryStyleName.substring(firstNameComma + 2);
92 return firstName;
93 }
94
95 }