ArrayListProcessing.java
1    package arraylists;
2    
3    import java.util.ArrayList;
4    import java.util.List;
5    import java.util.stream.Stream;
6    
7    public class ArrayListProcessing {
8        public static void main(String[] args) {
9            // POINT A: Add some strings which represent names to an ArrayList.
10           // First, make the list
11           List<String> names = new ArrayList<String>();
12           // Second, add its elements using ArrayList.add();
13           names.add("Holiday, Billie");
14           names.add("Claudel, Camille");
15           names.add("Picasso, Pablo");
16           names.add("Gallen-Kallela, Akseli");
17           names.add("Zotto, Osvaldo");
18   
19           // POINT B: Create an ArrayList of just the first name of the names
20           //          ArrayList. Use a for loop to print out the names, separated
21           //          by spaces.
22           List<String> firstNamesList = firstNames(names);
23           System.out.print("First names: ");
24           for (String firstName : firstNamesList) {
25               System.out.print(firstName + " ");
26           }
27   
28           // POINT C: Use String's join function to create and print a String of just
29           //          the first names of the names ArrayList with each name separated
30           //          by a comma.
31   
32           System.out.println();
33           String firstNames = String.join(", ", firstNamesList);
34           System.out.println("First names: " + firstNames);
35   
36           // POINT D: By analogy from points B and C, print a comma-separated
37           //          list of the last names in the names ArrayList.
38           List<String> lastNamesList = lastNames(names);
39           // System.out.print("Last names: ");
40           for (String lastName : lastNamesList) {
41               System.out.print(lastName + " ");
42           }
43   
44           String lastNames = String.join(", ", lastNamesList);
45           System.out.println("Last names: " + lastNames);
46   
47           // POINT E: Print a comma-separated list of all uppercase first names
48           //          from the list of names in the names ArrayList
49           List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names));
50           String upperCaseFirstNames = String.join(", ", upperCaseFirstNamesList);
51   
52           System.out.println("Uppercase first names: " + upperCaseFirstNames);
53   
54           // POINT F
55           List<String> hyphenatedNamesList = hyphenatedNames(lastNames(names));
56           String hyphenatedNames = String.join(", ", hyphenatedNamesList);
57   
58           System.out.println("Hyphenated last names: " + hyphenatedNames);
59   
60           // POINT G
61           int totalLength = totalNameLength(names);
62           System.out.println("Total length: " + totalLength);
63   
64           // POINT H
65           int firstLength = totalNameLength(firstNamesList);
66           System.out.println("Total length of first names: " + firstLength);
67   
68           // POINT I
69           int productLength = productNameLength(names);
70           System.out.println("Total product of length: " + productLength);
71   
72       }
73   
74       /* 
75       END OF MAIN METHOD 
76       */
77   
78           // POINT E
79           public static List<String> upperCaseNames(List<String> names) {
80           List<String> uppercases = new ArrayList<>();
81           for (String name : names) {
82               uppercases.add(name.toUpperCase());
83           }
84           return uppercases;
85       }   // or ... System.out.println("First names in uppercase: " + firstNames.toUpperCase());
86   
87           // POINT F: Print a comma-separated list of all hyphenated last names
88           //          from the list of names in the names ArrayList.
89   
90           public static List<String> hyphenatedNames(List<String> names) {
91           List<String> hyphenateds = new ArrayList<>();
92           for(String name :names){
93               if (name.contains("-")) {
94                   hyphenateds.add(name);
95               }
96           }
97           return hyphenateds;
98       }
99           // POINT G: Print the integer value of the total length of all names
100          //          in the names ArrayList.
101          public static int totalNameLength(List<String> names){
102          int totalLength = 0;  // accumulator
103          for (String name : names){
104              totalLength = totalLength + name.length();
105          }
106          return totalLength;
107          }
108  
109          // POINT H: Print the integer value of the total length of all
110          //          first names in the names ArrayList.
111                      // in main method.
112  
113          // POINT I: Print the integer value of the product of the length of
114          //          all first names in the names ArrayList.
115          public static int productNameLength(List<String> names){
116              int productLength = 1;  // accumulator
117              for (String name : names){
118                  productLength = ( productLength * name.length() );
119              }
120              return productLength;
121          }
122  
123  
124  
125      private static List<String> lastNames(List<String> names) {
126          List<String> lasts = new ArrayList<>();
127          for (String name : names) {
128              lasts.add(lastName(name));
129          }
130          return lasts;
131      }
132  
133      private static String lastName(String directoryStyleName) {
134          int space = directoryStyleName.indexOf(" ");
135          return directoryStyleName.substring(0, space - 1);
136      }
137  
138      private static List<String> firstNames(List<String> names){
139          List<String> firsts = new ArrayList<>();
140          for (String name : names) {
141              firsts.add(firstName(name));
142          }
143          return firsts;
144      }
145  
146      private static String firstName(String directoryStyleName) {
147          int space = directoryStyleName.indexOf(" ");
148          return directoryStyleName.substring(space + 1);
149      }
150  
151  }
152  
153  
154