Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 396
red shoe Avatar asked Mar 18 '15 18:03

red shoe


1 Answers

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.

like image 177
Paul Boddington Avatar answered Oct 22 '22 23:10

Paul Boddington