Why is the hash table of HashMap marked as transient although the class is serializable

前端 未结 2 548
悲&欢浪女
悲&欢浪女 2020-12-15 04:46

I was looking at the source of HashMap.

A HashMap implements Serializable.

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

2条回答
  •  自闭症患者
    2020-12-15 05:11

    The transient keyword indicates that a field shouldn't be included in the serialized representation of a class. The Entry[] table of a HashMap is simply an acceleration structure - it allows for fast lookup of the stored entries. The entire table itself doesn't need to be serialized, just the entries it contains, since the table can be rebuilt again when deserializing from the list of entries.

提交回复
热议问题