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