开发者

Java Set Stipulation

Java docs says the following about Set interface, can someone please help me understand how these additional stipulations are forced? I mean you cannot throw an extra exception that is not thrown by super interface (in this case Collection).

The Set interface places additional stipulations, beyond thos开发者_JS百科e inherited from the Collection interface, on the contracts of all constructors and on the contracts of the add, equals and hashCode methods.

Thanks

-Abidi


These are contractual stipulations.

It means that all Sets are expected to obey these rules.
They aren't enforced.


The stipulations are not forced, they are just added to the contract.

Luckily the contracts of the methods in Collection (and Object) are loose enough that an implementation can implement both the contract from Collection, Object and Set without violating any of these.

For example, the add method does not throw Exceptions when trying to add an object which is already in it, it just returns false (instead of true). This is already allowed by Collection.add:

Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜