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