StreamArrayListProcessing.java
|
package arraylistplay;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class StreamArrayListProcessing {
public static void main(String[] args){
//Point A: Add some strings which represent names to an ArrayList.
List<String> names = new ArrayList<>();
names.add("Holiday, Billie");
names.add("Claudel, Camille");
names.add("Picasso, Pablo");
names.add("Gallen-Kallela, Akseli");
names.add("Zotto, Osvaldo");
names.add("Vanderwall, Grace");
names.add("Mercer, Matthew");
//Point B: use man and the toList Collector to create an ArrayList of just the first names of the names ArrayList.Use a for loop to print out names, separated by spaces
List<String> firstNamesList = names.stream()
.map(n -> firstName(n))
.collect(Collectors.toList());
System.out.print("First names: ");
for (String firstName : firstNamesList){
System.out.print(firstName + " ");
}
System.out.println();
//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. Print it.
String firstNames = names.stream()
.map(n -> firstName(n))
.collect(Collectors.joining(", "));
System.out.println("First names: " + firstNames);
System.out.println("First names: " + firstNames);
//Point D: By analogy from point C, print a comma -separated list of the last names in the names ArrayList.
String lastNames = names.stream()
.map(n -> lastName(n))
.collect(Collectors.joining(","));
System.out.println("LastNames: " + lastNames);
//Point E: Print a comma-separated list of all uppercase first names from the list of names in the names ArrayList
String upperCaseFirstNames = names.stream()
.map(n -> firstName(n))
.map(n -> n.toUpperCase())
.collect(Collectors.joining(", "));
System.out.println("Uppercase first names: " + upperCaseFirstNames);
//Point F: Print a comma-separated list of all hyphenated last names from the list of names in the names ArrayList.
String hyphenateedLastNames = names.stream()
.map(n -> lastName(n))
.filter(n -> n.contains("-"))
.collect(Collectors.joining(", "));
System.out.println("Hypenated Last Names: " + hyphenateedLastNames);
//Point G: Print the integer value of the total length of all first names in the names ArrayList
int totalLength = names.stream()
.map(n -> n.length())
.reduce(0, (n1, n2) -> n1 +n2);
System.out.println("Total length: " + totalLength);
//Point H: Print the integer value of the product of the length of all first names in the names ArrayList.
int totalLengthFirstNames = firstNamesList.stream()
.map(n -> n.length())
.reduce(0,(n1, n2) -> n1+n2);
System.out.println("Total Length of the First Names: " + totalLengthFirstNames);
//Point I: Print the integer value of the product of the length of all first names in the names ArrayList.
int totalProductNames = names.stream()
.map(n -> n.length())
.reduce(0,(n1, n2) -> n1 * n2);
System.out.println("Total Length of the First Names: " + totalProductNames);
}
private static String firstName(String directoryStyleName) {
int i = directoryStyleName.indexOf(",");
return directoryStyleName.substring(i + 2);
}
private static String lastName(String directoryStyleName) {
int i = directoryStyleName.indexOf(",");
return directoryStyleName.substring(0, i);
}
}