// Both TreeMap and TreeSet are Sorted Collection which means they keep the elements in a // predefined Sorted order. Sorting order can be natural sorting order defined by Comparable // interface or custom sorting Order defined by Comparator interface. // Both TreeMap and TreeSet store their items in a binary search compatible format. import java.util.*; public class Assign3 { public static void main(String args[]) { // Create a hash map TreeMap tm = new TreeMap(); // Put elements to the map tm.put("Pizza", new Integer(1)); tm.put("Chips", new Integer(1)); tm.put("Chips", new Integer(1)); tm.put("Apple", new Integer(1)); tm.put("Beer", new Integer(1)); tm.put("Quesadilla", new Integer(1)); tm.put("Banana", new Integer(1)); // Get a set of the entries Set set = tm.entrySet(); // Get an iterator Iterator i = set.iterator(); // Display elements while(i.hasNext()) { Map.Entry me = (Map.Entry)i.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } System.out.println(); // Change a count Integer theCount = ((Integer)tm.get("Pizza")).intValue(); tm.put("Pizza", new Integer(theCount + 1)); System.out.println("Pizza count is now: " + tm.get("Pizza")); System.out.println("How many chips? " + tm.get("Chips")); System.out.println("Something not in list: " + tm.get("Orange")); } }