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