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