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("Awesome, Rinaldo");
23 names.add("Realle, Johannis");
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, including all commas and spaces: " + totalLength);
80
81 //POINT H: Print the integer value of the total length of all
82 // first names in the names ArrayList.
83 int lengthOfFirstNames = names.stream()
84 .map(n -> firstName(n))
85 .map(n -> n.length())
86 .reduce(0, (n1, n2) -> n1 + n2);
87
88 System.out.println("The length of the first names: " + lengthOfFirstNames);
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 productOfFirstNames = names.stream()
93 .map(n -> firstName(n))
94 .map(n -> n.length())
95 .reduce(1,(n1, n2) -> n1 * n2);
96
97 System.out.println("The product of the first names: " + productOfFirstNames);
98 }
99
100 private static String firstName(String directoryStyleName) {
101 int comma = directoryStyleName.indexOf(",");
102 String firstName = directoryStyleName.substring(comma + 2);
103 return firstName;
104 }
105
106 private static String lastName(String directoryStyleName) {
107 int comma = directoryStyleName.indexOf(",");
108 String lastName = directoryStyleName.substring(0, comma);
109 return lastName;
110 }
111 }
112