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