red shoe
red shoe

Reputation: 217

Shouldn't treeMap.entrySet() return a SortedSet?

Why don't the treeMap.entrySet() and treeMap.keySet() methods return SortedSet? I might go so far as saying that is a mistake. As per the API, a Set is defined as not having a particular ordering. However, the sets returned by TreeMap do have a particular ordering.

Upvotes: 8

Views: 1474

Answers (1)

Paul Boddington
Paul Boddington

Reputation: 37645

I think if they wrote the interface SortedMap now, both keySet and entrySet would return SortedSet. However, the SortedMap interface was introduced in Java 1.2 before covariant return types were allowed. They cannot change this now as there will be implementations of SortedMap out there for which keySet and entrySet returns a Set that is not a SortedSet.

Upvotes: 11

Related Questions