StreamArrayListProcessing.java
|
1 package arraylist;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.stream.Collectors;
6
7 public class StreamArrayListProcessing {
8
9 public static void main(String[] args) {
10
11 //POINT A: Add some strings which represent names to an ArrayList
12 List<String> names = new ArrayList<>();
13 names.add("Holiday, Billie");
14 names.add("Claudel, Camille");
15 names.add("Piscasso, Pablo");
16 names.add("Gallen-Kallela, Akesli");
17 names.add("Zotto, Osvaldo");
18 names.add("Maldonado, Gregory");
19 names.add("Smith, Katherine");
20
21 //POINT B: Use map and the toList collector to create an Arraylist of just
22 // The first names of the names ArrayList. Use a for loop to print out the names, separated by spaces
23
24 List<String> firstNamesList = names.stream()
25 .map( n -> firstName(n))
26 .collect(Collectors.toList());
27
28 System.out.print("First Names: ");
29 for (String firstName : firstNamesList) {
30 System.out.print(firstName + " ");
31 }
32 System.out.println();
33
34 //POINT C: Use map and the joining collector to create a String of just the first names of the names
35 // ArrayList with eaceh name separatede by a comma. Print it.
36
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
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 ofnames in the names Arraylist
52
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-sparated list of all hyphenated last names from the list of names in the names ArrayList
61
62 String hyphenatedLastNames = names.stream()
63 .map(n -> lastName(n))
64 .filter(n -> n.contains("-"))
65 .collect(Collectors.joining(", "));
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
70 int totalLength = names.stream()
71 .map(n -> n.length())
72 .reduce(0, (n1, n2) -> n1 + n2);
73
74 System.out.println("Total length: " + totalLength);
75
76 //POINT H: Print the integer value of the total length of all first names in the names Array List
77
78 int firstNamesLength = names.stream()
79 .map(n -> firstName(n))
80 .map(n -> n.length())
81 .reduce(0, (n1, n2) -> n1 + n2);
82
83 System.out.println("Length of First Names: "+ firstNamesLength);
84
85 //POINT I: Print the integer value of the prduct of the length of all first names in the names ArrayList
86
87 int productFirstNames = names.stream()
88 .map( n -> firstName(n))
89 .map(n -> n.length())
90 .reduce(1, (n1, n2) -> n1 * n2);
91
92 System.out.println("Product of First Names:" + productFirstNames);
93 }
94
95 private static String firstName(String directoryStyleName) {
96
97 int index = directoryStyleName.indexOf(",");
98 String firstName = directoryStyleName.substring(index + 2);
99
100 return firstName;
101 }
102
103 private static String lastName(String directoryStyleName) {
104
105 int index = directoryStyleName.indexOf(",");
106 String lastName = directoryStyleName.substring(0, index);
107
108 return lastName;
109 }
110
111 }
112