1 package arraylistplay; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class ArrayListProcessing { 7 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("Lennon, John"); 17 names.add("Alighieri, Dante"); 18 19 20 //POINT B: Create an ArrayList of just the first names of the names ArrayList. 21 // Use a for loop to print out the names, 22 // separated by spaces. 23 List<String> firstNamesList = firstNames(names); 24 25 System.out.print("First names: "); 26 for (String firstName : firstNamesList) { 27 System.out.print(firstName + " "); 28 } 29 System.out.println(); 30 31 32 //POINT C: Use String's join function to create and print a String of 33 //just the first names of the names 34 // ArrayList with each name separated by a comma. 35 String firstNames = String.join(",", firstNamesList); 36 System.out.println("First names: " + firstNames); 37 38 //Point D: By analogy from Points B and C, print a comma-separated list of the last names 39 //in the names ArrayList 40 List<String> lastNamesList = lastNames(names); 41 System.out.print("Last Names: "); 42 for (String lastName : lastNamesList) { 43 System.out.print(lastName + " "); 44 } 45 System.out.println(); 46 String lastNames = String.join(",", lastNamesList); 47 System.out.println("Last names: " + lastNames); 48 49 //POINT E:Print a comma-separated list of all uppercase first names from the list of names in the names ArrayList 50 List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names)); 51 String upperCaseFirstNames = String.join(",", upperCaseFirstNamesList); 52 System.out.println("UpperCase First Names: " + upperCaseFirstNames); 53 54 55 //POINT F: Print a comma-separated list of all hyphenated last names from the list of names in the 56 // names ArrayList 57 List<String> hyphenatedNamesList = hyphenatedNames(names); 58 String hyphenatedNames = String.join(",", hyphenatedNamesList); 59 System.out.println(("Hyphenated names: " + hyphenatedNames)); 60 61 //POINT G: Print the integer value of the total length of all names in the names ArrayList 62 int totalLength = totalNameLength(names); 63 System.out.println("Total length: " + totalLength); 64 65 //POINT H: Print the integer value of the total length 66 //of all first names in the names ArrayList 67 int totalFirstNamesLength = firstNameLength(firstNamesList); 68 System.out.println(("total first names length: " + totalFirstNamesLength)); 69 70 //POINT I: Print the integer value of the product of the 71 // length of all names in the names ArrayList 72 int totalLengthProduct = nameProduct(names); 73 System.out.println("the product of the length of all names in the ArrayList: "+totalLengthProduct); 74 } 75 76 private static String firstName(String directoryStyleName) { 77 78 79 return (directoryStyleName.substring(directoryStyleName.lastIndexOf(" "))); 80 } 81 82 private static List<String> firstNames(List<String> names) { 83 List<String> firsts = new ArrayList<>(); 84 for (String name : names) { 85 firsts.add(firstName(name)); 86 } 87 return firsts; 88 } 89 90 private static String lastName(String directoryStyleName) { 91 92 return (directoryStyleName.substring(0, directoryStyleName.indexOf(","))); 93 } 94 95 private static List<String> lastNames(List<String> names) { 96 List<String> lasts = new ArrayList<>(); 97 for (String name : names) { 98 lasts.add(lastName(name)); 99 } 100 return lasts; 101 } 102 103 public static List<String> upperCaseNames(List<String> names) { 104 List<String> uppercases = new ArrayList<>(); 105 for (String name : names) { 106 uppercases.add(name.toUpperCase()); 107 } 108 return uppercases; 109 } 110 111 public static List<String> hyphenatedNames(List<String> names) { 112 List<String> hyphenateds = new ArrayList<>(); 113 for (String name : names) { 114 if (name.contains("-")) { 115 hyphenateds.add(name); 116 } 117 } 118 return hyphenateds; 119 } 120 121 public static int totalNameLength(List<String> names) { 122 int totalLength = 0; 123 for (String name : names) { 124 totalLength = totalLength + name.length(); 125 } 126 return totalLength; 127 } 128 129 public static int firstNameLength(List<String> firstNames) { 130 int totalFirstNameLength = 0; 131 for (String firstName : firstNames) { 132 totalFirstNameLength = totalFirstNameLength + firstName.length(); 133 } 134 return totalFirstNameLength; 135 } 136 137 public static int nameProduct(List<String> names) { 138 int totalLength = 1; 139 for (String name : names) { 140 totalLength = totalLength * name.length() ; 141 } 142 return totalLength; 143 } 144 } 145 146 147 148 149 150 151 152 153 154