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 10 // POINT A: ADD SOME STRINGS WHICH REPRESENT NAMES TO AN ARRAYLIST 11 List<String> names = new ArrayList<>(); 12 names.add("Holiday, Billie"); 13 names.add("Claudel, Camille"); 14 names.add("Picasso, Pablo"); 15 names.add("Gallen-Kallela, Akseli"); 16 names.add("Zotto, Osvaldo"); 17 names.add("Jeter, Derek"); 18 names.add("Thompson, Nathan"); 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, SEPARATED BY SPACES. 22 List<String> firstNamesList = firstNames(names); 23 System.out.println("First Names: "); 24 for (String firstName : firstNamesList) { 25 System.out.println(firstName + " "); 26 } 27 System.out.println(); 28 29 // POINT C: USE STRING'S JOIN FUNCTION TO CREATE AND PRINT A STRING OF JUST THE 30 // FIRST NAMES OF THE NAMES ARRAYLIST WITH EACH NAME SEPARATED BY A COMMA. 31 String firstNames = String.join(",", firstNamesList); 32 System.out.println("First Names: " + firstNames); 33 34 // POINT D: BY ANALOGY OF POINTS B AND C, PRINT A COMMA-SEPARATED LIST OF THE 35 // LAST NAMES IN THE NAMES ARRAYLIST 36 List<String> lastNamesList = lastNames(names); 37 String lastNames = String.join(",", lastNamesList); 38 System.out.println("Last Names: " + lastNames); 39 40 // POINT E: PRINT A COMMA-SEPARATED LIST OF ALL UPPERCASE FIRST NAMES FROM THE 41 // LIST OF NAMES IN THE NAMES ARRAYLIST 42 List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names)); 43 String upperCaseFirstNames = String.join(",", upperCaseFirstNamesList); 44 System.out.println("Uppercase First Names: " + upperCaseFirstNames); 45 46 // POINT F: PRINT A COMMA-SEPARATED LIST OF ALL HYPHENATED LAST NAMES FROM THE 47 // LIST OF NAMES IN THE NAMES ARRAYLIST 48 List<String> hyphenatedLastNamesList = hyphenatedNames(lastNamesList); 49 String hyphenatedLastNames = String.join("-", hyphenatedLastNamesList); 50 System.out.println("Hyphenated Last Names: " + hyphenatedLastNames); 51 52 // POINT G: PRINT THE INTEGER VALUE OF THE TOTAL LENGTH OF ALL THE NAMES 53 // IN THE NAMES ARRAYLIST 54 int totalLength = totalNameLength(names); 55 System.out.println("Total Length: " + totalLength); 56 57 // POINT H: PRINT THE INTEGER VALUE OF THE TOTAL LENGTH OF ALL FIRST NAMES IN 58 // THE NAME ARRAYLIST 59 int totalFirstNamesLength = totalNameLength(firstNamesList); 60 System.out.println("Total Length of First Names: " + totalFirstNamesLength); 61 62 // POINT I: PRINT THE INTEGER VALUE OF THE PRODUCT OF THE LENGTH OF ALL FIRST 63 // NAMES IN THE NAMES ARRAYLIST 64 int totalProduct = productOfNameLength(firstNamesList); 65 System.out.println("Product of the Length of All First Names: " + totalProduct); 66 } 67 private static String firstName(String directoryStyleName) { 68 int commaPosition = directoryStyleName.indexOf(","); 69 String firstName = directoryStyleName.substring(commaPosition+ 2); 70 71 return firstName; 72 } 73 74 private static List<String> firstNames(List<String> names) { 75 List<String> firsts = new ArrayList<>(); 76 for ( String name : names ) { 77 firsts.add(firstName(name)); 78 } 79 return firsts; 80 } 81 82 private static String lastName(String directoryStyleName) { 83 int commaPosition = directoryStyleName.indexOf(","); 84 String lastName = directoryStyleName.substring(0,commaPosition); 85 return lastName; 86 } 87 88 private static List<String> lastNames(List<String> names) { 89 List<String> lasts = new ArrayList<>(); 90 for ( String name : names ) { 91 lasts.add(lastName(name)); 92 } 93 return lasts; 94 } 95 96 public static List<String> upperCaseNames(List<String> names) { 97 List<String> uppercases = new ArrayList<>(); 98 for ( String name : names ) { 99 uppercases.add(name.toUpperCase()); 100 } 101 return uppercases; 102 } 103 104 private static List<String> hyphenatedNames(List<String> names) { 105 List<String> hyphenates = new ArrayList<>(); 106 for ( String name : names ) { 107 if ( name.contains("-")) { 108 hyphenates.add(name); 109 } 110 } 111 return hyphenates; 112 } 113 114 public static int totalNameLength(List<String> names) { 115 int totalLength = 0; 116 for ( String name : names ) { 117 totalLength = totalLength + name.length(); 118 } 119 return totalLength; 120 } 121 122 public static int productOfNameLength(List<String> firstNames) { 123 int product = 1; 124 for ( String name : firstNames ) { 125 product = product*name.length(); 126 } 127 return product; 128 } 129 }