Why Java 6 overrides keySet(), entrySet() and valu

2019-02-27 16:59发布

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条回答
仙女界的扛把子
2楼-- · 2019-02-27 17:38

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.

查看更多
登录 后发表回答