StreamArrayListProcessing.java
|
1 package arraylistplay;
2
3 import java.util.ArrayList;
4 import java.util.List;
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("Mac, Toby");
17
18
19 //POINT B: Use map and toList collector to create an arraylist of just the first names of the names arraylist. use a for loop to print out the names, use a for loop to print out names separated by spaces.
20 List<String> firstNamesList = names.stream()
21 .map(n-> firstName(n))
22 .collect(Collectors.toList());
23 System.out.print("First names: ");
24 for (String firstName : firstNamesList){
25 System.out.print(firstName + " ");
26 }
27 System.out.println();
28 //POINT C: Use Strings join function to create and print a string of just the first names of the names arraylist with each name separated with a comma.
29 String firstNames = names.stream()
30 .map(n -> firstName(n))
31 .collect(Collectors.joining(", "));
32
33 System.out.println("First names: " + firstNames);
34
35
36 //POINT D: By anology from points B and C, print a comma-separated list of the last names in the names arraylist.
37 String lastNames = names.stream()
38 .map(n -> lastName(n))
39 .collect(Collectors.joining(", "));
40
41 System.out.println("Last names: " + lastNames);
42
43 //POINT E: Print a comma-separated list of all uppercase first names from the list of names in the arraylist
44 String upperCaseFirstNames = names.stream()
45 .map(n -> firstName(n))
46 .map(n -> n.toUpperCase())
47 .collect(Collectors.joining(", "));
48 System.out.println("Uppercase first names: " + upperCaseFirstNames);
49
50
51 //POINT F: Print a comma-separated list of all hyphenated last names from the list of names in the arraylist
52 String hyphenatedLastNames = names.stream()
53 .map(n -> lastName(n))
54 .filter(n -> n.contains("-"))
55 .collect(Collectors.joining(", "));
56 System.out.println("Hyphenated last names: " + hyphenatedLastNames);
57
58 //POINT G: Print the integer value of the total length of all names in arraylist
59 int totalLength = names.stream()
60 .map(n -> n.length())
61 .reduce(0, (n1, n2) -> n1 + n2);
62 System.out.println("Total length: " + totalLength);
63
64
65 //POINT H: Print the integer value of the total length of all first names in arraylist
66 int totalLengthfirst = names.stream()
67 .map(n -> firstName(n))
68 .map(n -> n.length())
69 .reduce(0, (n1, n2) -> n1 + n2);
70 System.out.println("Total length: " + totalLengthfirst);
71
72 //POINT I: Print the integer value of the product of the total length of all first names in arraylist
73 int totalproduct = names.stream()
74 .map(n -> n.length())
75 .reduce(1, (n1, n2) -> n1 * n2);
76 System.out.println("Total length: " + totalproduct);
77 }
78 private static String firstName(String directoryStyleName) {
79 return directoryStyleName.substring(directoryStyleName.indexOf(","));
80
81 }
82
83 private static String lastName(String directoryStyleName) {
84 return directoryStyleName.substring(0,directoryStyleName.indexOf(","));
85 }
86 }