StreamArrayListProcessing.java
|
1 /*
2 * A program to perform some basic operations on a list of Strings
3 * using Java streams.
4 */
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. List<String> names = new ArrayList<>();
16 List<String> names = new ArrayList<>();
17 names.add("Holiday, Billie");
18 names.add("Claudel, Camille");
19 names.add("Picasso, Pablo");
20 names.add("Gallen-Kallela, Akseli");
21 names.add("Zotto, Osvaldo");
22
23 // POINT B: Use map and the toList collector to create an ArrayList of just
24 // the first names of the names ArrayList. Use a for loop to print
25 // out the names, separated by spaces.
26
27 List<String> firstNamesList = names.stream().map(n -> firstName(n)).collect(Collectors.toList());
28 System.out.print("First names: ");
29 for (String firstName : firstNamesList) {
30 System.out.print(firstName + " ");
31 }
32 System.out.println();
33
34 // POINT C: Use map and the joining collector to create a String of just
35 // the first names of the names ArrayList with each name seperated
36 // by a coma. Print it.
37
38 String firstNames = names.stream()
39 .map(n -> firstName(n))
40 .collect(Collectors.joining(", "));
41
42 System.out.println("First names: " + firstNames);
43
44 // POINT D: By analogy from point C, print a coma-seperated list of the
45 // last names in the names ArrayList
46
47 String lastNames = names.stream()
48 .map(n -> lastName(n))
49 .collect(Collectors.joining(", "));
50
51 System.out.println("Last names: " + lastNames);
52
53 // POINT E: Print a coma-seperated list of all uppercase first names
54 // from the list of names in the names ArrayList
55
56 String upperCaseFirstNames = names.stream()
57 .map(n -> firstName(n))
58 .map(n -> n.toUpperCase())
59 .collect(Collectors.joining(", "));
60
61 System.out.println("Uppercase first names: " + upperCaseFirstNames);
62
63 // POINT F: Print a coma-seperated list of all hyphenated last names
64 // from the list of names in the names ArrayList
65
66 String hyphenatedLastNames = names.stream()
67 .map(n -> lastName(n))
68 .filter(n -> n.contains("-"))
69 .collect(Collectors.joining(", "));
70
71 System.out.println("hypenated last names: " + hyphenatedLastNames);
72
73 // POINT G: Print the integer value of the total length of all names
74 // in the names ArrayList.
75
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
84 int totalFirstLength = firstNamesList.stream()
85 .map(n -> n.length())
86 .reduce(0, (n1, n2) -> n1 + n2);
87 System.out.println("Total first names length:" + totalFirstLength);
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
93 int totalProduct = names.stream()
94 .map(n -> n.length())
95 .reduce(1, (n1, n2) -> n1 + n2);
96 System.out.println("Total product: " + totalProduct);
97 }
98
99 private static String firstName(String directoryStyleName) {
100 int p1 = directoryStyleName.indexOf(" ");
101 return directoryStyleName.substring(p1 + 1);
102 }
103
104 private static String lastName(String directoryStyleName) {
105 int p1 = directoryStyleName.indexOf(",");
106 return directoryStyleName.substring(0, p1);
107 }
108
109
110 }