StreamArrayListProcessing.java
1    package arraylists;
2    
3    import java.util.ArrayList;
4    import java.util.List;
5    import java.util.stream.Collectors;
6    
7    public class StreamArrayListProcessing {
8    
9        public static void main (String[] args) {
10           // POINT A: Add some strings which represent names to an ArrayList.
11           List<String> names = new ArrayList<>();
12           names.add("Holiday, Billie");
13           names.add("Claudel, Camille");
14           names.add("Picasso, Pablo");
15           names.add("Gallen-Kallela, Akseli");
16           names.add("Zotto, Osvaldo");
17   
18           //POINT B: Use map and the toList collector to create an ArrayList of just
19           // the first names of the names ArrayList. Use a for loop to print
20           // out the names, separated by spaces.
21           List <String> firstNamesList = names.stream()
22                   .map(n -> firstName(n))
23                   .collect(Collectors.toList());
24           System.out.print("First Names: ");
25           for (String firstName : firstNamesList) {
26               System.out.print(firstName + " ");
27           }
28           System.out.println();
29   
30           //POINT C: Use map and the joinin collector to create a String of just
31           // the first names of the names ArrayList with each name separated
32           // by a comma. Print it.
33           String firstNames = names.stream()
34                   .map(n -> firstName(n))
35                   .collect(Collectors.joining(", "));
36           System.out.println("First Names: " + firstNames);
37   
38           //POINT D: By analogy from point C, print a comma-separated list of the
39           // last names in the names ArrayList.
40           String lastNames = names.stream()
41                   .map(n -> lastName(n))
42                   .collect(Collectors.joining(", "));
43           System.out.println("Last Names: " + lastNames);
44   
45           //POINT E: Print a comma-separated list of all uppercase first names
46           // from the list of names in the names ArrayList.
47           String upperCaseFirstNames = names.stream()
48                   .map(n -> firstName(n))
49                   .map(n -> n.toUpperCase())
50                   .collect(Collectors.joining(", "));
51           System.out.println("Uppercase first names: " + upperCaseFirstNames);
52   
53           //POINT F: Print a comma-separated list of all hyphenated last names
54           // from the list of names in the names ArrayList.
55           String hyphenatedLastNames = names.stream()
56                   .map(n -> lastName(n))
57                   .filter(n -> n.contains("-"))
58                   .collect(Collectors.joining(", "));
59           System.out.println("Hyphenated last names: " + hyphenatedLastNames);
60   
61           //POINT G: Print the integer value of the total length of all names
62           // in the names ArrayList.
63           int totalLength = names.stream()
64                   .map(n -> n.length())
65                   .reduce(0, (n1, n2) -> n1 + n2);
66           System.out.println("Total length: " + totalLength);
67   
68           //POINT H: Print the integer value of the total length of all
69           // first names in the names ArrayList.
70           int totalFirstLength = firstNamesList.stream()
71                   .map(n -> n.length())
72                   .reduce(0, (n1, n2) -> n1 + n2);
73           System.out.println("Total first name length: " + totalFirstLength);
74   
75           //POINT I: Print the integer value of the product of the length
76           // of all first names in the names ArrayList.
77           int number = 1;
78           for (int i = 0; i < firstNamesList.size(); i = i + 1){
79               number = number * firstNamesList.get(i).length();
80           }
81           System.out.println("Product of the length of all first names: " + number);
82   
83       }
84   
85       private static String firstName(String directoryStyleName) {
86           int first = directoryStyleName.indexOf(" ");
87           String f = directoryStyleName.substring(first + 1);
88           return f;
89       }
90   
91       private static String lastName(String directoryStyleName) {
92           int last = directoryStyleName.indexOf(",");
93           String l = directoryStyleName.substring(0,last);
94           return l;
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  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145