Java Spring 2006 - HW4 FAQ

Click on a question to expand it (for the details).
Click on the question title again to hide the details.

+ SortedMap

Question:

For the JavaGrades class, we need to be able to sort using either the key (Student name), or the value (grade). We can use a SortedMap (like TreeMap), but this will only provide an ordering by name - how do we sort the map by value?


Answer:

You can't expect a Map to do both of these for you, so you need to do this yourself. Recall that you can sort any collection using one of the static methods in java.util.Collections. You can also provide your own Comparator for use when sorting. Finally, you can ask a Map for a Set of all the key values in the map. So - you could get a collection (set) of the key values, turn this into a List, and sort the List using a custom Comparator (which needs to be able to lookup the grade of any student given it's name, something you have already written!).