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