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