StreamArrayListProcessing.java
1    /* 
2     * A program to preform some basic operations on a list of String names. 
3     */
4    package arraylistplay;
5    
6    import java.util.ArrayList;
7    import java.util.List;
8    import java.util.stream.Collectors;
9    
10   public class StreamArrayListProcessing {
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           //Point B: Create and ArrayList of only first names
20           List<String> firstNamesList = names.stream()
21                   .map(n -> firstName(n))
22                   .collect(Collectors.toList());
23   
24           List<String> lastNamesList = names.stream()
25                   .map(n -> lastName(n))
26                   .collect(Collectors.toList());
27   
28           System.out.print("First names: ");
29           for (String firstName: firstNamesList) {
30               System.out.print(firstName + " ");
31           }
32           System.out.println();
33           //Point C: Make a string of the first names
34           String firstNames = names.stream()
35                   .map(n -> firstName(n))
36                   .collect(Collectors.joining(", "));
37   
38           System.out.print("First names: " + firstNames);
39           //Point D
40           String lastNames = names.stream()
41                   .map(n -> lastName(n))
42                   .collect(Collectors.joining(", "));
43   
44           System.out.print("Last names: " + lastNames);
45           //Point E
46           String upperCaseFirstNames = names.stream()
47                   .map(n -> firstName(n))
48                   .map(n -> n.toUpperCase())
49                   .collect(Collectors.joining(", "));
50   
51           System.out.println("Uppercase first names: " + upperCaseFirstNames);
52           //Point F
53           String hyphenatedLastNames = names.stream()
54                   .map(n -> lastName(n))
55                   .filter(n -> n.contains("-"))
56                   .collect(Collectors.joining(", "));
57   
58           System.out.println("Hyphenated names: " + upperCaseFirstNames);
59           //Point G
60           int totalLength = names.stream()
61                   .map(n -> n.length())
62                   .reduce(0, (n1, n2) -> n1 + n2);
63   
64           System.out.println("Total length: " + totalLength);
65           //Point H
66           int firstNamesTotalLength = firstNamesList.stream()
67                   .map(n -> n.length())
68                   .reduce(0, (n1, n2) -> n1 + n2);
69   
70           System.out.println("First names total length: " + firstNamesTotalLength);
71           //Point I
72           int lastNamesTotalLength = lastNamesList.stream()
73                   .map(n -> n.length())
74                   .reduce(0, (n1, n2) -> n1 + n2);
75   
76           System.out.println("Last names total length: " + lastNamesTotalLength);
77       }
78   
79       private static String firstName(String directoryStyleName) {
80           int directoryStyleNameCommaPosition = directoryStyleName.indexOf(",");
81           return directoryStyleName.substring(directoryStyleNameCommaPosition + 2);
82       }
83   
84       private static String lastName(String directoryStyleName) {
85           int directoryStyleNameCommaPosition = directoryStyleName.indexOf(",");
86           return directoryStyleName.substring(0, directoryStyleNameCommaPosition);
87       }
88   
89   }
90