Monday, February 12, 2007

Completed Java collections

Hello everyone,

We completed Java collections yesterday. The section was summed up with a dojo to experiment with using Maps and implementing the Comparable Interface for the class that we used as a 'key' in the TreeMap.

The following students participated in the dojo:
  • Jiten
  • Parul
  • Vibhor
  • Saurabh Rai
After completing the dojo, I gave a little take home exercise. But first a little background. We made the Student class implement Comparable because it was to be used as a key in a TreeMap. However, we may not always have the ability to implement an interface, especially in situations when we use classes from 3rd party libraries. But if we want to use objects of these classes as keys in a TreeMap, we have to give the Map some way of comparing a newly inserted key with existing keys (because the keys in a TreeMap are always sorted by invoking the compareTo method or with a Comparator). In such cases, we give the Map a Comparator.

For the exercise, remove the compareTo() method (and the Comparable Interface) from the student class, and give the TreeMap a Comparator object.
(this exercise is for practice and will NOT be graded)

No comments: