Search code examples
javadictionarysortedmapkeysetentryset

Why Java 6 overrides keySet(), entrySet() and values() interface in SortedMap


Java 5 http://docs.oracle.com/javase/1.5.0/docs/api/java/util/SortedMap.html

Java 6 https://docs.oracle.com/javase/6/docs/api/java/util/SortedMap.html

As you can see that since Java 6, these three apis are overridden. Can anyone tell me what's the purpose of making such a change?


Solution

  • The methods had to be overridden in order to have their own Javadoc.

    Other reasons why you would declare a method in a subinterface are the ability to restrict the return type or to add annotations, but they didn't do that in this case so that was not the reason.

    The Javadoc is part of the contract of the interface. In Java 6, Sun/Oracle felt the need to clarify the behaviour of these methods on a SortedMap, which is further restricted from the behaviour that they have in Map.

    For example, in SortedMap, the Javadoc of keySet says:

    The set's iterator returns the keys in ascending order.

    On Map, the same method doesn't have this description as in general, Maps are allowed to return keysets in any order that they like; SortedMap restricts itself further.