StreamArrayListProcessing.java
|
1 /*
2 * A program to perform some basic operations on a list of storage
3 * using Java streams
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("Curry, Stephen");
22 names.add("Thompson, Klay");
23 names.add("Green, Draymond");
24 names.add("Russel, Diangelo");
25
26 // POINT B: Use map and the toList collector to create an ArrayList of just the fist names of the name ArrayList. use a for loop to print out the names, separated by spaces
27 List<String> firstNamesList = names.stream()
28 .map(n -> firstName(n))
29 .collect(Collectors.toList());
30
31 System.out.println("First name: ");
32 for (String firstName : firstNamesList) {
33 System.out.print(firstName + " ");
34 }
35 System.out.println();
36
37 // 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.
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 comma-separated list of the last names in the names ArrayList.
45 String lastNames = names.stream()
46 .map(n -> lastName(n))
47 .collect(Collectors.joining(", "));
48
49 System.out.println("Last names: " + lastNames);
50
51 // POINT E: Print a comma-separated list of all uppercase first names from the list of names in the names ArrayList.
52 String upperCaseFirstNames = names.stream()
53 .map(n -> firstName(n))
54 .map(n -> n.toUpperCase())
55 .collect(Collectors.joining(", "));
56
57 System.out.println("Uppercase first name: " + upperCaseFirstNames);
58
59 // POINT F: Print a comma-separated list of all hyphenated list of all hyphenated last names 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 in the names ArrayList.
68 int totalLength = names.stream()
69 .map(n -> n.length())
70 .reduce(0, (n1, n2) -> n1 + n2);
71
72 System.out.println("Total length: " + totalLength);
73
74 // POINT H: Print the integer value of the total length of all first names in the name ArrayList.
75 int totalFirstNamesLength = names.stream()
76 .map(n -> firstName(n))
77 .map(n -> n.length())
78 .reduce(0, (n1, n2) -> n1 + n2);
79
80 System.out.println("Total first names length: " + totalFirstNamesLength);
81
82 // POINT I: Print the integer value of the product of the length of all first names in the name ArrayList.
83 int totalLengthProduct = names.stream()
84 .map(n -> n.length())
85 .reduce(0, (n1, n2) -> n1 * n2);
86
87 System.out.println("Total length: " + totalLengthProduct);
88
89 }
90 private static String firstName(String directoryStyleName) {
91 int firstComma = directoryStyleName.indexOf(",");
92 String firstName = directoryStyleName.substring(firstComma + 2);
93 return firstName;
94
95 }
96 private static String lastName(String directoryStyleName) {
97 int lastComma = directoryStyleName.indexOf(",");
98 String lastName = directoryStyleName.substring(0, lastComma);
99 return lastName;
100 }
101 }
102