Java:设置接口和集合接口的差异 [英] Java: Set interface and Collection interface differences

查看:146
本文介绍了Java:设置接口和集合接口的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是查找了设置接口,发现它大多(或完全)只重新声明已经在集合接口。 设置本身延伸集合,因此不意味着 Set 界面自动具有 Collection 的所有功能?

I just looked up the Set interface and found that it mostly (or completely) only redeclares functions which are already in the Collection interface. Set itself extends Collection, so doesn't that mean that the Set interface automatically has all the functions from Collection? So why are they redeclared then?

例如,设置重新声明:

/**
 * Returns the number of elements in this set (its cardinality).  If this
 * set contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
 * <tt>Integer.MAX_VALUE</tt>.
 *
 * @return the number of elements in this set (its cardinality)
 */
int size();

/**
 * Returns <tt>true</tt> if this set contains no elements.
 *
 * @return <tt>true</tt> if this set contains no elements
 */
boolean isEmpty();

Collection



And the declaration in Collection:

/**
 * Returns the number of elements in this collection.  If this collection
 * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
 * <tt>Integer.MAX_VALUE</tt>.
 *
 * @return the number of elements in this collection
 */
int size();

/**
 * Returns <tt>true</tt> if this collection contains no elements.
 *
 * @return <tt>true</tt> if this collection contains no elements
 */
boolean isEmpty();

这对我来说似乎是多余的。为什么不仅仅定义 Set 接口为:

This seems very redundant to me. Why not just define the Set interface as:

public interface Set<E> extends Collection<E> {}

我认为这些接口之间没有单一的区别,对吧?

I think there is no single difference between those interfaces, right?

当然,我不是在询问 Set 的不同语义/含义。我知道。我只是问它是否技术上(即对编译器)有什么区别。也就是说,一般来说:

Of course I am not asking about the different semantics / meaning of Set. I know that. I am just asking about if it technically (i.e. to the compiler) has any difference. I.e., speaking generally:

interface A { void foo(); }
interface B extends A { void foo(); }
interface C extends A {}

现在, A B C

虽然合同(即文档中说的内容)对于某些函数可能有所不同(如 add ),有一个有效的原因来重新声明它们:能够放置一个新的文档,即定义新的合同。

While the contract (i.e. what is said in the documentation) can really be different for some functions (as for add), there is a valid reason to redeclare them: To be able to put a new documentation, i.e. to define the new contract.

还有具有完全相同的文档/合同的函数(如 isEmpty )。为什么还要重新声明?

However, there are also functions (like isEmpty) which have exactly the same documentation / contract. Why are they also redeclared?

推荐答案

技术上来说,编译器没有什么区别。

Technically for the compiler it makes no difference at all.

但是,集合不能有重复的条目,而集合可以。这是值得了解的。

However, a set cannot have duplicate entries whereas a Collection can. This is worth knowing about.

因此,参数,返回值和发生的方法语义可能意味着不同的事情。重新声明也允许javadoc变得更具体。例如对于add():

Because of this, the methods semantics for parameters, return values and what happens can mean different things. Redeclaring also allows the javadoc to become more specific. For example for add():

设置:@return true如果此集合不包含指定的元素

Set: @return true if this set did not already contain the specified element

集合:@return true如果此集合因调用而更改

Collection: @return true if this collection changed as a result of the call

集合的含义更具体。

即使对于不是更具体的方法,它也使得javadoc更好。例如,对于size():返回此集合中的元素数(其基数)。

Even for methods that are not more specific, it enables the javadoc to be nicer. For example, for size() : "Returns the number of elements in this set (its cardinality)." which is closer to the language people used to mathematical sets will understand.

API文档总结了以下内容:Set接口添加了额外的规定,除了那些继承自Collection接口,对所有构造函数的合约以及add,equals和hashCode方法的合约。为了方便起见,这里还包括其他继承方法的声明(这些声明所附的规范已经适合于Set接口,但不包含任何附加规定。)

The API documents summarise this by saying: "The Set interface places additional stipulations, beyond those inherited from the Collection interface, on the contracts of all constructors and on the contracts of the add, equals and hashCode methods. Declarations for other inherited methods are also included here for convenience. (The specifications accompanying these declarations have been tailored to the Set interface, but they do not contain any additional stipulations.)"

这篇关于Java:设置接口和集合接口的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆