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("Sumner, Elijah");
23           names.add("DePretis, Amy");
24   
25           //POINT B: Use map and the toList collector to create an ArrayList of just
26           //         the first names of the names ArrayList. Use a for loop to print
27           //         out the names, separated by spaces.
28   
29           List<String> firstNamesList = names.stream()
30                   .map(n -> firstName(n))
31                   .collect(Collectors.toList());
32   
33           System.out.print("First names: ");
34           for (String firstName : firstNamesList){
35               System.out.print(firstName + " ");
36           }
37           System.out.println();
38   
39           //POINT C: Use a 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 comma. Print it.
42   
43           String firstNames = names.stream()
44                   .map(n -> firstName(n))
45                   .collect(Collectors.joining(", "));
46   
47           System.out.println("First names: " + firstNames);
48   
49           //POINT D: By analogy from point C, print a comma-separated list of the
50           //         last names in the names ArrayList.
51   
52           String lastNames = names.stream()
53                   .map(n -> lastName(n))
54                   .collect(Collectors.joining(", "));
55           System.out.println("Last names: " + lastNames);
56   
57           //POINT E: Print a comma-separated list of all uppercase first names
58           //         from the list of names in the names ArrayList.
59   
60           String upperCaseFirstNames = names.stream()
61                   .map(n -> firstName(n))
62                   .map(n -> n.toUpperCase())
63                   .collect(Collectors.joining(", "));
64   
65           System.out.println("Uppercase first names: " + upperCaseFirstNames);
66   
67           //POINT F: Print a comma-separated list of all hyphenated last names
68           //         from the list of names in the names ArrayList.
69   
70           String hyphenatedLastNames = names.stream()
71                   .map(n -> lastName(n))
72                   .filter(n -> n.contains("-"))
73                   .collect(Collectors.joining(", "));
74   
75           System.out.println("Hyphenated last names: " + hyphenatedLastNames);
76   
77           //POINT G: Print the integer value of the total length of all names
78           //         in the names ArrayList.
79   
80           int totalLength = names.stream()
81                   .map(n -> n.length())
82                   .reduce(0, (n1, n2) -> n1 + n2);
83   
84           System.out.println("Total length: " + totalLength);
85   
86           //POINT H: Print the integer value of the total length of all
87           //         first names in the names ArrayList.
88   
89           int firstNameLength = names.stream()
90                   .map(n -> firstName(n))
91                   .map(n -> n.length())
92                   .reduce(0, (n1, n2) -> n1 + n2);
93   
94           System.out.println("First name length: " + firstNameLength);
95   
96           //POINT I: Print the integer value of the product of the length of
97           //         all first names in the names ArrayList.
98   
99           int lastNameLength = names.stream()
100                  .map(n -> lastName(n))
101                  .map(n -> n.length())
102                  .reduce(0, (n1, n2) -> n1 + n2);
103  
104          System.out.println("Last name length: " + lastNameLength);
105      }
106  
107      private static String firstName(String directoryStyleName) {
108          int commaPosition = directoryStyleName.indexOf(",");
109          String firstName = directoryStyleName.substring(commaPosition+2);
110          return firstName;
111      }
112  
113      private static String lastName(String directoryStyleName) {
114          int commaPosition = directoryStyleName.indexOf(",");
115          String lastName = directoryStyleName.substring(0,commaPosition);
116          return lastName;
117      }
118  }
119