//Torbert, 2.17.2004 import java.util.*; public class ActingSchool { public static void main(String[] args) { TreeMap sGrades = new TreeMap(); sGrades.put("Jack Nicholson", "A-"); sGrades.put("Humprhey Bogart", "A+"); sGrades.put("Audrey Hepburn", "A"); sGrades.put("Meryl Streep", "A-"); sGrades.put("Jimmy Stewart", "A"); //display initial data Iterator it = sGrades.keySet().iterator(); while(it.hasNext()) { Object x = it.next(); Object y = sGrades.get(x); System.out.println(x + " (" + y + ")"); } //reverse the map TreeMap gStudents = new TreeMap(); it = sGrades.keySet().iterator(); while(it.hasNext()) { Object x = it.next(); Object y = sGrades.get(x); if(!gStudents.containsKey(y)) { ArrayList al = new ArrayList(); al.add(x); gStudents.put(y, al); } else ((ArrayList)gStudents.get(y)).add(x); } //display the reverse it = gStudents.keySet().iterator(); while(it.hasNext()) { Object x = it.next(); Object y = gStudents.get(x); System.out.println(x + ": " + y); } } }