Should treeMap.entrySet () return a SortedSet?

Why not treeMap.entrySet () and treeMap. do keySet () methods return a SortedSet ? I can say that this is a mistake. According to the API, Set is defined as not having a specific order. However, the sets returned by TreeMap do have a specific order.

+8
java collections
source share
1 answer

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

+10
source share

All Articles