ArrayListProcessing.java
1    /* 
2    *A program to perform some basic operations on a list of String names. 
3     */
4    package arraylistplay;
5    
6    import java.util.ArrayList;
7    import java.util.List;
8    
9    public class ArrayListProcessing {
10   
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("Eilish, Billie");
21           names.add("Reynolds, Dan");
22           names.add("Sermon, Wayne");
23   
24           //POINT B: Create an ArrayList of just the first names of the names
25           //ArrayList. Use a for loop to print out the names, separated
26           // by spaces.
27           List<String> firstNamesList = firstNames(names);
28   
29           System.out.print("First names: ");
30           for (String firstName : firstNamesList) {
31               System.out.print(firstName + " ");
32           }
33           System.out.println();
34   
35   
36           //POINT C: Use String's join function to create and print a String of
37           //Just the first names of the names ArrayList with each
38           // name separated by a comma.
39           String firstNames = String.join(", ", firstNamesList);
40           System.out.println("First name: " + firstNames);
41   
42           //POINT D: By analogy from points B and C, print a comma-separated
43           //list of the last names in the names ArrayList.
44   
45           List<String> lastNamesList = lastNames(names);
46   
47           System.out.print("Last names: ");
48           for (String lastName : lastNamesList) {
49               System.out.print(lastName + " ");
50           }
51           System.out.println();
52   
53           String lastNames = String.join(", ", lastNamesList);
54           System.out.println("Last name: " + lastNames);
55   
56           //POINT E: Print a comma-separated list of all uppercase first names
57           // from the list of names in the names ArrayList.
58           List<String> upperCaseFirstNameList = upperCaseNames(firstNames(names));
59           String upperCaseFirstNames = String.join(", ", upperCaseFirstNameList);
60   
61           System.out.println("Uppercase first names: " + upperCaseFirstNames);
62   
63           //POINT F: Print a comma-separated list of all hyphenated last names
64           //from the list of names in the names ArrayList.
65           List<String> hyphenatedLastNameList = hyphenatedNames(lastNames(names));
66           String hyphenatedLastName = String.join(", ", hyphenatedLastNameList);
67   
68           System.out.println("Hyphenated last names: " + hyphenatedLastName);
69   
70           //POINT G: Print the integer value of the total length of all names
71           //in the names ArrayList..
72           int totalLength = totalNameLength(names);
73           System.out.println("Total length: " + totalLength);
74   
75           //POINT H:Print the integer value of the total length of all
76           //first names in the names ArrayList
77           int totalFirstLength = totalNameLength(firstNames(names));
78           System.out.println("Total first name length: " + totalFirstLength);
79   
80           //POINT I: Print the integer value of the product of the length of
81           //all first names in the names ArrayList.
82           int totalProductLength = totalProductNameLength(names);
83           System.out.println("Total Product length: " + totalProductLength);
84   
85       }
86   
87       private static String firstName(String directoryStyleName) {
88           int dsnCommaPosition = directoryStyleName.indexOf(",");
89           String firstName = directoryStyleName.substring(dsnCommaPosition + 2);
90           return firstName;
91       }
92   
93       private static List<String> firstNames(List<String> names) {
94           List<String> firsts = new ArrayList<>();
95           for (String name : names) {
96               firsts.add(firstName(name));
97           }
98           return firsts;
99       }
100  
101      private static String lastName(String directoryStyleName) {
102          int dsnCommaPosition = directoryStyleName.indexOf(",");
103          String lastname = directoryStyleName.substring(0, dsnCommaPosition);
104          return lastname;
105      }
106  
107      private static List<String> lastNames(List<String> names) {
108          List<String> lasts = new ArrayList<>();
109          for (String name : names) {
110              lasts.add(lastName(name));
111          }
112          return lasts;
113      }
114  
115      public static List<String> upperCaseNames(List<String> names) {
116          List<String> uppercases = new ArrayList<>();
117          for (String name : names) {
118              uppercases.add(name.toUpperCase());
119          }
120          return uppercases;
121      }
122  
123      public static List<String> hyphenatedNames(List<String> names) {
124          List<String> hyphenateds = new ArrayList<>();
125          for (String name : names) {
126              if (name.contains("-")) {
127                  hyphenateds.add(name);
128              }
129          }
130          return hyphenateds;
131      }
132  
133       public static int totalNameLength(List<String> names) {
134           int totalLength = 0;
135         for (String name : names){
136           totalLength = totalLength + name.length();
137         }
138      return totalLength;
139       }
140  
141       public static int totalProductNameLength(List<String> names) {
142          int totalProductLength = 1;
143          for (String name : names) {
144              totalProductLength = totalProductLength * name.length();
145          }
146          return totalProductLength;
147       }
148  
149  
150  }
151  
152  
153  
154