java关键字map_Java中HashMap关键字transient的疑惑

补充一下,其实你看下HashMap中的size值也被transient修饰,不过writeObject方法确实将该对象进行了序列化,其实transient只是用于标志java.io.ObjectOutputStream.java 进行defaultWriteObject时的判定;

/**

* Save the state of the HashMap instance to a stream (i.e.,

* serialize it).

*

* @serialData 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.

*/

private void writeObject(java.io.ObjectOutputStream s)

throws IOException

{

Iterator> 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 e = i.next();

s.writeObject(e.getKey());

s.writeObject(e.getValue());

}

}

}

/**

* Write the non-static and non-transient fields of the current class to

* this stream. This may only be called from the writeObject method of the

* class being serialized. It will throw the NotActiveException if it is

* called otherwise.

*

* @throws IOException if I/O errors occur while writing to the underlying

* OutputStream

*/

public void defaultWriteObject() throws IOException {

if ( curContext == null ) {

throw new NotActiveException("not in call to writeObject");

}

Object curObj = curContext.getObj();

ObjectStreamClass curDesc = curContext.getDesc();

bout.setBlockDataMode(false);

defaultWriteFields(curObj, curDesc);

bout.setBlockDataMode(true);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值