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   
23           names.add("Eugen, Prinz");
24           names.add("Hipper, Admiral");
25           names.add("HMS, Amazon");
26   
27           // POINT B: Use map and the toList collector to create an ArrayList of just the first names of the names ArrayList.
28           //          Use a for loop to print out the names. separated by spaces.
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 map and the joining collector to create a String of just the first names of the names ArrayList
40           //          with each names separated by a comma. Print it.
41           String firstNames = names.stream()
42                   .map(n -> firstName(n))
43                   .collect(Collectors.joining(", "));
44   
45           System.out.println("First names: " + firstNames);
46   
47           // POINT D: By analogy from point C, print a comma-separated list of the last names in the names ArrayList.
48           String lastNames = names.stream()
49                   .map(n -> lastName(n))
50                   .collect(Collectors.joining(", "));
51   
52           System.out.println("Last names: " + lastNames);
53   
54           // POINT E: Print a comma-separated list of all uppercase first names from the list of names in the names ArrayList.
55           String upperCaseFirstNames = names.stream()
56                   .map(n -> firstName(n))
57                   .map(n -> n.toUpperCase())
58                   .collect(Collectors.joining(", "));
59   
60           System.out.println("Uppercase first names: " + upperCaseFirstNames);
61   
62           // POINT F: Print a comma-separated list of all hyphenated last names from the list of names in the names ArrayList.
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           int totalLength = names.stream()
72                   .map(n -> n.length())
73                   .reduce(0, (n1, n2) -> n1 + n2);
74   
75           System.out.println("Total length: " + totalLength);
76   
77           // POINT H: Print the integer value of the product of the length of all first names in the names ArrayList.
78           int firstLength = names.stream()
79                   .map(n -> firstName(n).length())
80                   .reduce(0, (n1, n2) -> n1 + n2);
81   
82           System.out.println("First name total length: " + firstLength);
83   
84           // POINT I: Print the integer value of the product of the length of all first names in the names ArrayList.
85           int productLength = names.stream()
86                   .map(n -> n.length())
87                   .reduce(1, (n1,n2) -> n1 * n2);
88   
89           System.out.println("Product of the lengths of the names: " + productLength);
90   
91       }
92   
93       private static String firstName(String directoryStyleName) {
94           int firstNamePosition = directoryStyleName.indexOf(",");
95           String firstName = directoryStyleName.substring(firstNamePosition + 2, directoryStyleName.length());
96           return firstName;
97       }
98   
99       private static String lastName(String directoryStyleName) {
100          int lastNamePosition = directoryStyleName.indexOf(",");
101          String lastName = directoryStyleName.substring(0, lastNamePosition);
102          return lastName;
103      }
104  }