StreamArrayListProcessing.java
1    /* 
2    * A program to perform some basic operations on a list of Strings using Java streams. 
3     */
4    package arraylists;
5    
6    import java.util.ArrayList;
7    import java.util.List;
8    import java.util.stream.Collectors;
9    
10   public class StreamArrayListProcessing {
11   
12       public static void main(String[] args){
13           //POINT A: Add some strings which represent names to an ArrayList.
14           List<String> names = new ArrayList<>();
15           names.add("Holiday, Billie");
16           names.add("Claudel, Camille");
17           names.add("Picasso, Pablo");
18           names.add("Gallen-Kallela, Akseli");
19           names.add("Zotto, Osvaldo");
20           names.add("Camacho III, Franklin");
21   
22           //POINT B: Use map and the toList collector to create an Array List of just
23           //         the first names of the names ArrayList. Use a for loop to print
24           //         out the names, separated by spaces.
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.print(firstName + " ");
33           }
34           System.out.println();
35           //POINT C: Use map and the joining collector to create a String of just the first names
36           //         of the names ArrayList with each name separated by a comma. Print it.
37   
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 from point C, print a comma-separated 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   
52           //POINT E: Print a comma-separated list of all uppercase first names from the list of names in the names ArrayList.
53   
54           String upperCaseFirstNames = names.stream()
55                   .map(n -> firstName(n))
56                   .map(n -> n.toUpperCase())
57                   .collect(Collectors.joining(", "));
58   
59           System.out.println("Uppercase first names: " + upperCaseFirstNames);
60   
61           //POINT F: Print a comma-separated list of all hyphenated last names from the list of names in the names ArrayList.
62   
63           String hyphenatedLastNames = names.stream()
64                   .map(n -> lastName(n))
65                   .filter(n -> n.contains("-"))
66                   .collect(Collectors.joining(", "));
67   
68           System.out.println("Hyphenated last names: " + hyphenatedLastNames);
69   
70           //POINT G: Print the integer value of the total length of all names in the names ArrayList.
71   
72           int totalLength = names.stream()
73                   .map(n -> n.length())
74                   .reduce(0, (n1, n2) -> n1 + n2);
75   
76           System.out.println("Total length: " + totalLength);
77   
78           //POINT H: Print the integer value of the total length of all first names in the names ArrayList.
79   
80           int totalLengthOfFirstNames = names.stream()
81                   .map(n -> firstName(n))
82                   .map(n -> n.length())
83                   .reduce(0, (n1, n2) -> n1 + n2);
84   
85           System.out.println("Total length of first names: " + totalLengthOfFirstNames);
86   
87           //POINT I: Print the integer value of the product of the length of all first names in the names ArrayList.
88   
89           int totalProductOfFirstNames = names.stream()
90                   .map(n -> firstName(n))
91                   .map(n -> n.length())
92                   .reduce(1, (n1, n2) -> n1 * n2);
93   
94                     System.out.println("Total product of first names: " + totalProductOfFirstNames);
95                  }
96   
97           private static String firstName(String directoryStyleName) {
98               int dsnCommaPosition = directoryStyleName.indexOf(",");
99               String firstName = directoryStyleName.substring(dsnCommaPosition + 2);
100              return firstName;
101          }
102      private static List<String> firstNames(List<String> names) {
103          List<String> firsts = new ArrayList<>();
104          for (String name :names) {
105              firsts.add(firstName(name));
106          }
107          return firsts;
108      }
109      private static String lastName(String directoryStyleName) {
110  
111          int dsnCommaPosition = directoryStyleName.indexOf(",");
112          String lastName = directoryStyleName.substring(0, dsnCommaPosition);
113          return lastName;
114      }
115      private static List<String> lastNames(List<String> names) {
116          List<String> lasts = new ArrayList<>();
117          for (String name :names) {
118              lasts.add(lastName(name));
119          }
120          return lasts;
121      }
122      public static List<String> upperCaseNames(List<String> names) {
123          List<String> uppercases = new ArrayList<>();
124          for (String name : names) {
125              uppercases.add(name.toUpperCase());
126          }
127          return uppercases;
128      }
129      public static List<String> hyphenatedNames(List<String> names) {
130          List<String> hyphenateds = new ArrayList<>();
131          for (String name : names) {
132              if (name.contains("-")) {
133                  hyphenateds.add(name);
134              }
135          }
136          return hyphenateds;
137      }
138      public static int totalNameLength(List<String> names) {
139          int totalLength = 0;
140          for (String name : names) {
141              totalLength = totalLength + name.length();
142          }
143          return totalLength;
144      }
145      public static int totalNameProduct(List<String> names) {
146          int totalProduct = 1;
147          for (String name : names) {
148              totalProduct = totalProduct * name.length();
149          }
150          return totalProduct;
151      }
152  
153  }
154  
155