My question is how do I decide what is supposed to be used - Comparator or Comparable?
And that it might throw an Exception.
Does this means Set is always supposed to implement Comparable Interface
If you take a look at the interfaces, and some examples on how they are to be used (correct examples, that is), you will notice that they are not the same thing.
The comparable interface is used by data objects to allow it to be compared to other objects. And the comparator is an external object that compares two objects. They work differently. One rely on the data object to compare itself to others. The other is a comparator object that compares data objects.
The TreeSet collection supports both. You can add comparable objects into the set, and the set will sort them. Or you can assign an external comparator, via a constructor, when the TreeSet is instantiated, and add any object (supported by the comparator) to the set.
Henry