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