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.\
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 names.add("Newell, Rachel");
23 names.add("Washington, George");
24
25 //POINT B: Use map and the toList collector to create an ArrayList of just
26 // the first names of the names ArrayList. Use a for loop to print
27 // out the names, separated by spaces.
28 List<String> firstNamesList = names.stream()
29 .map(n -> firstName(n))
30 .collect(Collectors.toList());
31
32 System.out.print("First names: ");
33 for (String firstName : firstNamesList) {
34 System.out.print(firstName + " ");
35 }
36 System.out.println();
37
38 //POINT C: Use map and the joining collector to create a String of just
39 // the first names of the names ArrayList with each name separated
40 // by a comma. Print it.
41 String firstNames = names.stream()
42 .map(n -> firstName(n))
43 .collect(Collectors.joining(", "));
44
45 System.out.println("First names: " + firstNames);
46
47 //POINT D: By analogy from point C, print a comma-separated list of the
48 // last names in the names ArrayList.
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
62 System.out.println("Uppercase first names: " + upperCaseFirstNames);
63
64 //POINT F: Print a comma-separated list of all hyphenated last names
65 // from the list of names in the names ArrayList.
66 String hyphenatedLastNames = names.stream()
67 .map(n -> lastName(n))
68 .filter(n -> n.contains("-"))
69 .collect(Collectors.joining(", "));
70
71 System.out.println("Hyphenated last names: " + hyphenatedLastNames);
72
73 //POINT G: Print the integer value of the total length of all names
74 // in the names ArrayList.
75 int totalLength = names.stream()
76 .map(n -> n.length())
77 .reduce(0, (n1, n2) -> n1 + n2);
78
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 int totalFirstNameLength = names.stream()
84 .map(n -> firstName(n).length())
85 .reduce(0, (n1, n2) -> n1 + n2);
86
87 System.out.println("Total First Name Length: " + totalFirstNameLength);
88
89 //POINT I: Print the integer value of the product of the length of
90 // all first names in the names ArrayList.
91 int totalFirstNameProduct = names.stream()
92 .map(n -> firstName(n).length())
93 .reduce(1, (n1, n2) -> n1 * n2);
94
95 System.out.println("Total Product: " + totalFirstNameProduct);
96 }
97
98 private static String firstName(String directoryStyleName) {
99 int spaceLocation = directoryStyleName.indexOf(" ");
100 return directoryStyleName.substring(spaceLocation + 1);
101 }
102
103 private static String lastName(String directoryStyleName) {
104 int commaLocation = directoryStyleName.indexOf(",");
105 return directoryStyleName.substring(0,commaLocation);
106 }
107
108
109
110 }
111