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 public class StreamArrayListProcessing {
12
13
14 public static void main(String[] args) {
15
16 // Point A: Add some strings which represent names to an ArrayList.
17 List<String> names = new ArrayList<>();
18 names.add("Holiday, Billie");
19 names.add("Claudel, Camille");
20 names.add("Picasso, Pablo");
21 names.add("Gallen-Kallela, Akseli");
22 names.add("Zotto, Osvaldo");
23 names.add("Amaratunga, Navya");
24
25
26 // Point B: Use map and the toList collector to create an ArrayList of just the first names of the names
27 // ArrayList. Use a for loop to print out the names, separated by spaces.
28 List<String> firstNamesList = names.stream()
29 .map(n -> firstName(n))
30 .collect(Collectors.toList());
31 System.out.print("First names: ");
32 for (String firstName : firstNamesList) {
33 System.out.print(firstName + " ");
34 }
35 System.out.println();
36
37
38 // Point C: Use map and the joining collector to create a String of just the first names of the names ArrayList
39 // with each name separates by a comma. Print it.
40 String firstNames = names.stream()
41 .map(n -> firstName(n))
42 .collect(Collectors.joining(", "));
43
44 System.out.println("First names: " + firstNames);
45
46 // Point D: By analogy from point C, print a comma-separated list of the last names
47 // in the names ArrayList.
48 String lastNames = names.stream()
49 .map(n -> lastName(n))
50 .collect(Collectors.joining(", "));
51
52 System.out.println("Last names: " + lastNames);
53
54
55 // Point E: Print a comma-separated list of all uppercase first names from the list of names
56 // 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
65 // Point F: Print a comma-separated list of all hyphenated last names from the list of names in the
66 // names ArrayList.
67 String hyphenatedLastNames = names.stream()
68 .map(n -> lastName(n))
69 .filter(n -> n.contains("-"))
70 .collect(Collectors.joining(", "));
71
72 System.out.println("Hyphenated last names: " + hyphenatedLastNames);
73 // Point G: Print the integer value of the total length of all names in the names ArrayList.
74 int totalLength = names.stream()
75 .map(n -> n.length())
76 .reduce(0, (n1,n2) -> n1+n2);
77
78 System.out.println("Total length: " + totalLength);
79
80
81 // Point H: Print the integer value of the total length of all first names in the names ArrayList.
82 int firstlength = firstNamesList.stream()
83 .map(n -> n.length())
84 .reduce(0, (n1,n2) -> n1+n2);
85
86 System.out.println("First name length: " + firstlength);
87
88
89 // Point I: Print the integer value of the product of the length of all the first names
90 // in the names ArrayList.
91
92 int product = firstNamesList.stream()
93 .map(n -> n.length())
94 .reduce(1, (n1,n2) -> n1*n2);
95
96 System.out.println("Product of the first names: " + product);
97
98 }
99
100
101 private static String firstName(String directoryStyleName) {
102 int commaPos = directoryStyleName.indexOf(",");
103 return directoryStyleName.substring(commaPos + 2);
104 }
105
106 private static String lastName(String directoryStyleName) {
107 int commaPos = directoryStyleName.indexOf(",");
108 return directoryStyleName.substring(0,commaPos);
109 }
110 }
111
112