ArrayListProcessing.java
1    /* 
2     * A program to perforrm some basic operation on a list of String names. 
3     */
4    
5    package arraylists;
6    
7    import javax.print.DocFlavor;
8    import java.util.ArrayList;
9    import java.util.List;
10   
11   public class ArrayListProcessing {
12   
13       public static void main(String[] args) {
14           //POINT A: Add some string which represent names to an ArrayList.
15           List<String> names = new ArrayList<>();
16           names.add("Holiday, Billie");
17           names.add("Claudel, Camille");
18           names.add("Picasso, Pablo");
19           names.add("Galeen-Kallella, Akseli");
20           names.add("Zotto, Osvaldo");
21           names.add("Curry, Steph");
22           names.add("Carney, Emily");
23           names.add("Tiller, Chyenne");
24           names.add("Sackey-Mensah, Mariah");
25   
26           //POINT B: 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   
29           List<String> firstNamesList = firstNames(names);
30   
31           System.out.print("First names: ");
32           for (String firstName : firstNamesList) {
33               System.out.println(firstName + " ");
34           }
35           System.out.println();
36   
37           //POINT C: Use String's join function to create and print a String of
38           //          just the first name of the names ArrayList with each
39           //          name separated by a comma,
40   
41           String firstNames = String.join(", ", firstNamesList);
42           System.out.println("First names: " + firstNames);
43   
44           //POINT D: By analogy from points B and C, print a comma-separated
45           //          list of the last names in the names ArrayList
46   
47           List<String> lastNamesList = lastNames(names);
48           System.out.print("Last names: ");
49           for (String lastName : lastNamesList) {
50               System.out.println(lastName + " ");
51           }
52           System.out.println();
53           String lastNames = String.join(", ", lastNamesList);
54           System.out.println("Last names: " + lastNames);
55   
56           //POINT E: Print a comma-separated list of all uppercase first names
57           //          from the list of names in the names ArrayList.
58   
59           List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names));
60           String upperCaseFirstNames = String.join(", ", upperCaseFirstNamesList);
61           System.out.println("UpperCase First Names: " + upperCaseFirstNames);
62   
63           //POINT F: Print a comma-separated list of all hyphenated last names
64           //         from the list of names in the names ArrayList
65   
66           List<String> hyphenatedNamesList = hypenatedNames(names);
67           String hyphenatedNames = String.join(",", hyphenatedNamesList);
68           System.out.println("Hyphenated Names: " + hyphenatedNames);
69   
70           //POINT G: Print the int value of the total length of all names
71           //          in the names ArrayList.
72   
73           int totalLength = totalNameLength(names);
74           System.out.println("Total length: " + totalLength);
75   
76           //POINT H: Print the int value of the total length of all
77           //          first names in the names ArrayList
78   
79           int totalFirstNameLength = totalNameLength(firstNamesList);
80           System.out.println("Total First Names length: " + totalFirstNameLength);
81   
82           //POINT I: Print the int value of the product of the length of
83           //          all first names in the names ArrayList.
84   
85           int totalLengthProduct = nameProduct(names);
86           System.out.println("The product of the length of all names in the ArrayList: " + totalLengthProduct);
87       }
88   
89       private static List<String> upperCaseNames(List<String> names) {
90           List<String> uppercases = new ArrayList<>();
91           for (String name : names) {
92               uppercases.add(name.toUpperCase());
93           }
94           return uppercases;
95       }
96   
97       private static List<String> hypenatedNames(List<String> names) {
98           List<String> hyphenateds = new ArrayList<>();
99           for (String name : names) {
100              if (name.contains("-")) {
101                  hyphenateds.add(name);
102              }
103          }
104          return hyphenateds;
105      }
106  
107      private static int totalNameLength(List<String> names) {
108          int totalLength = 0;
109          for (String name : names) {
110              totalLength = totalLength + name.length();
111          }
112          return totalLength;
113      }
114  
115      private static int totalFirstNameLength(List<String> firstNames) {
116          int totalFirstNameLength = 0;
117          for (String firstName : firstNames) {
118              totalFirstNameLength = totalFirstNameLength + firstName.length();
119          }
120          return totalFirstNameLength;
121      }
122  
123      private static int nameProduct(List<String> names) {
124          int totalLength = 1;
125          for (String name : names) {
126              totalLength = totalLength * name.length();
127          }
128          return totalLength;
129      }
130  
131      private static String firstName(String directoryStyleName) {
132              int commaPosition = directoryStyleName.indexOf(",");
133              String firstName = directoryStyleName.substring(commaPosition + 2);
134              return firstName;
135          }
136  
137          private static List<String> firstNames(List<String> names) {
138              List<String> firsts = new ArrayList<>();
139              for (String name : names) {
140                  firsts.add(firstName(name));
141              }
142              return firsts;
143          }
144  
145      private static String lastName(String directoryStyleName) {
146          int commaPosition = directoryStyleName.indexOf(",");
147          String lastName = directoryStyleName.substring(0, commaPosition);
148          return lastName;
149      }
150  
151      private static List<String> lastNames(List<String> names) {
152          List<String> lasts = new ArrayList<>();
153          for (String name : names) {
154              lasts.add(lastName(name));
155          }
156          return lasts;
157      }
158  
159      }
160  
161  
162  
163  
164