StreamArrayListProcessing.java
|
1 /*
2 A program to preform some basic operations on a list of strings
3 using java Streams
4 */
5
6 package arraylists;
7
8 import java.awt.*;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.stream.Collector;
12 import java.util.stream.Collectors;
13
14 public class StreamArrayListProcessing {
15
16 public static void main(String[] args) {
17 //point a: add some strings which represent names to an arraylist
18 List<String> names = new ArrayList<>();
19 names.add("Holiday, Billie");
20 names.add("Claudel, Camille");
21 names.add("Picasso, Pablo");
22 names.add("Gallen-Kallela, Akseli");
23 names.add("Zotto, Osvaldo");
24 names.add("Lovell, Owen");
25 names.add("Gomez, Selena");
26 names.add("Eilish, Billie");
27
28 //point b: use map and the toList collector to create an ArrayList of just
29 // the first names of the names ArrayList. use a for loop to print
30 // out the names, seperated by spaaces.
31
32 List<String> firstNamesList = names.stream()
33 .map(n -> firstName(n))
34 .collect(Collectors.toList());
35
36 System.out.print("First Names: ");
37 for (String firstName : firstNamesList){
38 System.out.print(firstName + " ");
39 }
40 System.out.println();
41
42 //poijnt c: use map and joining collector to create a string of just
43 // the first names of the names Array List with each name seperated by a]
44 // comma. print it
45 String firstNames = names.stream()
46 .map(n -> firstName(n))
47 .collect(Collectors.joining(", "));
48
49 System.out.println("First names: " + firstNames);
50
51 //point d: by analogy from point c print a comma seperated list
52 // of the last names in the ArrayList
53 String lastNames = names.stream()
54 .map(n -> lastName(n))
55 .collect(Collectors.joining(", "));
56
57 System.out.println("Last Names: " + lastNames);
58
59 //point E: priont a comma-seperated list of all the uppercasre last names fomr the list of names in the n
60 //names arraylist
61
62 String upperCaseFirstNames = names.stream()
63 .map( n -> firstName(n))
64 .map(n -> n.toUpperCase())
65 .collect(Collectors.joining(", "));
66
67 System.out.println("Uppercase first names: " + upperCaseFirstNames);
68
69 //point f: print a comma-seperated list of all the hyphenated last names fomr the list of names in the n
70 // //names arraylist
71
72 String hyphenatedLastNames = names.stream()
73 .map(n -> lastName(n))
74 .filter(n -> n.contains("-"))
75 .collect(Collectors.joining(", "));
76
77 System.out.println("Hyphenated last names: " + hyphenatedLastNames);
78
79 //point g: priont the integer value of the total length of all names in the array list
80 int totalLength = names.stream()
81 .map(n -> n.length())
82 .reduce(0, (n1, n2) -> n1 + n2);
83
84 System.out.println("Total length: " + totalLength);
85
86
87 //point h: priont the integer value of the total length of all first
88 // names in the array list
89 int firstLength = names.stream()
90 .map(n -> firstName(n))
91 .map(n -> n.length())
92 .reduce(0, (n1,n2) -> n1 +n2);
93 System.out.println("First name total length: " + firstLength);
94
95 //point i: priont the integer value of the product of length
96 //all first names in arraylist
97 int totalProduct = names.stream()
98 .map(n -> firstName(n))
99 .map(n -> n.length())
100 .reduce(1, (n1,n2) -> n1 * n2);
101
102 System.out.println("Product of lengths: " + totalProduct);
103
104
105 }
106 private static String firstName(String directoryStyleName) {
107 int positionOfComma = directoryStyleName.indexOf(",");
108 String firstName = directoryStyleName.substring(positionOfComma + 2);
109 return firstName;
110 }
111 private static String lastName(String directoryStyleName) {
112 int positionOfComma = directoryStyleName.indexOf(",");
113 String lastName = directoryStyleName.substring(0,positionOfComma);
114 return lastName;
115 }
116
117 }
118