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 Entry[] table is the one that stores the entire contents of the Map and if it cannot be serialized, how is the Map constructed back during de-serialization
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
HashMap
takes care of its own serialization through the use of thewriteObject
andreadObject
methods.If you look at the source you will see that it does not rely on the default serialization mechanism, but manually writes out all the entries (as an alternating stream of keys and values):
This is more compact than the array, which can contain many empty entries and link chains and overhead for Map$Entry wrappers.
Note that it still invokes
defaultWriteObject
for the "easy" fields. In order for that to work, it has to mark everything else astransient
.HashMaps don't serialize their Entry objects during serialization. Take a look at its
writeObject
method.The javadocs explain:
If you look at the
readObject
method you will see how the Entry table is rebuilt using the size, keys and values.