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

一个人想着一个人 提交于 2019-12-02 03:53:13

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/28060736/why-java-6-overrides-keyset-entryset-and-values-interface-in-sortedmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!