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