简体   繁体   English

Java:为什么Set <E> 提到Collection中的所有方法 <E>

[英]Java: Why does Set<E> mention all methods in Collection<E>

http://download.oracle.com/javase/tutorial/collections/interfaces/set.html http://download.oracle.com/javase/tutorial/collections/interfaces/set.html

Why does Set interface list all the methods in Collection? 为什么Set接口列出了Collection中的所有方法? Aren't these methods automatically inherited by the child interface? 这些方法不是由子接口自动继承的吗?

它列出了所有这些,因为文档是不同的,即使声明本身是相同的。

That link is a tutorial , not the API docs. 该链接是一个教程 ,而不是API文档。 Try this link: 试试这个链接:

http://download.oracle.com/javase/6/docs/api/java/util/Set.html http://download.oracle.com/javase/6/docs/api/java/util/Set.html

Set works different then Collection - in Set you can have no duplicate entries. 设置工作不同然后收集 - 在集合中您可以没有重复的条目。 If you would just copy methods from Collection you would implement Set wrong... 如果您只是从Collection中复制方法,那么您将实现Set wrong ...

Like Kathy wrote - documentation is different (because logic/use is different) 像Kathy写的那样 - 文档是不同的(因为逻辑/使用是不同的)

Collection .add(): 集合 .add():

Ensures that this collection contains the specified element (optional operation). 确保此集合包含指定的元素(可选操作)。 Returns true if this collection changed as a result of the call. 如果此集合因调用而更改,则返回true。 (Returns false if this collection does not permit duplicates and already contains the specified element.) Collections that support this operation may place limitations on what elements may be added to this collection. (如果此集合不允许重复并且已包含指定元素,则返回false。)支持此操作的集合可能会对可能添加到此集合的元素设置限制。 In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. 特别是,某些集合将拒绝添加null元素,而其他集合将对可能添加的元素类型施加限制。 Collection classes should clearly specify in their documentation any restrictions on what elements may be added. 集合类应在其文档中明确指定对可添加的元素的任何限制。

If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false). 如果一个集合因为已经包含该元素的原因而拒绝添加特定元素,那么它必须抛出异常(而不是返回false)。 This preserves the invariant that a collection always contains the specified element after this call returns. 这保留了在此调用返回后集合始终包含指定元素的不变量。

Set .add(): 设置 .add():

Adds the specified element to this set if it is not already present (optional operation). 如果指定的元素尚不存在,则将其添加到此集合(可选操作)。 More formally, adds the specified element e to this set if the set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). 更正式地,如果集合不包含元素e2,则将指定的元素e添加到该集合中(e == null?e2 == null:e.equals(e2))。 If this set already contains the element, the call leaves the set unchanged and returns false. 如果此set已包含该元素,则调用将保持set不变并返回false。 In combination with the restriction on constructors, this ensures that sets never contain duplicate elements. 结合对构造函数的限制,这可以确保集合永远不会包含重复元素。 The stipulation above does not imply that sets must accept all elements; 上述规定并不意味着集合必须接受所有要素; sets may refuse to add any particular element, including null, and throw an exception, as described in the specification for Collection.add. sets可以拒绝添加任何特定元素,包括null,并抛出异常,如Collection.add规范中所述。 Individual set implementations should clearly document any restrictions on the elements that they may contain. 单个集实现应明确记录对它们可能包含的元素的任何限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM