ArrayListProcessing.java
1    /* 
2    * A program to perform some basic operations on a list of String names. 
3    */
4    package arraylists;
5    
6    import javax.sound.midi.Soundbank;
7    import java.util.ArrayList;
8    import java.util.List;
9    import java.util.stream.Collectors;
10   
11   public class ArrayListProcessing {
12   
13       public static void main(String[] args){
14           // POINT A: Add some strings which 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   
22           // POINT B: Create an ArrayList of just the first names of the names
23           //          ArrayList. Use a for loop to print out the names, separated
24           //          by spaces.
25   
26           List<String> firstNamesList = names.stream()
27                   .map(n -> firstName(n))
28                   .collect(Collectors.toList());
29   
30           System.out.print("First names: ");
31           for(String firstName : firstNamesList){
32               System.out.print(firstName + " ");
33           }
34           System.out.println();
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 = names.stream()
40                   .map(n -> firstName(n))
41                   .collect(Collectors.joining(", "));
42   
43           System.out.println("First names: " + firstNames);
44   
45           // POINT D: By analogy from points B and C, print a comma-separated
46           // list of the last names in the names ArrayList.
47           List<String> lastNamesList = names.stream()
48                   .map(n -> lastName(n))
49                   .collect(Collectors.toList());
50   
51           System.out.print("Last names: ");
52           for(String lastName : lastNamesList){
53               System.out.print(lastName + " ");
54           }
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           String upperCaseFirstNames = names.stream()
59                   .map(n -> firstName(n))
60                   .map(n -> n.toUpperCase())
61                   .collect(Collectors.joining(", "));
62   
63           System.out.println("Uppercase first names: " + upperCaseFirstNames);
64   
65           // POINT F: Print a comma-separated list of all hyphenated last names
66           //          from the list of names in the names ArrayList.
67           String hyphenatedLastNames = names.stream()
68                   .map(n -> lastName(n))
69                   .filter(n -> n.contains("-"))
70                   .collect(Collectors.joining(", "));
71   
72           System.out.println("Hyphenated last names: " + hyphenatedLastNames);
73   
74           // POINT G: Print the integer value of the total length of all names
75           //          in the names ArrayList.
76           int totalLength = names.stream()
77                   .map(n -> n.length())
78                   .reduce(0, (n1,n2) -> n1 + n2);
79           System.out.println("Total length: " + totalLength);
80   
81           // POINT H: Print the integer value of the total length of all
82           //          first names in the names ArrayList.
83           int firstLength = names.stream()
84                   .map(n -> firstName(n))
85                   .map(n -> n.length())
86                   .reduce(0, (n1,n2) -> n1 + n2);
87           System.out.println("First name length: " + firstLength);
88   
89   
90           // POINT I: Print the integer value of the product of the length of
91           //          all first names in the names ArrayList.
92           int first1Length = names.stream()
93                   .map(n -> firstName(n))
94                   .map(n -> n.length())
95                   .reduce( 1, (n1,n2) -> n1 *n2);
96           System.out.println("First name product length: "+first1Length);
97       }
98       private static String firstName(String directoryStyleName) {
99           return directoryStyleName.substring(0,directoryStyleName.indexOf(","));
100      }
101      private static String lastName(String directoryStyleName) {
102          return directoryStyleName.substring(directoryStyleName.indexOf(" "));
103      }
104  }
105  
106  
107