java transient Entry[] table

来源:百度知道 编辑:UC知道 时间:2024/05/22 17:54:38
hashmap 的成员变量transient Entry[] table 为什么经过transient 修饰还是能够被序列化?

private void writeObject(java.io.ObjectOutputStream s)
throws IOException
{
Iterator<Map.Entry<K,V>> i =
(size > 0) ? entrySet0().iterator() : null;

// Write out the threshold, loadfactor, and any hidden stuff
s.defaultWriteObject();

// Write out number of buckets
s.writeInt(table.length);

// Write out size (number of Mappings)
s.writeInt(size);

// Write out keys and values (alternating)
if (i != null) {
while (i.hasNext()) {
Map.Entry<K,V> e = i.next();
s.writeObject(e.getKey());
s.writeObject(e.getValue());
}
}
}

这是HashMap的序列化方法,可以看到,序列化的时候手动将这个哈希表种的数据导出了
而在反序列化的时候,则是将手动导出的代码逐条插回去,也就是重建了transient Entry[] table 的内容。
private void readObject(java.io.ObjectInputStream s)
throws IOException, ClassNotFoundException
{
// Read in the threshol