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