StreamArrayListProcessing.java
|
1 package arraylists;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import java.util.stream.Collectors;
7
8 public class StreamArrayListProcessing {
9
10 public static void main(String[] args) {
11 // POINT A: Add some strings which represent names to an ArrayList.
12 List<String> names = new ArrayList<>();
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 // POINT B: Use map and the toList collector to create a String of
19 // just the first names of the names ArrayList. Use a for loop
20 // to print out the names, separated by spaces.
21 List<String> firstNamesList = names.stream()
22 .map(n -> firstName(n))
23 .collect(Collectors.toList());
24 System.out.print("First names: ");
25 for (String firstName : firstNamesList) {
26 System.out.print(firstName + " ");
27 }
28 // SPACE BETWEEN B and C
29 System.out.println();
30 // POINT C: Use map and the joining collector to create a String of
31 // just the first names of the names ArrayList with each name
32 // separated by a comma. Print it.
33 String firstNames = names.stream()
34 .map(n -> firstName(n))
35 .collect(Collectors.joining(", "));
36 System.out.println("First names: " + firstNames);
37 // POINT D: By analogy from Point C, print a comma-separated list of
38 // the last names in the names ArrayList.
39 String lastNames = names.stream()
40 .map(n -> lastName(n))
41 .collect(Collectors.joining(", "));
42 System.out.println("Last names: " + lastNames);
43 // POINT E: Print a comma-separated list of all uppercase first names
44 // from the list of names in the names ArrayList
45 String upperCaseFirstNames = names.stream()
46 .map(n -> firstName(n))
47 .map(n -> n.toUpperCase())
48 .collect(Collectors.joining(", "));
49 System.out.println("Uppercase first names: " + upperCaseFirstNames);
50 // POINT F: Print a comma-separated list of all hyphenated last names
51 // from the list of names in the names ArrayList.
52 String hyphenatedLastNames = names.stream()
53 .map(n -> lastName(n))
54 .filter(n -> n.contains("-"))
55 .collect(Collectors.joining(", "));
56 // POINT G: Print the integer value of the total length of all names
57 // in the names ArrayList.
58 int totalLength = names.stream()
59 .map(n -> n.length())
60 .reduce(0, (n1,n2) -> n1 + n2 );
61 System.out.println("Total length: " + totalLength);
62 // POINT H: Print the integer value of the total length of all names
63 // in the names ArrayList.
64 int totalFirstLength = firstNamesList.stream()
65 .map(n -> n.length())
66 .reduce(0, (n1,n2) -> n1 + n2 );
67 System.out.println("Total length of first names: " + totalFirstLength);
68 // POINT I: Print the integer value of the product of the length
69 // of all first names in the names ArrayList.
70 int productFirstLength = firstNamesList.stream()
71 .map(n -> n.length())
72 .reduce(1, (n1,n2) -> n1 * n2 );
73 System.out.println("Product of length of first names: " + productFirstLength);
74 } // END OF MAIN METHOD
75
76 // POINT B - first names
77 private static String firstName(String directoryStyleName) {
78 int space = directoryStyleName.indexOf(" ");
79 return directoryStyleName.substring(space + 1);
80 }
81 // POINT D - last names
82 private static String lastName(String directoryStyleName) {
83 int comma = directoryStyleName.indexOf(",");
84 return directoryStyleName.substring(0, comma) ;
85 }
86 } // END OF CLASS