StreamArrayListProcessing.java
1    package arraylistplay;
2    
3    import java.util.ArrayList;
4    import java.util.List;
5    import java.util.stream.Collectors;
6    
7    public class StreamArrayListProcessing {
8        public static void main(String[] args) {
9            // POINT A: Add some strings which represent names to an ArrayList.
10           List<String> names = new ArrayList<>();
11           names.add("Holiday, Billie");
12           names.add("Claudel, Camille");
13           names.add("Picasso, Pablo");
14           names.add("Gallen-Kallela, Akseli");
15           names.add("Zotto, Osvaldo");
16           names.add("MinSoe, Myat");
17           names.add("Kyaw, Aung-Khant");
18           names.add("Aung, KoKo");
19           // POINT B: Use map and the toList collector to create an ArrayList of just
20           //          the first names of the names ArrayList. Use a for loop to print
21           //          out the names, separated by spaces.
22           List<String> firstNamesList = names.stream().
23                   map(n -> firstName(n)).
24                   collect(Collectors.toList());
25   
26           System.out.print("First names: ");
27           for (String firstName : firstNamesList){System.out.print(firstName + " ");
28           }System.out.println();
29           // POINT C: Use map and the joining collector to create a String of just
30           //          the first names of the names ArrayList with each name separated
31           //          by a comma. Print it.
32           String firstNames = names.stream()
33                   .map(n -> firstName(n))
34                   .collect(Collectors.joining(", "));
35           System.out.println("First names: " + firstNames);
36           // POINT D: By analogy from point C, print a comma-separated list of the
37           //          last names in the names ArrayList.
38           String lastNames = names.stream()
39                   .map(n -> lastName(n))
40                   .collect(Collectors.joining(", "));
41           System.out.println("Last names: " + lastNames);
42           // POINT E: Print a comma-separated list of all uppercase first names
43           //         from the list of names in the names ArrayList.
44           String upperCaseFirstNames = names.stream()
45                   .map(n -> firstName(n))
46                   .map(n -> n.toUpperCase())
47                   .collect(Collectors.joining(", "));
48           System.out.println("Uppercase first names: " + upperCaseFirstNames);
49           // POINT F: Print a comma-separated list of all hyphenated last names
50           //          from the list of names in the names ArrayList.
51           String hyphenatedLastNames = names.stream()
52                   .map(n -> lastName(n))
53                   .filter(n -> n.contains("-"))
54                   .collect(Collectors.joining(", "));
55           System.out.println(hyphenatedLastNames);
56           // POINT G: Print the integer value of the total length of all names
57           //          in the names ArrayList.
58           int totalLength = names.stream()
59                   .map(n -> n.length())
60                   .reduce(0, (n1, n2) -> n1 + n2);
61   
62           System.out.println("Total length: " + totalLength);
63           // POINT H: Print the integer value of the total length of all
64           //          first names in the names ArrayList.
65           int totalFirstNameLength = firstNamesList.stream()
66                   .map(n -> n.length())
67                   .reduce(0,(n1,n2)->n1+n2);
68   
69           System.out.println("Total First Name Length: " +totalFirstNameLength);
70           // POINT I: Print the integer value of the product of the length of
71           //          all first names in the names ArrayList.
72           int totalProductNameLength = names.stream()
73                   .map(n -> n.length())
74                   .reduce(1,(n1,n2)->n1*n2);
75           System.out.println("Total Product of Names length: " +totalProductNameLength);
76       }
77       private static String firstName(String directoryStyleName) {
78           int commaPosition = directoryStyleName.indexOf(",");
79           String firstName = directoryStyleName.substring(2+commaPosition);
80           return firstName;
81       }
82       private static String lastName(String directoryStyleName){
83           int commaPosition = directoryStyleName.indexOf(",");
84           String lastName = directoryStyleName.substring(0,commaPosition);
85           return lastName;
86       }
87   }
88