SteamArrayListProcessing.java
1    /* 
2     * A program to perform some  basic operations on a list of String names 
3     */
4    package arraylist;
5    
6    import java.util.ArrayList;
7    import java.util.List;
8    import java.util.stream.Collectors;
9    
10   public class SteamArrayListProcessing {
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("Fisher, Raymond");
21           names.add("Brown-Johnson, Bob");
22   
23           //POINT B: Use map and the toList collector to create an ArrayList of just
24           //         the first names of the names ArrayList. Use a for loop to print
25           //         out the names, separated by spaces.
26           List<String> firstNamesList = names.stream()
27                   .map(n -> firstName(n))
28                   .collect(Collectors.toList());
29           System.out.print("First names: ");
30           for (String firstName : firstNamesList) {
31               System.out.print(firstName + " ");
32           }
33           System.out.println();
34   
35           //POINT C: Use map and the joining collector to create a String of just
36           //        the first names of the names ArrayList with each name separated
37           //        by a comma. Print it.
38   
39           String firstNames = names.stream()
40                   .map(n -> firstName(n))
41                   .collect(Collectors.joining(", "));
42   
43           System.out.println("First names: " + firstNames);
44   
45   
46           //POINT D: By analogy from points B and C, print a comma-separated
47           //list of the last names in the names ArrayList
48   
49           String lastNames = names.stream()
50                   .map(n -> lastName(n))
51                   .collect(Collectors.joining(", "));
52   
53           System.out.println("Last names: " + lastNames);
54   
55           //POINT E: Print a comma-separated list of all uppercase first names
56           //from the list of names in the names ArrayList
57           String upperCaseFirstNames = names.stream()
58                   .map(n -> firstName(n))
59                   .map(n -> n.toUpperCase())
60                   .collect(Collectors.joining(", "));
61           System.out.println("Uppercase first names: " + upperCaseFirstNames);
62   
63   
64           //POINT F: Print a comma-separated list of all hyphenated last names
65           // from the list of names in the names ArrayList
66           String hyphenatedLastNames = names.stream()
67                   .map(n -> lastName(n))
68                   .filter(n -> n.contains("-"))
69                   .collect(Collectors.joining(", "));
70           System.out.println("Hyphenated last names: " + hyphenatedLastNames);
71   
72   
73           //POINT G: Print the integer values of the total length of all names in the names ArrayList
74           int totalLength = names.stream()
75                   .map(n -> n.length())
76                   .reduce(0, (n1, n2) -> n1 + n2);
77           System.out.println("Total Length: " + totalLength);
78   
79   
80           //POINT H: Print the integer value of the total length of all
81           //first names in the names ArrayList
82           int fNameTotalLength = names.stream()
83                   .map(n -> firstName(n))
84                   .map(n -> n.length())
85                   .reduce(0, (n1, n2) -> n1 + n2);
86           System.out.println("Total First Name Length: " + fNameTotalLength);
87   
88           //POINT I: Print the integer value of the product of the length of
89           //all first names in the name ArrayList
90           int productLength = names.stream()
91                   .map(n -> firstName(n))
92                   .map(n -> n.length())
93                   .reduce(1, (n1, n2) -> n1 * n2);
94           System.out.println("Product of first names length: " + productLength);
95   
96   
97       }
98   
99       private static String firstName(String directoryStyleName) {
100          int positionOfComma = directoryStyleName.indexOf(",");
101          String firstName = directoryStyleName.substring(positionOfComma + 2);     ///Without the +2 the comma shows in the output
102          return firstName;
103      }
104  
105  
106  
107      private static String lastName(String directoryStyleName) {
108          int positionOfComma = directoryStyleName.indexOf(",");
109          String lastName = directoryStyleName.substring(0, positionOfComma);
110          return lastName;
111      }
112  
113  
114  
115  
116  }
117  
118