ArrayListProcessing.java
1    package arraylists;
2    
3    import java.util.ArrayList;
4    import java.util.List;
5    
6    public class ArrayListProcessing {
7    
8        /* 
9         * A program to perform some basic operations on a list of String names. 
10        */
11   
12       public static void main(String[] args) {
13   
14           // POINT A: add strings that represent names to an ArrayList
15           List<String> names = new ArrayList<>();
16           names.add("Holiday, Billie");
17           names.add("Claudel, Camille");
18           names.add("Picasso, Pablo");
19           names.add("Gallen-Kallela, Akseli");
20           names.add("Zotto, Osvaldo");
21           names.add("Robinson, Alexander");
22           names.add("Clark, Andrew");
23           names.add("Lopez, Edgar");
24   
25           // POINT B: create an ArrayList of only first names
26   
27           List<String> firstNamesList = firstNames(names);
28           System.out.print("First Names: ");
29           for (String firstName : firstNamesList) {
30               System.out.print(firstName + " ");
31           }
32           System.out.println();
33   
34           // POINT C: combining the Strings of the ArrayList into a single String using the join function
35           // ***each name is separated by a comma***
36   
37           String firstNames = String.join(", ", firstNamesList);
38           System.out.println("First Names: " + firstNames);
39   
40           // POINT D: the same concept of B and C but last names now
41   
42           List<String> lastNamesList = lastNames(names);
43           String lastNames = String.join(", ", lastNamesList);
44           System.out.println("Last Names: " + lastNames);
45   
46           // POINT E: a comma separated list of uppercase first names in the ArrayList
47   
48           List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names));
49           String upperCaseFirstNames = String.join(", ", upperCaseFirstNamesList);
50           System.out.println("Uppercase first names: " + upperCaseFirstNames);
51   
52   
53           // POINT F: a comma separated list of hyphenated last names in the ArrayList
54   
55           List<String> hyphenatedNamesList = hyphenatedNames(lastNames(names));
56           String hyphenatedNames = String.join(", ", hyphenatedNamesList);
57           System.out.println("Hyphenated names: " + hyphenatedNames);
58   
59           // POINT G: the integer value of the total length of all names in the ArrayList
60   
61           int totalLength = totalNameLength(names);
62           System.out.println("Total Length: " + totalLength);
63   
64           // POINT H: the integer value of the total length of all first names in the ArrayList
65   
66           int totalFirstsLength = totalNameLength(firstNames(names));
67           System.out.println("Total First Name Length: " + totalFirstsLength);
68   
69           // POINT I:
70   
71           int totalProduct = productNameLengths(firstNames(names));
72           System.out.println("Total First Name Product: " + totalProduct);
73   
74       }
75   
76       // FIRST NAME FUNCTIONS
77       private static String firstName(String directoryStyleName) {
78           int space = directoryStyleName.indexOf(" ");
79           return directoryStyleName.substring(space + 1);
80       }
81   
82       private static List<String> firstNames(List<String> names) {
83           List<String> firsts = new ArrayList<>();
84           for (String name : names) {
85               firsts.add(firstName(name));
86           }
87           return firsts;
88       }
89   
90       // LAST NAME FUNCTIONS
91       private static String lastName(String directoryStyleName) {
92           int comma = directoryStyleName.indexOf(",");
93           return directoryStyleName.substring(0, comma);
94       }
95   
96       private static List<String> lastNames(List<String> names) {
97           List<String> lasts = new ArrayList<>();
98           for (String name : names) {
99               lasts.add(lastName(name));
100          }
101          return lasts;
102      }
103  
104      // MAKES FIRST NAMES UPPERCASE
105      public static List<String> upperCaseNames(List<String> names) {
106          List<String> uppercases = new ArrayList<>();
107          for (String name : names) {
108              uppercases.add(name.toUpperCase());
109          }
110          return uppercases;
111      }
112  
113      // COLLECTS A LIST OF HYPHENATED NAMES
114      public static List<String> hyphenatedNames(List<String> names) {
115          List<String> hyphenateds = new ArrayList<>();
116          for (String name : names) {
117              if (name.contains("-")) {
118                  hyphenateds.add(name);
119              }
120          }
121          return hyphenateds;
122      }
123  
124      // TOTAL LENGTH OF ALL NAMES
125      public static int totalNameLength(List<String> names) {
126          int totalLength = 0;
127          for (String name : names) {
128              totalLength = totalLength + name.length();
129          }
130          return totalLength;
131      }
132  
133      private static int productNameLengths(List<String> names) {
134          int totalProduct = 1;
135          for (String name : names) {
136              totalProduct = totalProduct * name.length();
137          }
138          return totalProduct;
139      }
140  
141  }
142  
143