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, Skseli");
19           names.add("Zotto, Osvaldo");
20           names.add("Berk, Eric");
21           names.add("Hille, Eddie");
22           names.add("Shines, Scott");
23           names.add("Marino, Chris");
24   
25   
26           //Add code to which makes use of our firstNames function, and prints out the results prescribed by that comment
27   
28           //Point B: Create an ArrayList of just the first names of the names
29           // ArrayList. Use a for loop to print out the names, separated
30           // by spaces.
31           List<String> firstNamesList = firstNames(names);
32   
33           System.out.print("First names: ");
34           for (String firstName : firstNamesList) {
35               System.out.print(firstName + " ");
36           }
37           System.out.println();
38   
39           //Point C: Use String's join function to create and print a String of
40           // just the first names of the names ArrayList with each
41           // name separated by a coma
42   
43           String firstNames = String.join(", ", firstNamesList);
44           System.out.print("First names: " + firstNames);
45           System.out.println();
46   
47           //Point D: By analogy from points B and C, print a comma-separated
48           // list of the last names in the names ArrayList.
49   
50           List<String> lastNamesList = lastNames(names);
51   
52           System.out.print("Last names: ");
53           for (String lastName : lastNamesList) {
54               System.out.print(lastName + " ");
55           }
56           System.out.println();
57   
58           String lastNames = String.join(", ", lastNamesList);
59           System.out.print("Last names: " + lastNames);
60           System.out.println();
61   
62           //Point E: Print a coma-separated list of all uppercase first names
63           //from the list of names in the ArrayList
64           List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names));
65           String upperCaseFirstNames = String.join(", ", upperCaseFirstNamesList);
66   
67           System.out.println("Uppercase first names: " + upperCaseFirstNames);
68   
69           //Point F: Print a coma-separated list of all hyphenated last names
70           //from the list of names in the ArrayList
71           List<String> hyphenatedLastNamesList = hyphenatedNames(lastNames(names));
72           String hyphenatedLastNames = String.join(" - ", hyphenatedLastNamesList);
73   
74           System.out.println("hyphenated last names: " + hyphenatedLastNames);
75   
76           //Point G: Print the integer value of the total length of all names
77           // in the names ArrayList
78   
79           int totalLength = totalNameLength(names);
80           System.out.println("Total length: " + totalLength);
81   
82           //Point H: Print the integer value of the total length of all
83           // first names in the names ArrayList
84   
85           int totalFirstNamesLength = totalNameLength(firstNamesList);
86           System.out.println(" Total first name length: " + totalFirstNamesLength);
87   
88           //Point I: Print the integer value of the product of the length of
89           // all first names in the names ArrayList
90   
91           int productOfLengths = productOfNameLengths(names);
92           System.out.println(" Product of the lengths of each of the names: " + productOfLengths);
93   
94   
95       }
96   
97       private static String firstName (String dsn){
98           int commaPosition = dsn.indexOf(",");
99           return dsn.substring(2 + commaPosition);
100      }
101      private static List<String> firstNames (List <String> names) {
102          List<String> firsts = new ArrayList<>();
103          for (String name : names) {
104              firsts.add(firstName(name));
105          }
106          return firsts;
107      }
108  
109      private static String lastName (String dsn){
110          int commaPosition = dsn.indexOf(",");
111          return dsn.substring(2 + commaPosition);
112      }
113      private static List<String> lastNames (List <String> names) {
114          List<String> lasts = new ArrayList<>();
115          for (String name : names) {
116              lasts.add(lastName(name));
117          }
118          return lasts;
119      }
120  
121      public static List<String> upperCaseNames(List<String> names){
122          List<String> uppercases = new ArrayList<>();
123          for (String name : names){
124              uppercases.add(name.toUpperCase());
125          }
126          return uppercases;
127      }
128  
129      public static List<String> hyphenatedNames(List<String> names){
130          List<String> hyphenateds = new ArrayList<>();
131          for (String name : names){
132              if (name.contains(" - ")) {
133                  hyphenateds.add(name);
134              }
135          }
136          return hyphenateds;
137      }
138  
139      public static int totalNameLength(List<String> names){
140          int totalLength = 0;
141          for (String name : names){
142              totalLength = totalLength + name.length();
143          }
144          return totalLength;
145      }
146  
147      public static int productOfNameLengths(List<String> names) {
148          int productOfNameLengths = 0;
149          for (String name : names) {
150              productOfNameLengths = productOfNameLengths * name.length();
151          }
152          return productOfNameLengths;
153      }
154  
155  }
156  
157  
158