StreamArrayListProcessing.java
1    package arraylists;
2    
3    import java.util.ArrayList;
4    import java.util.List;
5    import java.util.StringJoiner;
6    import java.util.stream.Collectors;
7    import java.util.stream.Stream;
8    
9    public class StreamArrayListProcessing {
10   
11       public static void main(String[] args) {
12           //Point A: Add some strings which represent names to an ArrayList
13           List<String> names = new ArrayList<>();
14           names.add("Holiday, Billie");
15           names.add("Claudel, Camille");
16           names.add("Picasso, Pablo");
17           names.add("Gallen-Kallela, Akseli");
18           names.add("Zotto, Osvaldo");
19           names.add("Hurts, Jalen");
20           names.add("Burrow, Joe");
21           names.add("Allen, Josh");
22   
23           //Point B: USe map and the toList collector to create an ArrayList of just the first names of the names ArrayList
24           // Use a for loop to print out the names
25   
26           List<String> firstNamesList = names.stream()
27                   .map(n -> firstName(n))
28                   .collect(Collectors.toList());
29   
30           System.out.print("First Names: ");
31           for (String firstName : firstNamesList) {
32               System.out.println(firstName);
33           }
34           System.out.println();
35   
36           //Point C: Use map and the joining collector to create a String of just the first names of the names ArrayList
37           //with each name seperated by a comma. Print it
38           String firstNames = names.stream()
39                   .map(n -> firstName(n))
40                   .collect(Collectors.joining(", "));
41   
42           System.out.println("First names: " + firstNames);
43   
44           //Point D: By analogy, print a comma seperated list of the last names in the names ArrayList
45   
46           String lastNames = names.stream()
47               .map(n -> lastName(n))
48               .collect(Collectors.joining(", "));
49   
50           System.out.println("Last names: " + lastNames);
51           //Point E: Print a comma seperated list of all uppercase first names in the names ArrayList
52   
53           String uppercaseFirstNames = names.stream()
54                   .map(n -> firstName(n))
55                   .map(n -> n.toUpperCase())
56                   .collect(Collectors.joining(", "));
57   
58           System.out.println("Uppercase first names: " + uppercaseFirstNames);
59           //Point F: Print a comma seperated list of all hyphenated last names in the names ArrayList
60           String hyphenatedLastNames = names.stream()
61                   .map(n -> lastName(n))
62                   .filter(n -> n.contains("-"))
63                   .collect(Collectors.joining(", "));
64   
65           System.out.println("Last names containing a hyphen: " + hyphenatedLastNames);
66           //Point G Refinement: Print the interger value of the total length of all names in the names ArrayList
67           int totalLength = names.stream()
68                   .map(n -> n.length())
69                   .reduce(0, (n1, n2) -> n1 + n2);
70   
71           System.out.println("Total Length: " + totalLength);
72           //Point H: Print the interger value of the total length of all first names in the names Array List
73           int totalFirstName = names.stream()
74                   .map(n -> firstName(n))
75                   .map(n -> n.length())
76                   .reduce(0, (n1,n2) -> n1 + n2);
77   
78           System.out.println("Total length of first names: " + totalFirstName);
79           //Point I: Print the interger value of the product of the lengths of each of the names
80           int productofNames = names.stream()
81                   .map(n -> n.length())
82                   .reduce(0,(n1,n2) -> n1 * n2);
83   
84           System.out.println("Product of Names: " + productofNames);
85   }
86       private static String firstName(String directoryStyleName) {
87           int commaPosition = directoryStyleName.indexOf(",");
88           String first = directoryStyleName.substring(commaPosition + 2);
89   
90           return first;
91   }
92       private static String lastName(String directoryStyleNames) {
93           int commaPosition = directoryStyleNames.indexOf(",");
94           String last = directoryStyleNames.substring(0,commaPosition);
95   
96           return last;
97   }
98   }