为什么HashMap的哈希表被标记为临时的,尽管该类是可序列化的 [英] Why is the hash table of HashMap marked as transient although the class is serializable

查看:191
本文介绍了为什么HashMap的哈希表被标记为临时的,尽管该类是可序列化的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查看 HashMap 的来源。

A HashMap implements Serializable

确定这是为了可以作为一个对象进行peristed /传输。

Ok this is so that it can be peristed/transmitted as an object.

但我看到散列表本身标记为 transient

But I see that the hashtable itself is marked as transient.

我没有得到这个。如果你将它标记为暂时的,这不是意味着它应该序列化?

I don't get this.If you mark it as transient, doesn't this mean that it should not be serialized?

但所有的数据都在表中。为什么 transient

But all the data are in the table.So why is it transient?

也许我对 Serializable 如何工作感到困惑?

Perhaps I am confused on how Serializable works?

推荐答案

HashMap 使用 writeObject readObject 来实现自定义序列化,而不是让它的字段正常序列化。它将桶的数量,总大小和每个条目写入流中,并在反序列化时从这些字段重建自身。正如tzaman所说,表本身在序列形式中是不必要的,所以它不是序列化以节省空间。

HashMap uses writeObject and readObject to implement custom serialization rather than just letting its field be serialized normally. It writes the number of buckets, the total size and each of the entries to the stream and rebuilds itself from those fields when deserialized. As tzaman says, the table itself is unnecessary in the serial form, so it's not serialized to save space.

你可以阅读更多关于这些方法和其他方法在 writeReplace 和 readResolve javase / 6 / docs / api / java / io / Serializable.html> Serializable javadoc。

You can read more about those methods and some other methods of doing custom serialization (writeReplace and readResolve) in the Serializable javadoc.

这篇关于为什么HashMap的哈希表被标记为临时的,尽管该类是可序列化的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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