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