StreamArrayListProcessing.java
|
1 /*
2 *A program to perform some basic operations on a list of Strings
3 * using Java streams.
4 */
5 package arraylistplay;
6
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.stream.Collectors;
10
11 public class StreamArrayListProcessing {
12 public static void main(String[] args) {
13 //POINT A: Add some strings which represent names to an ArrayList.
14 List<String> names = new ArrayList<>();
15 names.add("Holiday, Billie");
16 names.add("Claudel, Camille");
17 names.add("Picasso, Pablo");
18 names.add("Gallen-Kallela, Akseli");
19 names.add("Zotto, Osvaldo");
20 names.add("Eilish, Billie");
21 names.add("Reynolds, Dan");
22 names.add("Sermon, Wayne");
23
24 //POINT B: Use map and the toList collector to create an ArrayList of just
25 //the first names of the names ArrayList. Use a for loop to print
26 //out the names, separated by spaces.
27 List<String> firstNamesList = names.stream()
28 .map(n -> firstName(n))
29 .collect(Collectors.toList());
30
31 System.out.print("First names: ");
32 for (String firstName : firstNamesList) {
33 System.out.print(firstName + " ");
34 }
35 System.out.println();
36
37 //POINT C: Use map and the joining collector to create a String of just
38 //the first names of the names ArrayList with each name separated
39 //by a comma. Print it.
40 String firstNames = names.stream()
41 .map(n -> firstName(n))
42 .collect(Collectors.joining(", "));
43 System.out.println("First names: " + firstNames);
44
45 //POINT D: By analogy from point C, print a comma-separated list of the
46 //last names in the names ArrayList.
47 String lastNames = names.stream()
48 .map(n -> lastName(n))
49 .collect(Collectors.joining(", "));
50 System.out.println("Last name: " + lastNames);
51
52 //POINT E: Print a comma-separated list of all uppercase first names
53 //from the list of names in the names ArrayList.
54 String upperCaseFirstNames = names.stream()
55 .map(n -> firstName(n))
56 .map(n -> n.toUpperCase())
57 .collect(Collectors.joining(", "));
58 System.out.println("Uppercase first names: " + upperCaseFirstNames);
59
60 //POINT F: Print a comma-separated list of all uppercase first names
61 //from the list of names in the names ArrayList.
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
69 //names in the names ArrayList.
70 int totalLength = names.stream()
71 .map(n -> n.length())
72 .reduce(0,(n1 , n2) -> n1 + n2);
73 System.out.println("Total length: " + totalLength);
74
75 //POINT H: Print the integer value of the total length of all
76 //first names in the names ArrayList.
77 int totalFirstLength = names.stream()
78 .map(n -> firstName(n).length())
79 .reduce(0,(n1 , n2) -> n1 + n2);
80 System.out.println("Total first name length: " + totalFirstLength);
81
82 //POINT I: Print the integer value of the product of the length of
83 //all first names in the names ArrayList.
84 int totalProductLength = names.stream()
85 .map(n -> n.length())
86 .reduce(1, (n1 , n2) -> n1 * n2);
87 System.out.println("Total Product length: " + totalProductLength);
88 }
89
90 private static String firstName(String directoryStyleName) {
91 int dsnCommaPosition = directoryStyleName.indexOf(",");
92 String firstName = directoryStyleName.substring(dsnCommaPosition + 2);
93 return firstName;
94 }
95
96 private static String lastName(String directoryStyleName) {
97 int dsnCommaPosition = directoryStyleName.indexOf(",");
98 String lastname = directoryStyleName.substring(0, dsnCommaPosition);
99 return lastname;
100 }
101
102 public static List<String> upperCaseNames(List<String> names) {
103 List<String> uppercases = new ArrayList<>();
104 for (String name : names) {
105 uppercases.add(name.toUpperCase());
106 }
107 return uppercases;
108 }
109
110 public static List<String> hyphenatedNames(List<String> names) {
111 List<String> hyphenateds = new ArrayList<>();
112 for (String name : names) {
113 if (name.contains("-")) {
114 hyphenateds.add(name);
115 }
116 }
117 return hyphenateds;
118 }
119
120 public static int totalProductNameLength(List<String> names) {
121 int totalProductLength = 1;
122 for (String name : names) {
123 totalProductLength = totalProductLength * name.length();
124 }
125 return totalProductLength;
126 }
127
128
129 }
130