ArrayListProcessing.java
1    /* 
2     * A program to perform some basic operations on a list of String Names. 
3     */
4    
5    
6    package arraylistplay;
7    
8    import java.util.ArrayList;
9    import java.util.List;
10   
11   public class ArrayListProcessing {
12   
13       public static void main(String[] arg) {
14   
15           //Point A: Add some strings which represent names to an ArrayList.
16           List<String> names = new ArrayList<>();
17           names.add("Holiday, Billie");
18           names.add("Squarepants, Spongebob");
19           names.add("Brady, Tom");
20           names.add("Saget, Bob");
21           names.add("Joe, Billy-Bob");
22           names.add("Green, Nathan");
23   
24           //Point B: Create An Arraylist of just the first names of the names Arraylist, seperated by spaces
25           System.out.println("First name ...");
26           for (String name : names) {
27               System.out.println(firstName(name));
28           }
29           System.out.println();
30   
31           //Point C: Use String`s join function to create and print string of the first names of the names ArrayList
32           // Each line seperated by a comma
33            String firstNames = String.join(", ", firstNamesList(names));
34           System.out.println("First names: " + firstNames);
35           System.out.println();
36   
37           //Point D: By analogy from points B and C, print a comma-separated list of the last names
38           // In the names ArrayList
39           System.out.println("Last name ...");
40           for (String name : names) {
41               System.out.println(lastName(name));
42           }
43           System.out.println();
44   
45           String lastNames = String.join(", ", lastNamesList(names));
46           System.out.println("Last names: " + lastNames);
47   
48           // Point E: Print a comma-seperated list of all uppercase first names
49           List<String> upperCaseFirstNamesList = upperCaseNames(firstNamesList(names));
50           String upperCaseFirstNames = String.join(", ", upperCaseFirstNamesList);
51           System.out.println("Uppercase first names: " + upperCaseFirstNames);
52   
53           //Point F: Print a comma-seperated list of all hyphenated last names
54           List<String> hyphenatedNamesList = hyphenatedNames(lastNamesList(names));
55           String hyphenatedNames = String.join(", ", hyphenatedNamesList);
56           System.out.println("hyphenateds names: " + hyphenatedNames);
57   
58           // Point G: Print the integer value of the total length of all names
59           int totalLength = totalNameLength(names);
60           System.out.println("Total length: " + totalLength);
61   
62           // Point H: Print the integer value of the total length of all first names
63           int totalfirstLength = totalfirstNameLength(names);
64           System.out.println("Total first name length: " + totalfirstLength);
65   
66           //Point I: Print the integer value of the product of the length of all first names
67           double productoflength = productNamesLength(names);
68           System.out.println(("product of length " + productoflength));
69   
70       }
71   
72       private static List<String> firstNamesList(List<String> names) {
73           List<String> first = new ArrayList<>();
74           for (String name : names) {
75               first.add(firstName(name));
76           }
77           return first;
78       }
79   
80       private static List<String> lastNamesList(List<String> names) {
81           List<String> last = new ArrayList<>();
82           for (String name : names) {
83               last.add(lastName(name));
84           }
85           return last;
86       }
87   
88       private static String firstName(String directoryStyleName) {
89           int commaPosition = directoryStyleName.indexOf(",");
90           return directoryStyleName.substring(commaPosition + 2);
91       }
92   
93       private static String lastName(String directoryStyleName) {
94           int commaPosition = directoryStyleName.indexOf(",");
95           return directoryStyleName.substring(0, commaPosition);
96       }
97   
98       public static List<String> upperCaseNames(List<String> names) {
99           List<String> uppercases = new ArrayList<>();
100          for (String name : names) {
101              uppercases.add(name.toUpperCase());
102          }return uppercases;
103      }
104  
105      public static List<String> hyphenatedNames(List<String> names) {
106          List<String> hyphenateds = new ArrayList<>();
107          for (String name : names) {
108              if (name.contains("-")) {
109                  hyphenateds.add(name);
110              }
111          }return hyphenateds;
112      }
113  
114      public static int totalNameLength(List<String> names) {
115          int totalLength = 0;
116          for (String name : names) {
117              totalLength = totalLength + name.length();
118          }return totalLength;
119      }
120  
121      public static int totalfirstNameLength(List<String> names) {
122          int totalfirstLength = 0;
123          for (String name : names) {
124              totalfirstLength = totalfirstLength + firstName(name).length();
125          }
126          return totalfirstLength;
127      }
128  
129      public static double productNamesLength(List<String> names) {
130          double productoflength = 1;
131          for (String name : names) {
132              productoflength = productoflength * name.length();
133          }
134          return productoflength;
135      }
136  }