关于jdk1.8容器hashmap中的变量table加了transient修饰后如何序列化。Serializable

关于jdk1.8容器hashmap中的变量table加了transient修饰后如何序列化。Serializable

2018年03月12日 11:13:24 qq_35516657 阅读数:462

    众所周知,transient修饰符的作用是使该变量在序列化的时候不会被储存。

transient Node<K,V>[] table;

    但是hashmap中的变量table是储存了容器中所有的元素,在序列化中不被储存,那么反序列化后hashmap对象中岂不是个空容器?

    后来通过细想,table里存的只是引用,就算在序列化中储存到硬盘里,反序列化后table变量里的引用已经没有意义了。

    至于hashmap是如何在序列化中储存元素呢?原来是它通过重写Serializable接口中的writeObject方法和readObject方法实现的。下面贴出两个方法的源代码。

 
  1. private void writeObject(java.io.ObjectOutputStream s)

  2. throws IOException {

  3. int buckets = capacity();

  4. // Write out the threshold, loadfactor, and any hidden stuff

  5. s.defaultWriteObject();

  6. s.writeInt(buckets);

  7. s.writeInt(size);

  8. internalWriteEntries(s);

  9. }

  10.  
  11. /**

  12. * Reconstitute the {@code HashMap} instance from a stream (i.e.,

  13. * deserialize it).

  14. */

  15. private void readObject(java.io.ObjectInputStream s)

  16. throws IOException, ClassNotFoundException {

  17. // Read in the threshold (ignored), loadfactor, and any hidden stuff

  18. s.defaultReadObject();

  19. reinitialize();

  20. if (loadFactor <= 0 || Float.isNaN(loadFactor))

  21. throw new InvalidObjectException("Illegal load factor: " +

  22. loadFactor);

  23. s.readInt(); // Read and ignore number of buckets

  24. int mappings = s.readInt(); // Read number of mappings (size)

  25. if (mappings < 0)

  26. throw new InvalidObjectException("Illegal mappings count: " +

  27. mappings);

  28. else if (mappings > 0) { // (if zero, use defaults)

  29. // Size the table using given load factor only if within

  30. // range of 0.25...4.0

  31. float lf = Math.min(Math.max(0.25f, loadFactor), 4.0f);

  32. float fc = (float)mappings / lf + 1.0f;

  33. int cap = ((fc < DEFAULT_INITIAL_CAPACITY) ?

  34. DEFAULT_INITIAL_CAPACITY :

  35. (fc >= MAXIMUM_CAPACITY) ?

  36. MAXIMUM_CAPACITY :

  37. tableSizeFor((int)fc));

  38. float ft = (float)cap * lf;

  39. threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ?

  40. (int)ft : Integer.MAX_VALUE);

  41.  
  42. // Check Map.Entry[].class since it's the nearest public type to

  43. // what we're actually creating.

  44. SharedSecrets.getJavaOISAccess().checkArray(s, Map.Entry[].class, cap);

  45. @SuppressWarnings({"rawtypes","unchecked"})

  46. Node<K,V>[] tab = (Node<K,V>[])new Node[cap];

  47. table = tab;

  48.  
  49. // Read the keys and values, and put the mappings in the HashMap

  50. for (int i = 0; i < mappings; i++) {

  51. @SuppressWarnings("unchecked")

  52. K key = (K) s.readObject();

  53. @SuppressWarnings("unchecked")

  54. V value = (V) s.readObject();

  55. putVal(hash(key), key, value, false, false);

  56. }

  57. }

  58. }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值