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