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 string 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("Galeen-Kallella, Akseli");
21           names.add("Zotto, Osvaldo");
22           names.add("Curry, Steph");
23           names.add("Carney, Emily");
24           names.add("TIller, Chyenne");
25           names.add("Sackey-Mensah, Mariah");
26   
27           //POINT B: Use map and the toList collector to create an ArrayList of just
28           //          the first names of the names ArrayList. Use a for loop to print
29           //          out the names, separated by spaces.
30   
31           List<String> firstNamesList = names.stream()
32                   .map(n -> firstName(n))
33                   .collect(Collectors.toList());
34           System.out.println("First Name: ");
35           for (String firstName : firstNamesList) {
36               System.out.print(firstName + " "); }
37           System.out.println();
38   
39           //POINT C: Use map and the joining collector to create a String of just
40           //          the first names of the names ArrayList with each name separated
41           //          by a coma. Print it.
42   
43           String firstNames = names.stream()
44                   .map(n -> firstName(n))
45                   .collect(Collectors.joining(", "));
46           System.out.println("First Names: " + firstNames);
47   
48           //POINT D: By analogy from point C, print a comma-separated list of the
49           //         last names in the names ArrayList.
50   
51         String lastNames = names.stream()
52                 .map(n->lastName(n))
53                 .collect(Collectors.joining(", "));
54          System.out.println("Last Names: " + lastNames);
55   
56           //POINT E: Print a comma-separated list of all uppercase first names
57           //         from the list of names in the names ArrayList.
58   
59           String upperCaseFirstNames = names.stream()
60                   .map(n -> firstName(n))
61                   .map(n -> n.toUpperCase())
62                   .collect(Collectors.joining(", "));
63           System.out.println("UpperCase First Names: " + upperCaseFirstNames);
64   
65           //POINT F: Print a comma-separated list of all hyphenated last names
66           //         from the list of names in the names ArrayList.
67   
68          String hyphenated = names.stream()
69                  .map(n -> lastName(n))
70                  .filter(n->n.contains("-"))
71                  .collect(Collectors.joining(", "));
72           System.out.println("All Hyphenated Last Names: " + hyphenated);
73   
74           //POINT G: Print the integer value of the total length
75           //         of all names in the names ArrayList.
76   
77           int totalLength = names.stream()
78                   .map(n -> n.length())
79                   .reduce(0, (n1, n2) -> n1 + n2);
80           System.out.println("Total Length:" + totalLength);
81   
82           //POINT H: Print the integer value of the total length of all
83           //         first names in the names ArrayList.
84   
85           int firstNameLength = names.stream()
86                   .map(n -> firstName(n))
87                   .map(n -> n.length())
88                   .reduce(0, (n1, n2) -> n1 + n2);
89           System.out.println("The Length Of The First Name:" + firstNameLength);
90   
91           //POINT I: Print the integer value of the product of the length of
92           //         all first names in the names ArrayList.
93   
94           int productLength = names.stream()
95                   .map(n -> firstName(n))
96                   .map(n -> n.length())
97                   .reduce(1, (n1, n2) -> n1 * n2);
98           System.out.println("The Product Of The Length Of All First Names: " + productLength);
99       }
100  
101  
102      private static String firstName(String directoryStyleName) {
103          return (directoryStyleName.substring(directoryStyleName.lastIndexOf((" "))));
104      }
105  
106      private static String lastName(String directoryStyleName) {
107          return (directoryStyleName.substring(0,directoryStyleName.lastIndexOf((","))));
108      }
109  
110      private static List<String> hyphenatedNames(List<String> names) {
111          List<String> hyphenateds = new ArrayList<>();
112          for (String name : names) {
113              if (name.contains("-")) {
114                  hyphenateds.add(name);
115              }
116          }
117          return hyphenateds;
118      }
119  
120  }
121  
122