StreamArrayListProcessing.java
|
1 package arraylists;
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("Bailey, Jordan");
17 names.add("Reeves, Keanu");
18 names.add("Smith, Will");
19 //POINT B: USE MAP AND THE TOLIST COLLECTOR TO CREATE AN ARRAYLIST OF JUST THE FIRST NAMES OF THE NAMES ARRAYLIST. USE A FOR LOOP TO PRINT 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 System.out.print(firstName + " ");
26 }
27 System.out.println();
28 //POINT C: USE MAP AND THE JOINING COLLECTOR TO CREATE A STRING OF JUST THE FIRST NAMES OF THE NAMES ARRAYLIST WITH EACH NAME SEPARATED BY A COMMA.
29 String firstNames = names.stream()
30 .map(n -> firstName(n))
31 .collect(Collectors.joining(", "));
32 System.out.println("First Names: " + firstNames);
33 //POINT D: BY ANALOGY FROM POINT C, PRINT A COMMA SEPARATED LIST OF THE LAST NAMES IN THE NAMES ARRAYLIST.
34 String lastNames = names.stream()
35 .map(n ->lastName(n))
36 .collect(Collectors.joining(", "));
37 System.out.println("Last Names: " + lastNames);
38 //POINT E: PRINT A COMMA SEPARATED LIST OF ALL UPPERCASE FIRST NAMES FROM THE LIST OF NAMES IN THE NAMES ARRAYLIST.
39 String uppercaseFirstNames = names.stream()
40 .map(n -> firstName(n))
41 .map(n -> n.toUpperCase())
42 .collect(Collectors.joining(", "));
43 System.out.println("Upper Case First Names: " + uppercaseFirstNames);
44 //POINT F: PRINT A COMMA SEPARATED LIST OF ALL THE HYPHENATED LAST NAMES FROM THE LIST OF NAMES IN THE NAMES ARRAYLIST.
45 String hyphenatedLastNames = names.stream()
46 .map(n -> lastName(n))
47 .filter(n -> n.contains("-"))
48 .collect(Collectors.joining(", "));
49 System.out.println("Hyphenated Last Names: " + hyphenatedLastNames);
50 //POINT G: PRINT THE INTEGER VALUE OF THE TOTAL LENGTH OF ALL NAMES IN THE NAMES ARRAYLIST
51 int totalLength = names.stream()
52 .map(n -> n.length())
53 .reduce(0, (n1,n2) -> n1 + n2);
54 System.out.println("Total Length: " + totalLength);
55 //POINT H: PRINT THE INTEGER VALUE OF THE TOTAL LENGTH OF ALL FIRST NAMES IN THE NAMES ARRAYLIST
56 int firstNamesLength = names.stream()
57 .map(n -> firstName(n))
58 .map(n -> n.length())
59 .reduce(0, (n1,n2) -> n1 + n2);
60 System.out.println("First Names Length: " + firstNamesLength);
61 //POINT I: PRINT THE INTEGER VALUE OF THE PRODUCT OF THE LENGTH OF ALL FIRST NAMES IN THE NAMES ARRAYLIST.
62 int firstNameProduct = names.stream()
63 .map(n -> firstName(n))
64 .map(n -> n.length())
65 .reduce(1, (n1,n2) -> n1*n2);
66 System.out.println("First Names Product: " + firstNameProduct);
67 }
68
69 private static String firstName(String directoryStyleName) {
70 int positionOfComma = directoryStyleName.indexOf(",");
71 String First = directoryStyleName.substring(positionOfComma + 2);
72 return First;
73 }
74
75 private static String lastName(String directoryStyleName) {
76 int commaPos = directoryStyleName.indexOf(",");
77 return directoryStyleName.substring(0, commaPos);
78 }
79
80 public static List<String> upperCaseNames(List<String> names) {
81 List<String> uppercases = new ArrayList<>();
82 for (String name : names) {
83 uppercases.add(name.toUpperCase());
84 }
85 return uppercases;
86 }
87
88 public static List<String> hyphenatedNames(List<String> names) {
89 List<String> hyphenateds = new ArrayList<>();
90 for (String name : names) {
91 if (name.contains("-")) {
92 hyphenateds.add(name);
93 }
94 }
95 return hyphenateds;
96 }
97 }
98