StreamArrayListProcessing.java
1    /*A program to perform some basic operations on a list of Strings 
2    using Java streams.*/
3    
4      package arraylistplay;
5    
6      import java.util.ArrayList;
7      import java.util.List;
8      import java.util.stream.Collectors;
9    
10     public class StreamArrayListProcessing {
11         public static void main(String[] args) {
12             // point a: add some strings to this arraylist of names
13             List<String> names = new ArrayList<>();
14             names.add("Holiday, Billie");
15             names.add("Claudel, Camille");
16             names.add("Picasso, Pablo");
17             names.add("Gallen-Kallela, Akseli");
18             names.add("Zotto, Osvaldo");
19             names.add("Lennon, John");
20             names.add("Alighieri, Dante");
21   
22             // POINT B: Use map and the toList collector to create an ArrayList of just
23             //the first names of the names ArrayList. Use a for loop to print
24             //out the names, separated by spaces.
25             List<String> firstNamesList = names.stream()
26                     .map(n -> firstName(n))
27                     .collect(Collectors.toList());
28             System.out.print("First names: ");
29             for (String firstName : firstNamesList) {
30                 System.out.print(firstName + " ");}
31                 System.out.println();
32   
33             // POINT C: Use map and the joining collector to create a String of just
34             //          the first names of the names ArrayList with each name separated
35             //          by a comma. Print it.
36             String firstNames = names.stream()
37                     .map(n -> firstName(n))
38                     .collect(Collectors.joining(", "));
39             System.out.println("First names: " + firstNames);
40   
41             // POINT D: By analogy from point C, print a comma-separated list of the
42             //          last names in the names ArrayList
43             String lastNames = names.stream()
44             .map(n->lastName(n))
45                     .collect(Collectors.joining(", "));
46             System.out.println("Last names: " + lastNames);
47   
48             // POINT E: Print a comma-separated list of all uppercase first names
49             //          from the list of names in the names ArrayList.
50             String upperCaseFirstNames = names.stream()
51                     .map(n -> firstName(n))
52                     .map(n -> n.toUpperCase())
53                     .collect(Collectors.joining(", "));
54             System.out.println("Uppercase first names: " + upperCaseFirstNames);
55   
56             // POINT F: Print a comma-separated list of all hyphenated last names
57             //          from the list of names in the names ArrayList.
58             String hyphenateds = names.stream()
59             .map(n -> lastName(n))
60                     .filter(n -> n.contains("-"))
61                     .collect(Collectors.joining(", "));
62   
63             System.out.println("all hyphenated last names: " + hyphenateds);
64   
65             // POINT G: Print the integer value of the total length of all names
66             //          in the names ArrayList.
67             int totalLength = names.stream()
68                     .map(n -> n.length())
69                     .reduce(0, (n1, n2) -> n1 + n2);
70             System.out.println("Total length: " + totalLength);
71   
72             // POINT H: Print the integer value of the total length of all
73             //          first names in the names ArrayList.
74             int firstNameLength = names.stream()
75                     .map(n -> firstName(n))
76                     .map(n -> n.length())
77                     .reduce(0,(n1,n2) -> n1+n2);
78             System.out.println("the length of the first names: " + firstNameLength);
79   
80             // POINT I: Print the integer value of the product of the length of
81             //          all first names in the names ArrayList.
82             int productLength = names.stream()
83                     .map(n -> firstName(n))
84                     .map(n -> n.length())
85                     .reduce(1,(n1,n2)-> n1*n2);
86             System.out.println("the product of the length of all first names: " + productLength);
87   
88         }
89         private static String firstName(String directoryStyleName) {
90   
91   
92   
93             return(directoryStyleName.substring(directoryStyleName.lastIndexOf(" ")));
94         }
95         private static String lastName(String directoryStyleName) {
96   
97             return(directoryStyleName.substring(0,directoryStyleName.indexOf(",")));
98   
99   
100        }
101  
102        public static List<String> hyphenatedNames(List<String> names) {
103            List<String> hyphenateds = new ArrayList<>();
104            for (String name : names) {
105                if (name.contains("-")) {
106                    hyphenateds.add(name);
107                }
108            }
109            return hyphenateds;
110        }
111  
112    }
113