java 为什么 Collection 不扩展 Cloneable 和 Serializable?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10464026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:09:17  来源:igfitidea点击:

Why doesn't Collection extend Cloneable and Serializable?

javacollections

提问by Raj

In Java library, what is the reason that the Collectioninterface doesn't extend Cloneableand Serializableinterfaces?

在Java库中,Collection接口没有扩展CloneableSerializable接口的原因是什么?

回答by Vivin Paliath

Collectionis an interface that specifies a group of objects known as elements. The details of how the group of elements is maintained is left up to the concrete implementations of Collection. For example, some Collectionimplementations like Listallow duplicate elements whereas other implementations like Setdon't. A lot of the Collectionimplementations have a public clonemethod. However, it does't really make sense to include it in all implementations of Collection. This is because Collectionis an abstract representation. What matters is the implementation. The semantics and the implications of either cloning or serializing come into play when dealing with the actualimplementation; that is, the concrete implementation should decide how it should be cloned or serialized, or even ifit can be cloned or serialized. In some cases, depending on what the actual backing-implementation is, cloning and serialization may not make much sense. So mandating cloning and serialization in allimplementations is actually less flexible and more restrictive. The specific implementation should make the decision as to whether it can be cloned or serialized.

Collection是一个接口,它指定了一组称为元素的对象。如何维护元素组的细节由 的具体实现决定Collection。例如,一些Collection实现List允许重复元素,而其他实现Set则不允许。许多Collection实现都有一个公共clone方法。但是,将它包含在Collection. 这是因为Collection是抽象表示。重要的是实施。在处理实际情况时,克隆或序列化的语义和含义开始发挥作用。执行; 也就是说,具体的实现应该决定它应该如何克隆或序列化,甚至是否可以克隆或序列化。在某些情况下,根据实际的支持实现是什么,克隆和序列化可能没有多大意义。因此,在所有实现中强制克隆和序列化实际上更不灵活且更具限制性。具体的实现应该决定是否可以克隆或序列化。

Here's an explanationfrom Oracle's documentation:

以下是Oracle 文档中的解释

Many Collection implementations (including all of the ones provided by the JDK) will have a public clone method, but it would be mistake to require it of all Collections. For example, what does it mean to clone a Collection that's backed by a terabyte SQL database? Should the method call cause the company to requisition a new disk farm? Similar arguments hold for serializable.

If the client doesn't know the actual type of a Collection, it's much more flexible and less error prone to have the client decide what type of Collection is desired, create an empty Collection of this type, and use the addAll method to copy the elements of the original collection into the new one.

许多 Collection 实现(包括 JDK 提供的所有实现)都有一个 public clone 方法,但是要求所有 Collections 都使用它是错误的。例如,克隆一个由 TB 级 SQL 数据库支持的 Collection 意味着什么?方法调用是否会导致公司申请新的磁盘群?类似的论据适用于可序列化。

如果客户端不知道 Collection 的实际类型,让客户端决定需要什么类型的 Collection,创建一个这种类型的空 Collection,并使用 addAll 方法复制将原始集合的元素转换为新的集合。

回答by T.J. Crowder

Because if it did, that would require all Collectionimplementations to be Cloneableand Serializable, which is more restrictive than needed. Implementationsfrequently also implement those interfaces, but it's not for the Collectioninterface to require it.

因为如果是这样,那将要求所有Collection实现都是Cloneableand Serializable,这比需要的限制更多。实现也经常实现这些接口,但Collection接口不需要它。