HashMap Serializability

后端 未结 3 793
误落风尘
误落风尘 2021-02-02 07:47

HashMap implements the Serializable interface; so it can be serialized. I have looked at the implementation of HashMap and the Entry[] table is marked as transient. Since the En

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-02 08:42

    HashMaps don't serialize their Entry objects during serialization. Take a look at its writeObject method.

    The javadocs explain:

    The capacity of the HashMap (the length of the bucket array) is emitted (int), followed by the size (an int, the number of key-value mappings), followed by the key (Object) and value (Object) for each key-value mapping. The key-value mappings are emitted in no particular order.

    If you look at the readObject method you will see how the Entry table is rebuilt using the size, keys and values.

提交回复
热议问题