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