In Java library, what is the reason that the Collection
interface doesn't extend Cloneable
and Serializable
interfaces?
在Java库中,Collection接口不扩展Cloneable和Serializable接口的原因是什么?
2 个解决方案
#1
19
Collection
is 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 Collection
implementations like List
allow duplicate elements whereas other implementations like Set
don't. A lot of the Collection
implementations have a public clone
method. However, it does't really make sense to include it in all implementations of Collection
. This is because Collection
is 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 actual implementation; that is, the concrete implementation should decide how it should be cloned or serialized, or even if it 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 all implementations 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的具体实现。例如,像List这样的Collection实现允许重复元素,而像Set这样的其他实现则不允许。许多Collection实现都有一个公共克隆方法。但是,将它包含在Collection的所有实现中并不是真的有意义。这是因为Collection是一个抽象表示。重要的是实施。在处理实际实现时,克隆或序列化的语义和含义发挥作用;也就是说,具体实现应决定如何克隆或序列化,或者即使它可以克隆或序列化。在某些情况下,根据实际的后备实现,克隆和序列化可能没有多大意义。因此,在所有实现中强制克隆和序列化实际上不太灵活且限制性更强。具体实现应该决定是否可以克隆或序列化。
Here's an explanation from 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.
许多Collection实现(包括JDK提供的所有实现)都将具有公共克隆方法,但要求所有集合都是错误的。例如,克隆由TB级SQL数据库支持的Collection意味着什么?方法调用是否会导致公司请求新的磁盘服务器场?类似的论点适用于可序列化。
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的实际类型,它会更灵活,更不容易让客户端决定需要什么类型的Collection,创建一个这种类型的空集合,并使用addAll方法复制原始集合的元素进入新的元素。
#2
8
Because if it did, that would require all Collection
implementations to be Cloneable
and Serializable
, which is more restrictive than needed. Implementations frequently also implement those interfaces, but it's not for the Collection
interface to require it.
因为如果确实如此,那就要求所有Collection实现都是Cloneable和Serializable,这比需要的更严格。实现经常也实现这些接口,但是Collection接口不需要它。
#1
19
Collection
is 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 Collection
implementations like List
allow duplicate elements whereas other implementations like Set
don't. A lot of the Collection
implementations have a public clone
method. However, it does't really make sense to include it in all implementations of Collection
. This is because Collection
is 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 actual implementation; that is, the concrete implementation should decide how it should be cloned or serialized, or even if it 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 all implementations 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的具体实现。例如,像List这样的Collection实现允许重复元素,而像Set这样的其他实现则不允许。许多Collection实现都有一个公共克隆方法。但是,将它包含在Collection的所有实现中并不是真的有意义。这是因为Collection是一个抽象表示。重要的是实施。在处理实际实现时,克隆或序列化的语义和含义发挥作用;也就是说,具体实现应决定如何克隆或序列化,或者即使它可以克隆或序列化。在某些情况下,根据实际的后备实现,克隆和序列化可能没有多大意义。因此,在所有实现中强制克隆和序列化实际上不太灵活且限制性更强。具体实现应该决定是否可以克隆或序列化。
Here's an explanation from 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.
许多Collection实现(包括JDK提供的所有实现)都将具有公共克隆方法,但要求所有集合都是错误的。例如,克隆由TB级SQL数据库支持的Collection意味着什么?方法调用是否会导致公司请求新的磁盘服务器场?类似的论点适用于可序列化。
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的实际类型,它会更灵活,更不容易让客户端决定需要什么类型的Collection,创建一个这种类型的空集合,并使用addAll方法复制原始集合的元素进入新的元素。
#2
8
Because if it did, that would require all Collection
implementations to be Cloneable
and Serializable
, which is more restrictive than needed. Implementations frequently also implement those interfaces, but it's not for the Collection
interface to require it.
因为如果确实如此,那就要求所有Collection实现都是Cloneable和Serializable,这比需要的更严格。实现经常也实现这些接口,但是Collection接口不需要它。