ArrayListProcessing.java
1    /* 
2     * A program to perform some basic operations on a list of String names. 
3     */
4    
5    package arraylists;
6    
7    import java.util.ArrayList;
8    import java.util.List;
9    
10   public class ArrayListProcessing {
11   
12       public static void main(String[] args){
13           //Point A: Add some strings which represent names to an ArrayList.
14           List<String> names = new ArrayList<>();
15           names.add("Holiday, Billie");
16           names.add("Claudel, Camille");
17           names.add("Picasso, Pablo");
18           names.add("Gallen-Kallela, Akseli");
19           names.add("Zotto, Osvaldo");
20           names.add("Lennon, John");
21           names.add("Zevon, Warren");
22           names.add("Bay, James");
23   
24           // POINT B: Create an ArrayList of just the first names of the names ArrayList. Use a for loop to print out the names, separated by spaces.
25           List<String> firstNamesList = firstNames(names);
26   
27           System.out.print("First names: ");
28           for (String firstName : firstNamesList){
29               System.out.print(firstName + " ");
30           }
31           System.out.println();
32   
33           // POINT C: Use String's join function to create and print a String of just the first names of the names ArrayList with each name separated by a comma.
34           String firstNames = String.join(", ", firstNamesList);
35   
36           System.out.println("First names: " + firstNames);
37   
38           // POINT D: By analogy from points B and C, print a comma-separated list of the last names in the names ArrayList.
39           List<String> lastNamesList = lastNames(names);
40   
41           System.out.print("Last names: ");
42           for (String lastName : lastNamesList){
43               System.out.print(lastName + " ");
44           }
45           System.out.println();
46   
47           String lastNames = String.join(", ", lastNamesList);
48   
49           System.out.println("Last names: " + lastNames);
50   
51           // POINT E: Print a comma-separated list of all uppercase first names from the list of names in the names ArrayList.
52           List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names));
53           String upperCaseFirstNames = String.join(", ", upperCaseFirstNamesList);
54   
55           System.out.println("Uppercase first names: " + upperCaseFirstNames);
56   
57           // POINT F: Print a comma-separated list of all hyphenated last names from the list of names in the names ArrayList.
58           List<String> hyphenatedLastNamesList = hyphenatedNames(lastNames(names));
59           String hyphenatedLastNames = String.join(", ", hyphenatedLastNamesList);
60   
61           System.out.println("Hyphenated last names: " + hyphenatedLastNames);
62   
63           // POINT G: Print the integer value of the total length of all names in the names ArrayList.
64           int totalLength = totalNameLength(names);
65   
66           System.out.println("Total length: " + totalLength);
67   
68           // POINT H: Print the integer value of the total length of all first names in the names ArrayList.
69           List<String> firstName = firstNames(names);
70           int totalFirstLength = 0;
71           for (int i = 0; i < firstName.size(); i = i + 1){
72               totalFirstLength = totalFirstLength + firstName.get(i).length();
73           }
74           System.out.println("Total first length: " + totalFirstLength);
75   
76           // POINT I: Print the integer value of the product of the length of all first names in the names ArrayList.
77           int totalLengthProduct = totalLengthProduct(names);
78   
79           System.out.println("Total length product: " + totalLengthProduct);
80       }
81   
82       // POINT B
83       private static String firstName(String directoryStyleName) {
84           int i = directoryStyleName.indexOf(",") + 2;
85           return directoryStyleName.substring(i);
86       }
87   
88       private static List<String> firstNames(List<String> names){
89           List<String> firsts = new ArrayList<>();
90           for (String name : names){
91               firsts.add(firstName(name));
92           }
93           return firsts;
94       }
95       // POINT D
96       private static String lastName(String directoryStyleName) {
97           int i = directoryStyleName.indexOf(",");
98           return directoryStyleName.substring(0, i);
99       }
100  
101      private static List<String> lastNames(List<String> names){
102          List<String> lasts = new ArrayList<>();
103          for (String name : names){
104              lasts.add(lastName(name));
105          }
106          return lasts;
107      }
108      // POINT E
109      public static List<String> upperCaseNames(List<String> names){
110          List<String> uppercases = new ArrayList<>();
111          for (String name : names){
112              uppercases.add(name.toUpperCase());
113          }
114          return uppercases;
115      }
116      // POINT F
117      public static List<String> hyphenatedNames(List<String> names){
118          List<String> hyphenateds = new ArrayList<>();
119          for (String name : names){
120              if (name.contains("-")) {
121                  hyphenateds.add(name);
122              }
123          }
124          return hyphenateds;
125      }
126      // POINT G
127      public static int totalNameLength(List<String> names){
128          int totalLength = 0;
129          for (String name : names){
130              totalLength = totalLength + name.length();
131          }
132          return totalLength;
133      }
134      // POINT I
135      public static int totalLengthProduct(List<String> names){
136          int totalNameLengthProduct = 1;
137          for (String name : names){
138              int nameProduct = name.length();
139              totalNameLengthProduct = nameProduct * totalNameLengthProduct;
140          }
141          return totalNameLengthProduct;
142      }
143  
144  }