1 package arraylists; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class ArrayListProcessing { 7 public static void main (String[] args) { 8 //POINT A: ADD SOME STRINGS WHICH REPRESENT NAMES TO AN ARRAYLIST 9 List<String> names = new ArrayList<>(); 10 names.add("Holiday, Billie"); 11 names.add("Claudel, Camille"); 12 names.add("Picasso, Pablo"); 13 names.add("Gallen-Kallela, Akseli"); 14 names.add("Zotto, Osvaldo"); 15 names.add("Bailey, Jordan"); 16 names.add("Reeves, Keanu"); 17 names.add("Smith, Will"); 18 19 // Point B: Create an ArrayList of just the first names of the names ArrayList. 20 //Use a for loop to print out the names, separated by spaces. 21 List<String> firstNamesList = firstNames(names); 22 23 System.out.print("First names: "); 24 for (String firstName : firstNamesList) { 25 System.out.print(firstName + " "); 26 } 27 System.out.println(); 28 29 // Point C: Use String's join function to create and print a String of just the first names of the names 30 // 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 from points B and C, print a comma-separated list of the last names 35 // in the names ArrayList. 36 List<String> lastNamesList = lastNames(names); 37 38 System.out.print("Last names: "); 39 for (String lastName : lastNamesList) { 40 System.out.print(lastName + " "); 41 } 42 System.out.println(); 43 44 String lastNames = String.join(", ", lastNamesList); 45 46 System.out.println("Last names: " + lastNames); 47 48 // Point E: Print a comma-separated list of all uppercase first names from the list of names 49 // in the names ArrayList. 50 List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names)); 51 String upperCaseFirstNames = String.join(", ", upperCaseFirstNamesList); 52 53 System.out.println("Uppercase first names: " + upperCaseFirstNames); 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 = upperCaseNames(hyphenatedNames(names)); 58 String hyphenatedNames = String.join(", ", hyphenatedNamesList); 59 60 System.out.println("Hyphenated names: " + hyphenatedNames); 61 62 // Point G: Print the integer value of the total length of all names in the names ArrayList. 63 int totalLength = totalNameLength(names); 64 65 System.out.println("Total length: " + totalLength); 66 67 // Point H: Print the integer value of the total length of all first names in the names ArrayList. 68 69 int firstlength = 0; 70 for (String name : firstNamesList) { 71 firstlength = firstlength + name.length(); 72 } 73 74 System.out.println("Total first name length: " + firstlength); 75 // Point I: Print the integer value of the product of the length of all the first names 76 // in the names ArrayList. 77 78 int product = 1; 79 for (String name : firstNamesList) { 80 product = name.length()*product; 81 82 } 83 System.out.println("Product of first names: " + product); 84 } 85 86 private static List<String> firstNames(List<String> names) { 87 List<String> firsts = new ArrayList<>(); 88 for (String name : names) { 89 firsts.add(firstName(name)); 90 } 91 return firsts; 92 } 93 94 private static String firstName(String directoryStyleName) { 95 int commaPos = directoryStyleName.indexOf(","); 96 return directoryStyleName.substring(commaPos + 2); 97 } 98 99 private static List<String> lastNames(List<String> names) { 100 List<String> lasts = new ArrayList<>(); 101 for (String name : names) { 102 lasts.add(lastName(name)); 103 } 104 return lasts; 105 } 106 107 private static String lastName(String directoryStyleName) { 108 int commaPos = directoryStyleName.indexOf(","); 109 return directoryStyleName.substring(0, commaPos); 110 } 111 112 public static List<String> upperCaseNames(List<String> names) { 113 List<String> uppercases = new ArrayList<>(); 114 for (String name : names) { 115 uppercases.add(name.toUpperCase()); 116 } 117 return uppercases; 118 } 119 120 public static List<String> hyphenatedNames(List<String> names) { 121 List<String> hyphenateds = new ArrayList<>(); 122 for (String name : names) { 123 if (name.contains("-")) { 124 hyphenateds.add(name); 125 } 126 } 127 return hyphenateds; 128 } 129 130 public static int totalNameLength(List<String> names) { 131 int totalLength = 0; 132 133 for (String name : names) { 134 totalLength = totalLength + name.length(); 135 } 136 return totalLength; 137 138 } 139 }