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("Awesome, Rinaldo");
21           names.add("Realle, Johannis");
22   
23           //POINT B: Create an ArrayList of just the first names of the names
24           //         ArrayList.  Use a for loop to print out the names, separated
25           //         by spaces
26           List<String> firstNamesList = firstNames(names);
27           System.out.print("First names: ");
28           for (String firstName : firstNamesList) {
29               System.out.print(firstName + " ");
30           }
31   
32           //POINT C: Use String's join function to create and print a String of
33           //          just the first names of the names ArrayList with each
34           //          name separated by a comma.
35           String firstNames = String.join(", ", firstNamesList);
36           System.out.println("\nFirst names: " + firstNames);
37   
38           //POINT D: By analogy from point B and C, print a comma-separated
39           //          list of the last names in the names ArrayList.
40           List<String> lastNamesList = lastNames(names);
41           String lastNames = String.join(", ", lastNamesList);
42           System.out.println("Last names: " + lastNames);
43   
44           //POINT E: Print a comma-separated list of all uppercase first names
45           //          from the list of names in the names ArrayList.
46           List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names));
47           String upperCaseFirstNames = String.join(", ", upperCaseFirstNamesList);
48           System.out.println("Uppercase first names: " + upperCaseFirstNames);
49   
50           //POINT F: Print a comma-separated list of all hyphenated last names
51           //          from the list of names in the names ArrayList.
52           List<String> hyphenatedNamesList = hyphenatedNames(names);
53           String hyphenatedNames = String.join(",", hyphenatedNamesList);
54           System.out.println("Hyphenated name: " + hyphenatedNames);
55   
56           //POINT G: Print the integer value of the total length of all names
57           //          in the name ArrayList.
58           int totalLength = totalNameLength(names);
59           System.out.println("Total length of names ArrayList, including commas and spaces: " + totalLength);
60   
61           //POINT H: Print the integer value of the product of the length of all
62           //          first names in the names ArrayList.
63           String nameNumberOne = names.get(0);
64           String nameNumberTwo = names.get(1);
65           int lengthOfFirstTwo = nameNumberOne.length() + nameNumberTwo.length();
66           System.out.println("The length of the first two names, including commas and spaces: " + lengthOfFirstTwo);
67   
68           //POINT I: Print the integer value of the product of the length of
69           //          all first names in the names ArrayList.
70          System.out.println("The product of the first names: " + productOfFirstNames(firstNamesList));
71       }
72   
73       private static String firstName(String directoryStyleName) {
74           int comma = directoryStyleName.indexOf(",");
75           String firstName = directoryStyleName.substring(comma + 2);
76           return firstName;
77       }
78   
79       private static List<String> firstNames(List<String> names) {
80           List<String> firsts = new ArrayList<>();
81           for (String name : names) {
82               firsts.add(firstName(name));
83           }
84           return firsts;
85       }
86   
87       private static String lastName(String directoryStyleName) {
88           int comma = directoryStyleName.indexOf(",");
89           String lastName = directoryStyleName.substring(0, comma);
90           return lastName;
91       }
92   
93       private static List<String> lastNames(List<String> names) {
94           List<String> lasts = new ArrayList<>();
95           for (String name : names) {
96               lasts.add(lastName(name));
97           }
98           return lasts;
99       }
100  
101      public static List<String> upperCaseNames(List<String> names){
102          List<String> uppercases = new ArrayList<>();
103          for (String name : names) {
104              uppercases.add(name.toUpperCase());
105          }
106          return uppercases;
107      }
108  
109      public static List<String> hyphenatedNames(List<String> names){
110          List<String> hyphenateds = new ArrayList<>();
111          for (String name : names){
112              if (name.contains("-")) {
113                  hyphenateds.add(name);
114              }
115          }
116          return hyphenateds;
117      }
118  
119      public static int totalNameLength(List<String> names){
120          int totalLength = 0;
121          for (String name : names){
122              totalLength = totalLength + name.length();
123          }
124          return totalLength;
125      }
126      public static int productOfFirstNames(List<String> firstNamesList){
127          int productOfFirstNames = 1;
128          for (String firstName : firstNamesList){
129              productOfFirstNames = (productOfFirstNames) * (firstName.length());
130          }
131          return productOfFirstNames;
132      }
133  }
134  
135