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("Sanchez, Louis");
23           names.add("Martinez, Valeria");
24           names.add("Sanchez-Junior, Louis");
25   
26           // POINT B: Use map and the toList collector to create an ArrayList of just the first names of the names ArrayList.
27           //          Use a for loop to print out the names. separated by spaces.
28           List<String> firstNamesList = names.stream()
29                   .map(n -> firstName(n))
30                   .collect(Collectors.toList());
31   
32           System.out.print("First names: ");
33           for (String firstName : firstNamesList) {
34               System.out.print(firstName + " ");
35           }
36           System.out.println();
37   
38           // POINT C: Use map and the joining collector to create a String of just the first names of the names ArrayList
39           //          with each names separated by a comma. Print it.
40           String firstNames = names.stream()
41                   .map(n -> firstName(n))
42                   .collect(Collectors.joining(", "));
43   
44           System.out.println("First names: " + firstNames);
45   
46           // POINT D: By analogy from point C, print a comma-separated list of the last names in the names ArrayList.
47           String lastNames = names.stream()
48                   .map(n -> lastName(n))
49                   .collect(Collectors.joining(", "));
50   
51           System.out.println("Last names: " + lastNames);
52   
53           // POINT E: Print a comma-separated list of all uppercase first names 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   
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           String hyphenatedLastNames = names.stream()
63                   .map(n -> lastName(n))
64                   .filter(n -> n.contains("-"))
65                   .collect(Collectors.joining(", "));
66   
67           System.out.println("Hyphenated last names: " + hyphenatedLastNames);
68   
69           // POINT G: Print the integer value of the total length of all names 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 product of the length of all first names in the names ArrayList.
77           int firstLength = names.stream()
78                   .map(n -> firstName(n).length())
79                   .reduce(0, (n1, n2) -> n1 + n2);
80   
81           System.out.println("First name total length: " + firstLength);
82   
83           // POINT I: Print the integer value of the product of the length of all first names in the names ArrayList.
84           int productLength = names.stream()
85                   .map(n -> n.length())
86                   .reduce(1, (n1,n2) -> n1 * n2);
87   
88           System.out.println("Product of the lengths of the names: " + productLength);
89   
90       }
91   
92       private static String firstName(String directoryStyleName) {
93           int firstNamePosition = directoryStyleName.indexOf(",");
94           String firstName = directoryStyleName.substring(firstNamePosition + 2, directoryStyleName.length());
95           return firstName;
96       }
97   
98       private static String lastName(String directoryStyleName) {
99           int lastNamePosition = directoryStyleName.indexOf(",");
100          String lastName = directoryStyleName.substring(0, lastNamePosition);
101          return lastName;
102      }
103  }