StreamArrayListProcessing.java
/* 
 * A program to perform some basic operations on 
 * a list of Strings using Java streams. 
 */

package arraylists;

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, Kaseli");
        names.add("Zotto, Osvaldo");

        // 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.
        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 an 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);

        // 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("Last names: " + 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 hyphenatedLastNames = names.stream()
                .map(n -> lastName(n))
                .filter(n -> n.contains("-"))
                .collect(Collectors.joining(", "));

        System.out.println("Hyphenated Last Names: " + hyphenatedLastNames);

        // POINT G: Print the integer value of the total length of all 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 total length of all
        //          first names in the names ArrayList.
        int totalLength2 = names.stream()
                .map(n -> n.length())
                .reduce(0,(n1, n2) -> (n2 + n1) - n1);

        System.out.println("Length of first names: " + totalLength2);

        // POINT I: Print the integer value of the product of the length of
        //          all first names in the names ArrayList.
        int producttotalLength = names.stream()
                .map(n -> n.length())
                .reduce(0,(n1, n2) -> (n2 + n1) * n2);

        System.out.println("Product of the lengths: " + producttotalLength);
    }
    private static String lastName(String directoryStyleName) {
        directoryStyleName = directoryStyleName.substring(0,directoryStyleName.indexOf(","));
        return directoryStyleName;
    }
    private static String firstName(String directoryStyleName) {
        directoryStyleName = directoryStyleName.substring(directoryStyleName.indexOf(",")+2);
        return directoryStyleName;
    }
}