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("Sumner, Elijah");
21           names.add("DePretis, Amy");
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   
28           System.out.print("First names: ");
29           for (String firstName : firstNamesList){
30               System.out.print(firstName + " ");
31           }
32           System.out.println();
33           //POINT C: Use String's join function to create and print a String of
34           //         just the first names of the names ArrayList with each
35           //         name separated by a comma.
36   
37           String firstNames = String.join(", ",firstNamesList);
38           System.out.println("First names: " + firstNames);
39   
40           //POINT D: By analogy from points B and C, print a comma-separated
41           //         list of the last names in the names ArrayList.
42           List<String> lastNamesList = lastNames(names);
43   
44           String lastNames = String.join(", ",lastNamesList);
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           List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names));
51           String upperCaseFirstNames = String.join(", ", upperCaseFirstNamesList);
52   
53           System.out.println("Uppercase first names: " + upperCaseFirstNames);
54   
55           //POINT F: Print a comma-separated list of all hyphenated last names
56           //         from the list of names in the names ArrayList.
57   
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
64           //         in the names ArrayList.
65   
66           int totalLength = totalNameLength(names);
67   
68           System.out.println("Total length: " + totalLength);
69   
70           //POINT H: Print the integer value of the total length of all
71           //         first names in the names ArrayList.
72   
73           int totalFirstNameLength = totalNameLength(firstNames(names));
74   
75           System.out.println("Total first name length: " + totalFirstNameLength);
76   
77           //POINT I: Print the integer value of the product of the length of
78           //         all first names in the names ArrayList.
79   
80           int totalLastNameLength = totalNameLength(lastNames(names));
81   
82           System.out.println("Total last name length: " + totalLastNameLength);
83       }
84   
85       private static String firstName(String directoryStyleName) {
86           int commaPosition = directoryStyleName.indexOf(",");
87           String firstName = directoryStyleName.substring(commaPosition+2);
88           return firstName;
89       }
90   
91       private static List<String> firstNames(List<String> names) {
92          List<String> firsts = new ArrayList<>();
93          for (String name : names){
94              firsts.add(firstName(name));
95          }
96          return firsts;
97       }
98   
99       private static String lastName(String directoryStyleName) {
100          int commaPosition = directoryStyleName.indexOf(",");
101          String lastName = directoryStyleName.substring(0,commaPosition);
102          return lastName;
103      }
104  
105      private static List<String> lastNames(List<String> names) {
106          List<String> lasts = new ArrayList<>();
107          for (String name : names){
108              lasts.add(lastName(name));
109          }
110          return lasts;
111      }
112  
113      public static List<String> upperCaseNames(List<String> names){
114          List<String> uppercases = new ArrayList<>();
115          for (String name : names) {
116              uppercases.add(name.toUpperCase());
117          }
118          return uppercases;
119      }
120  
121      public static List<String> hyphenatedNames(List<String> names) {
122          List<String> hyphenateds = new ArrayList<>();
123          for (String name : names){
124              if (name.contains("-")) {
125                  hyphenateds.add(name);
126              }
127          }
128          return hyphenateds;
129      }
130  
131      public static int totalNameLength(List<String> names){
132          int totalLength = 0;
133          for(String name : names){
134              totalLength = totalLength + name.length();
135          }
136          return totalLength;
137      }
138  }
139