1 /* 2 * A program to perform some basic operations on a list of String names. 3 */ 4 5 package arraylists; 6 7 import java.util.ArrayList; 8 import java.util.List; 9 10 public class ArrayListProcessing { 11 12 public static void main(String[] args) { 13 // POINT A: Add some strings which represent names to an ArrayList. 14 List<String> names = new ArrayList<>(); 15 names.add("Holiday, Billie"); 16 names.add("Claudel, Camille"); 17 names.add("Picasso, Pablo"); 18 names.add("Gallen-Kallela, Akseli"); 19 names.add("Zotto, Osvaldo"); 20 names.add("Camacho III, Franklin"); 21 22 // POINT B: Create an ArrayList of just the first names of the names ArrayList. 23 // Use a for loop to print out the names, separated by spaces. 24 List<String> firstNamesList = firstNames(names); 25 26 System.out.print("First names: "); 27 for (String firstName : firstNamesList) { 28 System.out.print(firstName + " "); 29 } 30 System.out.println(); 31 32 // POINT C: Use Strings join function to create and print a String of just the 33 // first names of the names ArrayList with each name separated by a comma. 34 String firstNames = String.join(", ", firstNamesList); 35 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 39 // last names in the names ArrayList. 40 List<String> lastNamesList = lastNames(names); 41 42 System.out.print("Last names: "); 43 for (String lastName : lastNamesList) { 44 System.out.print(lastName + " "); 45 } 46 System.out.println(); 47 48 String lastNames = String.join(", ", lastNamesList); 49 50 System.out.println("Last names: " + lastNames); 51 52 // POINT E: Print a comma-separated list of all uppercase first names from the 53 // list of names in the names ArrayList. 54 List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names)); 55 String upperCaseFirstNames = String.join(", ", upperCaseFirstNamesList); 56 System.out.println("Uppercase first names: " + upperCaseFirstNames); 57 58 // POINT F: Print a comma-separated list of all the hyphenated last names from 59 // from the list of names in the names ArrayList. 60 List<String> hyphenatedLastNamesList = hyphenatedNames(lastNames(names)); 61 String hyphenatedLastNames = String.join(", ", hyphenatedLastNamesList); 62 63 System.out.println("Hyphenated last names: " + hyphenatedLastNames); 64 65 // POINT G: Print the integer value of the total length of all names in the 66 // names in the names ArrayList. 67 int totalLength = totalNameLength(names); 68 69 System.out.println("Total length: " + totalLength); 70 71 // POINT H: Print the integer value of the total length of all first names in 72 // the names ArrayList. 73 int totalLengthOfFirstNames = totalNameLength(firstNames(names)); 74 75 System.out.println("Total length of first names: " + totalLengthOfFirstNames); 76 77 // POINT I: Print the integer value of the product of the length of all first 78 // names in the names ArrayList. 79 int totalProductOfFirstNames = totalNameProduct(firstNames(names)); 80 81 System.out.println("Total product: " + totalProductOfFirstNames); 82 } 83 84 private static String firstName(String directoryStyleName) { 85 int dsnCommaPosition = directoryStyleName.indexOf(","); 86 String firstName = directoryStyleName.substring(dsnCommaPosition + 2); 87 return firstName; 88 } 89 90 private static List<String> firstNames(List<String> names) { 91 List<String> firsts = new ArrayList<>(); 92 for (String name :names) { 93 firsts.add(firstName(name)); 94 } 95 return firsts; 96 } 97 98 private static String lastName(String directoryStyleName) { 99 100 int dsnCommaPosition = directoryStyleName.indexOf(","); 101 String lastName = directoryStyleName.substring(0, dsnCommaPosition); 102 return lastName; 103 } 104 105 private static List<String> lastNames(List<String> names) { 106 List<String> lasts = new ArrayList<>(); 107 for (String name :names) { 108 lasts.add(lastName(name)); 109 } 110 return lasts; 111 } 112 113 public static List<String> upperCaseNames(List<String> names) { 114 List<String> uppercases = new ArrayList<>(); 115 for (String name : names) { 116 uppercases.add(name.toUpperCase()); 117 } 118 return uppercases; 119 } 120 121 public static List<String> hyphenatedNames(List<String> names) { 122 List<String> hyphenateds = new ArrayList<>(); 123 for (String name : names) { 124 if (name.contains("-")) { 125 hyphenateds.add(name); 126 } 127 } 128 return hyphenateds; 129 } 130 131 public static int totalNameLength(List<String> names) { 132 int totalLength = 0; 133 for (String name : names) { 134 totalLength = totalLength + name.length(); 135 } 136 return totalLength; 137 } 138 139 public static int totalNameProduct(List<String> names) { 140 int totalProduct = 1; 141 for (String name : names) { 142 totalProduct = totalProduct * name.length(); 143 } 144 return totalProduct; 145 } 146 }