1 package arraylists; 2 import java.util.ArrayList; 3 import java.util.List; 4 5 public class ArrayListProcessing { 6 public static void main(String[] args){ 7 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 16 17 18 // POINT B: Create an ArrayList of just the first names of the names 19 // ArrayList. Use a for loop to print out the names, separated 20 // by spaces. 21 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 30 // just the first names of the names ArrayList with each 31 // name separated by a comma. 32 33 String firstNames = String.join(",", firstNamesList); 34 35 System.out.println("First names: " + firstNames); 36 37 38 39 //Point D: By analogy from points B and C, print a comma-Separated list 40 // of the last names in the names ArrayList. 41 List<String> lastNamesList = lastNames(names); 42 String lastNames= String.join(",", lastNamesList); 43 System.out.println("Last names:"+ lastNames); 44 45 // Point E: Print a comma-separated list of all uppercase first names 46 // from the list of names in the names ArrayList. 47 List<String> upperCaseFirstNamesList= upperCaseNames(firstNames(names)); 48 String upperCaseFirstNames= String.join(",", upperCaseFirstNamesList); 49 50 System.out.println("UpperCase first names:"+ upperCaseFirstNames); 51 52 // Point F: Print a comma-separated list of all uppercase first names 53 // from the list of names in the names ArrayList 54 List<String>hypenatedNames= hypenatedNames(lastNamesList); 55 String hypenatedLastNames= String.join("-", hypenatedNames); 56 57 System.out.println("hyphenated last names:"+ hypenatedLastNames); 58 59 // Point G: Print the integer value of the total length of all names in the names ArrayList. 60 int totalLength= totalNameLength(names); 61 System.out.println("Total Length:"+ totalLength); 62 63 //Point H: Print the integer value of the total length of all first names in the names ArrayList. 64 int totalFirstNameLength= totalNameLength(firstNamesList); 65 System.out.println("Total Length of First names:"+ totalFirstNameLength); 66 67 //Point I: Print the integer value of the product 68 // of the length of all first names in the names ArrayList. 69 int totalproduct= productionOfNameLength(firstNamesList); 70 System.out.println("Product of the length of all first names:"+ totalproduct); 71 72 } 73 74 private static String firstName(String directoryStyleName) { 75 int commaPosition = directoryStyleName.indexOf(","); 76 String firstName = directoryStyleName.substring(commaPosition + 2); 77 78 return firstName; 79 } 80 private static List<String> firstNames(List<String> names) { 81 List<String> firsts = new ArrayList<>(); 82 for (String name : names) { 83 firsts.add(firstName(name)); 84 } 85 return firsts; 86 } 87 private static String lastName(String directoryStyleName) { 88 int commaPosition= directoryStyleName.indexOf(","); 89 String lastName= directoryStyleName.substring(0,commaPosition); 90 91 return lastName; 92 } 93 private static List<String>lastNames(List<String>names) { 94 List<String> lasts = new ArrayList<>(); 95 for (String name : names) { 96 lasts.add(lastName(name)); 97 } 98 return lasts; 99 } 100 public static List<String>upperCaseNames(List<String>names) { 101 List<String> uppercases = new ArrayList<>(); 102 for (String name : names) { 103 uppercases.add(name.toUpperCase()); 104 } 105 return uppercases; 106 } 107 public static List<String>hypenatedNames(List<String>names){ 108 List<String>hypenateds= new ArrayList<>(); 109 for (String name: names){ 110 if (name.contains("-")){ 111 hypenateds.add(name); 112 } 113 } 114 return hypenateds; 115 } 116 //Point G: total length of the names 117 public static int totalNameLength(List<String>names){ 118 int totalLength= 0; 119 for (String name: names){ 120 totalLength= totalLength+ name.length(); 121 } 122 return totalLength; 123 } 124 125 //Point I: product of the length of the first names 126 public static int productionOfNameLength(List<String>firstnames){ 127 int product= 1; 128 for (String name: firstnames){ 129 product= product*name.length(); 130 } 131 return product; 132 } 133 134 }