StreamArrayListProcessing.java
|
1 /*
2 * A program to perform some basic operations on a list of String names
3 */
4 package arraylists;
5
6 import java.util.ArrayList;
7 import java.util.List;
8 import java.util.stream.Collectors;
9
10 class SteamArrayListProcessing {
11
12 public static void main(String[] args) {
13 //POINT A: Add some strings which represent names to an ArrayList.
14 List<String> names = new ArrayList<>();
15 names.add("Holiday, Billie");
16 names.add("Claudel, Camille");
17 names.add("Picasso, Pablo");
18 names.add("Gallen-Kallela, Akseli");
19 names.add("Zotto, Osvaldo");
20 names.add("LaRosa, Danielle");
21 names.add("Berk, Erik");
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 List<String> firstNamesList = names.stream()
27 .map(n -> firstName(n))
28 .collect(Collectors.toList());
29 System.out.print("First names: ");
30 for (String firstName : firstNamesList) {
31 System.out.print(firstName + " ");
32 }
33 System.out.println();
34
35 //POINT C: Use map and the joining collector to create a String of just
36 // the first names of the names ArrayList with each name separated
37 // by a comma. Print it.
38
39 String firstNames = names.stream()
40 .map(n -> firstName(n))
41 .collect(Collectors.joining(", "));
42
43 System.out.println("First names: " + firstNames);
44
45
46 //POINT D: By analogy from points B and C, print a comma-separated
47 //list of the last names in the names ArrayList
48
49 String lastNames = names.stream()
50 .map(n -> lastName(n))
51 .collect(Collectors.joining(", "));
52
53 System.out.println("Last names: " + lastNames);
54
55 //POINT E: Print a comma-separated list of all uppercase first names
56 //from the list of names in the names ArrayList
57 String upperCaseFirstNames = names.stream()
58 .map(n -> firstName(n))
59 .map(n -> n.toUpperCase())
60 .collect(Collectors.joining(", "));
61 System.out.println("Uppercase first names: " + upperCaseFirstNames);
62
63 //POINT F: Print a comma-separated list of all hyphenated last names
64 // from the list of names in the names ArrayList
65 String hyphenatedLastNames = names.stream()
66 .map(n -> lastName(n))
67 .filter(n -> n.contains("-"))
68 .collect(Collectors.joining(", "));
69 System.out.println("Hyphenated last names: " + hyphenatedLastNames);
70
71
72 //POINT G: Print the integer values of the total length of all names in the names ArrayList
73 int totalLength = names.stream()
74 .map(n -> n.length())
75 .reduce(0, (n1, n2) -> n1 + n2);
76 System.out.println("Total Length: " + totalLength);
77
78
79 //POINT H: Print the integer value of the total length of all
80 //first names in the names ArrayList
81 int fNameTotalLength = names.stream()
82 .map(n -> firstName(n))
83 .map(n -> n.length())
84 .reduce(0, (n1, n2) -> n1 + n2);
85 System.out.println("Total First Name Length: " + fNameTotalLength);
86
87 //POINT I: Print the integer value of the product of the length of
88 //all first names in the name ArrayList
89 int productLength = names.stream()
90 .map(n -> firstName(n))
91 .map(n -> n.length())
92 .reduce(1, (n1, n2) -> n1 * n2);
93 System.out.println("Product of first names length: " + productLength);
94
95
96 }
97
98 private static String firstName(String directoryStyleName) {
99 int positionOfComma = directoryStyleName.indexOf(",");
100 String firstName = directoryStyleName.substring(positionOfComma + 2); ///Without the +2 the comma shows in the output
101 return firstName;
102 }
103
104
105
106 private static String lastName(String directoryStyleName) {
107 int positionOfComma = directoryStyleName.indexOf(",");
108 String lastName = directoryStyleName.substring(0, positionOfComma);
109 return lastName;
110 }
111
112
113
114
115 }
116
117