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
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("Tepfenhart-Yamada, Emily");
23 names.add("Jingleheimer-Schmidt, John Jacob");
24
25
26 //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.
27
28 List<String> firstNamesList = names.stream()
29 .map(n -> firstName(n))
30 .collect(Collectors.toList());
31
32 System.out.println("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 the first names of the names ArrayLis with each name separated by a comma. Print it.
39 String firstNames = names.stream()
40 .map(n -> firstName(n))
41 .collect(Collectors.joining(", "));
42
43 System.out.println("First names: " + firstNames);
44
45 //POINT D: By analogy from point C, print a comma-separated list of the last names in the names ArrayList.
46 String lastNames = names.stream()
47 .map(n -> lastName(n))
48 .collect(Collectors.joining(", "));
49
50 System.out.println("Last names: " + lastNames);
51
52 //POINT E: Print a comma-separated list of all uppercase first names from the list of names in the names ArrayList.
53 String upperCaseFirstNames = names.stream()
54 .map(n -> firstName(n))
55 .map(n -> n.toUpperCase())
56 .collect(Collectors.joining(", "));
57
58 System.out.println("Uppercase first names: " +upperCaseFirstNames);
59
60 //POINT F: Print a comma-separated list of all hyphenated last names from the list of names in the names ArrayList.
61 String hyphenatedLastNames = names.stream()
62 .map(n -> lastName(n))
63 .filter(n -> n.contains("-"))
64 .collect(Collectors.joining(", "));
65
66 System.out.println("Hyphenated last names: " + hyphenatedLastNames);
67
68 //POINT G: Print the integer value of the total length of all names in the names ArrayList.
69 int totalLength = names.stream()
70 .map(n -> n.length())
71 .reduce(0, (n1, n2) -> n1 + n2);
72
73 System.out.println("Total length: " + totalLength);
74
75 //POINT H: Print the integer value of the total length of all names in the names ArrayList.
76 int totalLengthOfFirstNames = names.stream()
77 .map(n -> firstName(n))
78 .map(n -> n.length())
79 .reduce(0, (n1, n2) -> n1 + n2);
80
81 System.out.println("Total first name length: " + totalLengthOfFirstNames);
82
83 //POINT I: Print the integer value of the product of the length of all first names in the names ArrayList.
84 int totalProductOfLength = names.stream()
85 .map(n -> n.length())
86 .reduce(1, (n1, n2) -> n1 * n2);
87
88 System.out.println("Total product of name length: " + totalProductOfLength);
89
90 }
91
92 private static String firstName(String directoryStyleNames) {
93
94 return directoryStyleNames.substring(directoryStyleNames.indexOf(" ") + 1);
95 }
96
97 private static String lastName(String directoryStyleName){
98
99 return directoryStyleName.substring(0,directoryStyleName.indexOf(","));
100 }
101
102
103 }