StreamArrayListProcessing.java
|
1 /*
2 * A program to perform some basic operations on a list of string
3 * using Java streams.
4 */
5
6 package arraylistplay;
7
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.stream.Collectors;
11
12 public class StreamArrayListProcessing {
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("Zotton, Osvaldo");
21 names.add("Harry, Khant");
22 names.add("Roronoa, Zoro");
23 names.add("Monkey-D, Luffy");
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 name 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 Name: "+ 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 Name: "+ hyphenatedLastNames);
72
73 //Point G: Print the integer value of the total length of all
74 // names 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
82 // all first name in the names ArrayList.
83 int totallengthFirstName = names.stream()
84 .map(n-> firstName(n))
85 .map(n-> n.length())
86 .reduce(0, (n1,n2) -> n1+n2);
87
88 System.out.println("Total Length of the first names: " + totallengthFirstName);
89
90 // POINT I: Print the integer value of the product of the length of
91 // all first names in the names ArrayList.
92 int productOfTotalLength = names.stream()
93 .map(n-> firstName(n).length())
94 .reduce(1, (n1,n2) -> n1*n2);
95
96 System.out.println("Product of the lengths: "+ productOfTotalLength);
97 }
98
99 // private static List<String> lastNames(List<String> names) {
100 // List<String> lasts = new ArrayList<>();
101 // for (String name : names){
102 // lasts.add(lastName(name));
103 // }
104 // return lasts;
105 // }
106 //
107 // private static List<String> firstNames(List<String> names) {
108 // List<String> firsts = new ArrayList<>();
109 // for (String name : names) {
110 // firsts.add(firstName(name));
111 // }
112 // return firsts;
113 // }
114 //
115 // public static List<String> hyphenatedNames(List<String> names){
116 // List<String> hyphenateds = new ArrayList<>();
117 // for (String name : names){
118 // if (name.contains("-")){
119 // hyphenateds.add(name);
120 // }
121 // }
122 // return hyphenateds;
123 // }
124 //
125 // public static List<String> upperCaseName (List<String> names){
126 // List<String> uppercases = new ArrayList<>();
127 // for (String name : names){
128 // uppercases.add(name.toUpperCase());
129 // }
130 // return uppercases;
131 // }
132 //
133 // public static int totalNameLength(List<String> names){
134 // int totalLength = 0;
135 // for (String name : names){
136 // totalLength = totalLength + name.length();
137 // }
138 // return totalLength;
139 // }
140 //
141 // private static int totalNameProduct(List<String> names) {
142 // int totalLength = 1;
143 // for (String name : names){
144 // totalLength = totalLength * name.length();
145 // }
146 // return totalLength;
147 // }
148
149 private static String firstName(String name) {
150 String firstName = name.substring(name.indexOf(",")+2);
151 return firstName;
152 }
153
154 private static String lastName(String directoryStyleName) {
155 String lastName = directoryStyleName.substring(0,directoryStyleName.indexOf(","));
156 return lastName;
157 }
158 }
159