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