StreamArrayListProcessing.java
1    
2    
3               /* 
4         * A program to perform some basic operations on a list of Strings using Java streams3     */
5    
6                package arraylists;
7    
8                import java.util.ArrayList;
9        import java.util.List;
10       import java.util.stream.Collectors;
11   
12              public class StreamArrayListProcessing {
13   
14                  public static void main (String [] args) {
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("Claudel, Camllie");
19                      names.add("Picasso, Pablo");
20                      names.add("Gallen-Kallela, Akseli");
21                      names.add("Zotto, Osvaldo");
22                      names.add("Bob, BobBy");
23                      names.add("Jadwin-Gavin, Adam");
24                      // POINT B: Use map and the toList collector to create an ArrayList of just the first names of the names ArrayList.
25                      //          Use a for loop to print out the names, separated by spaces.
26                      List<String> firstNameList = names.stream().map(n -> firstName(n)).collect(Collectors.toList());
27                      System.out.print("First names: ");
28                     for (String firstName : firstNameList) {
29                              System.out.print(firstName + " ");
30                          }
31                      System.out.println();
32                     // POINT C: Use map and the joining collector to created a String of just the first names of the names ArrayList
33                      //          with each name separated by a comma. Print it.
34                     String firstNames = names.stream().map(n -> firstName(n)).collect(Collectors.joining(", "));
35                      System.out.println("First names: " + firstNames);
36                      // POINT D: By analogy from point C, print a comma-separated list of the last names in the names ArrayList.
37                      List<String> lastNameList = names.stream().map(n -> lastName(n)).collect(Collectors.toList());
38                      System.out.print("Last names: ");
39                      for (String lastName : lastNameList) {
40                             System.out.print(lastName + " ");
41                         }
42                      System.out.println();
43                      String lastNames = names.stream().map(n -> lastName(n)).collect(Collectors.joining(", "));
44                      System.out.println("Last names: " + lastNames);
45                      // POINT E: Print a comma-separated list of all uppercase first names from the list of names in the names ArrayList.
46                      String upperCaseFirstNames = names.stream().map(n -> firstName(n)).map(n -> n.toUpperCase()).collect(Collectors.joining(", "));
47                      System.out.println("Uppercase first names: " + upperCaseFirstNames);
48                      // POINT F: Print a comma-separated list of all hyphenated last names from the list of names in the names ArrayList.
49                      String hyphenatedLastNames = names.stream().map(n -> lastName(n)).filter(n -> n.contains("-")).collect(Collectors.joining(", "));
50                      System.out.println("HyphenatedLastNames: " + hyphenatedLastNames);
51                      // POINT G: Print the integer value of the total length of all names in the names ArrayList.
52                      int totalLength = names.stream().map(n -> n.length()).reduce(0,(n1,n2) -> n1 + n2);
53                      System.out.println("Total Length: " + totalLength);
54                      // POINT H: Print the integer value of the total length of all first names in the names ArrayList.
55                      int firstNameTotalLength = firstNameList.stream().map(n -> n.length()).reduce(0,(n1, n2) -> n1 + n2);
56                     System.out.println("Total first names length: " + firstNameTotalLength);
57                      // POINT I: Print the integer value of the product of the length of all first names in the names ArrayList.
58                      int namesProductLength = firstNameList.stream().map(n -> n.length()).reduce(1,(n1,n2) -> n1*n2);
59                      System.out.println("Total product of names length: " + namesProductLength);
60                  }
61          private static String lastName(String directoryStyleName) {
62                      int lastNameComma = directoryStyleName.indexOf(",");
63                      String lastName = directoryStyleName.substring(0,lastNameComma);
64                      return lastName;
65                  }
66          private static String firstName(String directoryStyleName) {
67                     int firstNameComma = directoryStyleName.indexOf(",");
68                      String firstName = directoryStyleName.substring(firstNameComma+2);
69           return firstName;
70                  }
71   
72              }
73   
74