StreamArrayListProcessing.java
|
1 package arraylist;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.stream.Collectors;
6
7 public class StreamArrayListProcessing {
8 public static void main(String[] args){
9 // POINT A: Add some strings which represent names to an ArrayList.
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("Chen, Jakie");
17 names.add("Khan, Genghis");
18 // POINT B: Use map and the toList collector to create an ArrayList of just
19 //the first names of the names ArrayList. Use a for loop to print
20 //out the names, separated by spaces.
21 List<String> firstNamesList = names.stream()
22 .map(n -> firstName(n))
23 .collect(Collectors.toList());
24 System.out.print("First names: ");
25 for (String firstName : firstNamesList){
26 System.out.print(firstName + " ");
27 }
28 System.out.println();
29
30 // POINT C: Use map and the joining collector to create a String of just
31 //the first names of the names ArrayList with each name separated
32 //by a comma. Print it.
33 String firstNames = names.stream()
34 .map(n -> firstName(n))
35 .collect(Collectors.joining(", "));
36 System.out.println(("First names: " + firstNames));
37 // POINT D: By analogy from point C, print a comma-separated list of the
38 //last names in the names ArrayList.
39 String lastnames = names.stream()
40 .map(n -> lastName(n))
41 .collect(Collectors.joining(", "));
42 System.out.println("last Name: " + lastnames);
43
44 // POINT E: Print a comma-separated list of all uppercase first names
45 //from the list of names in the names ArrayList.
46 String upperCaseFirstNames = names.stream()
47 .map(n -> firstName(n))
48 .map(n -> n.toUpperCase())
49 .collect(Collectors.joining(", "));
50 System.out.println("Uppercase first names: " + upperCaseFirstNames);
51
52 // POINT F: Print a comma-separated list of all hyphenated last names
53 //from the list of names in the names ArrayList.
54 String hyphenatedLastNames = names.stream()
55 .map(n -> lastName(n))
56 .filter(n -> n.contains("-"))
57 .collect(Collectors.joining(", "));
58 System.out.println("Hypenated last name " + hyphenatedLastNames);
59
60 // POINT G: Print the integer value of the total length of all names
61 //in the names ArrayList.
62
63 int totalLength = names.stream()
64 .map(n -> n.length())
65 .reduce(0, (n1, n2) -> n1 + n2);
66
67 System.out.println("Total length: " + totalLength);
68 // POINT H: Print the integer value of the total length of all
69 //first names in the names ArrayList.
70 int firstNameLength = names.stream()
71 .map(n -> firstName(n).length())
72 .reduce(0, (n1, n2) -> n1 + n2);
73
74 System.out.println("Total First length: " + firstNameLength);
75
76 // POINT I: Print the integer value of the product of the length of
77 //all first names in the names ArrayList.
78 int ProductNameLength = names.stream()
79 .map(n -> firstName(n).length())
80 .reduce(1, (n1, n2) -> n1 * n2);
81
82 System.out.println(" Product Total length: " + ProductNameLength);
83 }
84 private static String firstName(String directoryStyleName) {
85 int commaPosition = directoryStyleName.indexOf(",");
86 String firstName = directoryStyleName.substring(commaPosition + 1);
87
88
89 return firstName;
90 }
91 private static String lastName(String directoryStyleName) {
92 int commaPosition = directoryStyleName.indexOf(",");
93 String lastName = directoryStyleName.substring(0,commaPosition);
94 return lastName;
95 }
96 }
97