ArrayListProcessing.java
1    /* 
2    * A program to perform some  basic operations on a list of String names 
3     */
4    package arraylist;
5    
6    import java.util.ArrayList;
7    import java.util.List;
8    
9    public class ArrayListProcessing {
10   
11       public static void main(String[] args) {
12           //POINT A: Add some strings which represent names to an ArrayList.
13           List<String> names = new ArrayList<>();
14           names.add("Holiday, Billie");
15           names.add("Claudel, Camille");
16           names.add("Picasso, Pablo");
17           names.add("Gallen-Kallela, Akseli");
18           names.add("Zotto, Osvaldo");
19           names.add("Fisher, Raymond");
20           names.add("Brown-Johnson, Bob");
21   
22           //POINT B: Create an ArrayList ofjust the first names of the names ArrayList.
23           // Use a for loop to print out the names, separated by spaces
24           List<String> firstNamesList = firstNames(names);
25   
26           System.out.print("First names: ");
27           for (String firstName : firstNamesList) {
28               System.out.print(firstName + " ");
29           }
30           System.out.println();
31   
32   
33           //POINT C: Use String's join function to create and print a String of just
34           // the first names of the names ArrayList with each name separated by a comma
35   
36           String firstNames = String.join(", ", firstNamesList);
37           System.out.println("First names: " + firstNames);
38   
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   
43           List<String> LastNamesList = LastNames(names);
44   
45           String lastNames = String.join(", ", LastNamesList);
46           System.out.println("Last names: " + lastNames);
47   
48   
49           //POINT E: Print a comma-separated list of all uppercase first names
50           //from the list of names in the names ArrayList
51   
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
58           // from the list of names in the names ArrayList
59   
60           List<String> hyphenatedNamesList = hyphenatedNames(LastNames(names));
61   
62           String hyphenatedNames = String.join(", ", hyphenatedNamesList);
63           System.out.println("Hyphenated names: " + hyphenatedNames);
64   
65           //POINT G: Print the integer values of the total length of all names in the names ArrayList
66   
67           int totalLength = totalNameLength(names);
68   
69           System.out.println("Total length: " + totalLength);
70   
71   
72           //POINT H: Print the integer value of the total length of all
73           //first names in the names ArrayList
74   
75           int fNameLength = totalNameLength(firstNames(names));
76   
77           System.out.println("Total First Name Length: " + fNameLength);
78   
79   
80           //POINT I: Print the integer value of the product of the length of
81           //all first names in the name ArrayList
82   
83           int productLengths = productLength(firstNames(names));
84   
85           System.out.println("Product of first names length: " + productLengths);
86   
87       }
88   
89       private static String firstName(String directoryStyleName) {
90           int positionOfComma = directoryStyleName.indexOf(",");
91           String firstName = directoryStyleName.substring(positionOfComma + 2);     ///Without the +2 the comma shows in the output
92           return firstName;
93       }
94   
95   
96       private static List<String> firstNames(List<String> names) {
97           List<String> firsts = new ArrayList<>();
98           for (String name : names) {
99               firsts.add(firstName(name));
100          }
101          return firsts;
102      }
103  
104      private static String lastName(String directoryStyleName) {
105          int positionOfComma = directoryStyleName.indexOf(",");
106          String lastName = directoryStyleName.substring(0, positionOfComma);
107          return lastName;
108      }
109  
110      private static List<String> LastNames(List<String> names) {
111          List<String> lasts = new ArrayList<>();
112          for (String name : names) {
113              lasts.add(lastName(name));
114          }
115          return lasts;
116      }
117  
118      public static List<String> upperCaseNames(List<String> names) {
119          List<String> uppercases = new ArrayList<>();
120          for (String name : names) {
121              uppercases.add(name.toUpperCase());
122          }
123          return uppercases;
124      }
125  
126      public static List<String> hyphenatedNames(List<String> names) {
127          List<String> hyphenateds = new ArrayList<>();
128          for (String name : names) {
129              if (name.contains("-")) {
130                  hyphenateds.add(name);
131              }
132          }
133          return hyphenateds;
134      }
135  
136      public static int totalNameLength(List<String> names){
137          int totalLength = 0;
138          for (String name: names){
139              totalLength = totalLength + name.length();
140          }
141          return totalLength;
142      }
143      public static int productLength(List<String> names){
144          int productLength = 1;
145          for (String name: names){
146              productLength = productLength * name.length();
147          }
148          return productLength;
149      }
150  
151  }
152  
153  
154  
155