为什么 java.util.List 不实现 Serialized?

发布于 2024-10-16 02:14:46 字数 577 浏览 2 评论 0原文

为什么java.util.List没有实现Serializable,而LinkedListArraylist等子类却实现了?这似乎不违背继承原则吗?例如,如果我们想通过网络发送一个 Linkedlist,我们必须这样写:

new ObjectOutputStream(some inputStream).writeObject(some LinkedList);

到目前为止一切都很好,但是在读取另一端的对象时我们必须明确地说 LinkedList l = (LinkedList)objectInputStream.readObject( ); 而不是 List l = (List)objectInputStream.readObject();。如果我们要将写入功能从 LinkedList 更改为 ArrayList,我们还必须更改读取部分。让 List 实现 Serialized 就可以解决这个问题。

Why is it that java.util.List does not implement Serializable while subclasses like LinkedList, Arraylist do? Does not it seem to be against inheritance principles? For example if we want to send a Linkedlist over a network, we have to write:

new ObjectOutputStream(some inputStream).writeObject(some LinkedList);

So far so good, but while reading the object on the other side we have to explicity say LinkedList l = (LinkedList)objectInputStream.readObject(); instead of List l = (List)objectInputStream.readObject();. If we were ever to change the writing functionality from LinkedList to say ArrayList, we will also have to change the reading part. Having List implement Serializable would have solved the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

作妖 2024-10-23 02:14:46

List 未实现 Serializable,因为它不是列表的关键要求。不保证(或需要)List 的每个可能实现都可以序列化。

LinkedListArrayList 选择这样做,但这特定于它们的实现。其他List实现可能不是Serialized

List does not implement Serializable because is it not a key requirement for a list. There is no guarantee (or need) that every possible implementation of a List can be serialized.

LinkedList and ArrayList choose to do so, but that is specific to their implementation. Other List implementations may not be Serializable.

踏雪无痕 2024-10-23 02:14:46

List 是一个接口,使其扩展 Serialized 意味着 List 的任何实现都应该是可序列化的。

可序列化属性不是 List 抽象的一部分,因此不应是实现所必需的。

List is an interface and making it extend Serializable would mean that any implementation of List should be serializable.

The serializable property is not part of the List abstraction and should therefore not be required for an implementation.

浅笑依然 2024-10-23 02:14:46

不。LinkedList 始终是一个 List。当你反序列化链表时,由于 LinkedList 是一个 List,所以你可以写

List l = (List) objectInputStream.readObject();

L in fact a LinkedList 这一事实并不重要。你想要一个列表,你得到了一个列表。

No. A LinkedList is always a List. When you deserialize the linked list, since a LinkedList is a List, you may write

List l = (List) objectInputStream.readObject();

The fact that l is in fact a LinkedList is not important. You wanted a List, and you got a List.

左耳近心 2024-10-23 02:14:46

因为 List 也可以由用户特定的子类实现,并且实现者可能不一定希望实现 Serialized。可串行性也不属于List的关键职责,因此没有理由将两者链接在一起。

Because List is open to be implemented by user specific subclasses as well, and implementors may not necessarily want to implement Serializable. Serializability does not belong to the key responsibilities of a List either, so there is no reason to link the two together.

清君侧 2024-10-23 02:14:46

考虑假设的 ThreadList 实现 List,其中包含任何给定时间点的活动线程列表。该实现透明地浏览活动线程并允许轻松访问它们 - 为了您的方便。这样的实现应该是可序列化的(忘记Thread 是不可序列化的)吗?

由实现接口的人来决定她的实现是否可以安全地序列化。 List 太通用了,因为基本上说明了 *T` 类型的项目的有序集合。

Consider hypothetical ThreadList implements List<Thread>, containing the list of active threads at any given point in time. The implementation transparently browses active threads and allows easy access to them - for your convenience. Should such an implementation be serializable (forgetting that Thread is not serializable)?

It is up to the person implementing the interface to decide, whether her implementation is safe to be serialized. List is too generic, as basically stating *ordered collection of items of type T`.

风苍溪 2024-10-23 02:14:46

你的问题似乎是基于误解。要序列化对象,该对象(或其类)必须实现 Serializable,但不需要使用 Serialized 类型(或某些子类型)的表达式来做这个。 writeObject 方法的参数类型为 Object 而不是 Serialized,并且返回类型为 readObject,这是有意为之的()

但即使这些参数和返回类型是可序列化的,您也不需要知道具体的实现类型:

ObjectOutputStream stream = ...;
List myList = ...;
stream.writeObject((Serializable)myList);

并且

ObjectInputStream stream = ...;
List myList = (List) stream.readObject();

将像现在一样工作(无需可序列化转换)。

ObjectInputStream 和 ObjectOutputStream 在调用时根本不关心您的类型,它们只是查看手头的对象及其类。

Your question seems to be based on a misunderstanding. To serialize an object, the object (or its class) has to implement Serializable, but you don't need to use an expression of type Serializable (or some subtype) to do this. It is quite intentional that the writeObject method has a parameter type of Object and not Serializable, as well as the return type of readObject().

But even if these parameter and return types were Serializable, you would not need to know the specific implementation types:

ObjectOutputStream stream = ...;
List myList = ...;
stream.writeObject((Serializable)myList);

and

ObjectInputStream stream = ...;
List myList = (List) stream.readObject();

would work as well as it works now (without the Serializable cast).

ObjectInputStream and ObjectOutputStream do not care at all about your types when invoking, they simply look at the object at hand and its class.

弃爱 2024-10-23 02:14:46

如果 List 实现/扩展了 Serialized,那么您就隐含了这样的约定:List 的所有实现类/子类也是可序列化的,但这并不总是正确的。例如,查看 ForwardingListMultimap 的 guava-collections 实现。它在功能上不需要是可序列化的,而这只是因为 List 不可序列化而成为可能。

If List implements/extends Serializable then you have implied the contract that all implementation classes/subclasses of List are also Serializable which is not always true. For e.g look at the guava-collections implementation of ForwardingListMultimap. It doesn't need to be Serializable functionally and this was possible only because List is not Serializable.

陪我终i 2024-10-23 02:14:46

为什么java.util.List没有实现Serialized...

因为并不是世界上每个 List 实现都必须是 Serializable

在阅读另一边的对象时,我们必须明确地说......
而不是

List l = (List) objectInputStream.readObject();

你尝试过吗?如果你这样做,我想你会发现它有效。

Why is it that java.util.List does not implement Serializable ...

Because not every List implementation in the world must be Serializable.

while reading the object on the other side we have to explicity say ...
instead of

List l = (List) objectInputStream.readObject();

Have you tried it? If you do, I think you'll find that it works.

池木 2024-10-23 02:14:46

List 扩展了 Collection,它不能实现任何东西,因为它是一个接口......

List extends Collection, and it can't implement anything because it's an interface...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文