COP 3530 Section U03 Fall 2017 Some Set and Some Map Programs ============================== I. The first program forms a hash set. Notice that the entries are not in alphabetical order and no duplications are allowed. We can use the enhanced for loop. import java.util.*; // set sets public class Sets { public static void main(String[] args) { System.out.println("Test Sets"); System.out.println("=========\n\n"); Set set = new HashSet<>(); System.out.println("We form a set with Papa Bill, Markey, " + " Markey, Zoila, Shrek, Zoila."); set.add("Papa Bill"); set.add("Markey"); set.add("Markey"); //ignored, no duplications set.add("Zoila"); set.add("Shrek"); set.add("Zoila"); // ignored, no duplications System.out.println("\nThe set is:"); for (String s: set) System.out.println(s); } } The output: run: Test Sets ========= We form a set with Papa Bill, Markey, Markey, Zoila, Shrek, Zoila. The set is: Shrek Zoila Markey Papa Bill BUILD SUCCESSFUL (total time: 1 second) 2. The second program uses a tree set and is more elaborate. We used the reversed alphabetical order and we used the methods headSet and tailSet. Notice that if we remove an item from the head set, it is removed from the set, so we only get a segment of the set, not a new set. import java.util.*; /** * * @author pelina */ public class Main { public static void main(String[] args) { System.out.println("We form a tree set with the names:"); System.out.println("bob, al, markey, geoff, ali baba, rose, zoila, al."); System.out.println("We use the reverse comparator for ordering the data."); SortedSet s = new TreeSet(Collections.reverseOrder()); s.add("bob"); s.add("al"); s.add("markey"); s.add("geoff"); s.add("ali baba"); s.add("rose"); s.add("zoila"); s.add("al"); // useless add // print the collection System.out.println("The set is:"); for (String val: s) System.out.println(val); // print the smallest and the largest String small = s.first(); String large = s.last(); System.out.println("\nThe smallest is " + small); System.out.println("The largest is " + large); SortedSet pre = s.headSet("geoff"); SortedSet post = s.tailSet("kip"); System.out.println("The set pre, i.e. the items < Geoff are:"); for (String val: pre) System.out.println(val); System.out.println("The items >= kip are:"); for (String val: post) System.out.println(val); System.out.println("\nWe try to remove the markey from pre."); if (s.remove("markey")) System.out.println("markey was removed."); else System.out.println("markey was not removed."); System.out.println("\nNow print pre."); for (String val: pre) System.out.println(val); System.out.println("\nNow print the set."); for (String val: s) System.out.println(val); } } The output: run: We form a tree set with the names: bob, al, markey, geoff, ali baba, rose, zoila, al. We use the reverse comparator for ordering the data. The set is: zoila rose markey geoff bob ali baba al The smallest is zoila The largest is al The set pre, i.e. the items < Geoff are: zoila rose markey The items >= kip are: geoff bob ali baba al We try to remove the markey from pre. markey was removed. Now print pre. zoila rose Now print the set. zoila rose geoff bob ali baba al BUILD SUCCESSFUL (total time: 1 second) 3. The 3rd program shows how to use a map. We used a tree map and a separate method for printing a map. The main method formed a map using the put method and showed how to use the methods get, keySet, and values. Notice that we remove a key from the key set or a value from the value set, the corresponding entry(ies) re removed. package maptest; import java.util.Map; import java.util.TreeMap; import java.util.Set; import java.util.Collection; /** * * @author pelina */ public class Main { // print a map public static void printMap(String msg, Map m) { System.out.println(msg + ":"); Set> entries = m.entrySet(); for ( Map.Entry thisPair :entries) { System.out.print(thisPair.getKey() + "; "); System.out.println(thisPair.getValue()); } } /** * @param args the command line arguments */ public static void main(String[] args) { Map phone1 = new TreeMap(); phone1.put("Mark", "(305)111-1111"); phone1.put("Bill", "(305)222-2222"); phone1.put("Mary", "(305)333-3333"); phone1.put("Carol", "(305)444-4444"); phone1.put("Dan", "(305)555-5555"); phone1.put("Mark", "(305)666-6666"); // print the map System.out.println("The map:"); printMap("phone 1", phone1); // System.out.println("phone1.get(\"Carol\"): " + phone1.get("Carol")); // print the keys System.out.println("\nThe keys are: "); Set keys = phone1.keySet(); // print the collection for (String val: keys) System.out.println(val); System.out.println("\nThe values are: "); Collection values = phone1.values(); // print the collection for (String val: values) System.out.println(val); keys.remove("Susan"); keys.remove("Mary"); values.remove("(305)222-2222"); System.out.println("After Susan, Mary and (305)222-2222 are removed"); System.out.println("\nThe map is: "); printMap("phone 1", phone1); } } The output: run: The map: phone 1: Bill; (305)222-2222 Carol; (305)444-4444 Dan; (305)555-5555 Mark; (305)666-6666 Mary; (305)333-3333 phone1.get("Carol"): (305)444-4444 The keys are: Bill Carol Dan Mark Mary The values are: (305)222-2222 (305)444-4444 (305)555-5555 (305)666-6666 (305)333-3333 After Susan, Mary and (305)222-2222 are removed The map is: phone 1: Carol; (305)444-4444 Dan; (305)555-5555 Mark; (305)666-6666 BUILD SUCCESSFUL (total time: 0 seconds) 4. The 4th program uses a hash map. Again, we use put to create a map, and Map.Entry to print the pairs. We used Map.remove to remove an entry. import java.util.*; // test maps public class TestMaps { public static void main(String[] args) { System.out.println("Maps"); System.out.println("====\n\n"); System.out.println("Form the Husband of map."); Map map = new HashMap<>(); // add some pairs map.put("Jaime","Papa Bill"); map.put("Jill", "Mark"); map.put("Zoila", "Jorge"); map.put("Ana", "Jorge"); map.put("Karen", "Papa Bill"); map.put("Christine", null); System.out.println("\nPrint the map."); Set> set = map.entrySet(); for (Map.Entry pair : set) { String key = pair.getKey(); String value = pair.getValue(); System.out.println("The husband of " + key + " is " + value + "."); } // Now let us say that Christine gets married to Hugo System.out.println("\nChristine gets married to Hugo."); map.remove("Christine"); map.put("Christine", "Hugo"); System.out.println("\nPrint the map."); for (Map.Entry pair : set) { String key = pair.getKey(); String value = pair.getValue(); System.out.println("The husband of " + key + " is " + value + "."); } } } The output: run: Maps ==== Form the Husband of map. Print the map. The husband of Jaime is Papa Bill. The husband of Zoila is Jorge. The husband of Karen is Papa Bill. The husband of Ana is Jorge. The husband of Jill is Mark. The husband of Christine is null. Christine gets married to Hugo. Print the map. The husband of Jaime is Papa Bill. The husband of Zoila is Jorge. The husband of Karen is Papa Bill. The husband of Ana is Jorge. The husband of Jill is Mark. The husband of Christine is Hugo. BUILD SUCCESSFUL (total time: 0 seconds)