So, the TreeMap class in java is of the form TreeMap<K,V>. Obviously K needs to be a Combarable, but that is only checked at runtime with a cast, and if it not a Comparable an exception is thrown. Would it not have made more sense to define this class as TreeMap<K extends Comparable<? super K>, V>?
What am I missing here?
An instance of
TreeMapcan be given a comparator for the keys, so they do not have to have a natural total ordering.[EDIT]
More specifically, an instance of
TreeMapcan be created by providing the constructor with an instance ofComparatorthat is capable of comparing two keys for order. If you create a map in such a way, the comparator will be used for all key comparisons. In that case, the keys wouldn’t have to be inherently comparable.