Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization of a Collection

 
Bhupinder Dhillon
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just noticed that only Vector serializes it's data elements. So, if someone wanted to serialize ArrayList and it's data then you would need to extend it to serialize the data elements. Is there a reason as to why SUN implemented the Collections like this?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not true. All the standard implementations of collections implement Serializable - ArrayList, LinkedList, HashSet, TreeSet, HashMap, TreeMap, and assorted others. The interfaces in the collections framework do not require an implementation to implement Serializable - but most do so anyway.
[ March 04, 2003: Message edited by: Jim Yingst ]
 
Bhupinder Dhillon
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
All the standard implementations of collections implement Serializable - ArrayList, LinkedList, HashSet, TreeSet, HashMap, TreeMap, and asssorted others.

But if you look at the "Serialized Form" of any of these classes in javadoc, you'll see that none of them serialize the data elements. Most of them only serialize the size of the collection - which is pretty much useless without the data elements.
[ March 04, 2003: Message edited by: Bhupinder Dhillon ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. I don't know how this "Serialized Form page is generated offhand, but it's apparently either misleading or completely in error. It's easy to see that all the elements of an ArrayList are serialized, by either writing a test, or looking at the source code and examining the writeObject(ObjectOutputStream) method.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking further at ArrayList, it appears that the elementData array is marked transient. Not because the data in it shouldn't be serialized, but because they don't want the default implementation of writeObject() (invoked in this class by s.defaultwriteObject()) to handle it. Instead the elements in the array are serialized explicitly by the writeObject() method. So it appears that the Serialized Form page is generated by looking at what fields are non-transient and thus would be serialized by the defaultWriteObject() - but the javadoc tool isn't smart enough to figure out what other things are being serilized explicitly by the writeObject() method. So the "Serialized Form" page is of very limited use.
 
Bhupinder Dhillon
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see they have made it transient because it's useless writing out empty elements in the object[], but I wonder why they simply didn't call trimToSize() before serializing it? That way the object[] wouldn't have needed to be transient.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think trimToSize() is just unnecessary extra work. It takes time to create a new array object of the correct size and copy all pertinent data into the new array. Instead the writeObject() method doesn't even bother writing the array object itself - it just writes the contents of the array. One less object to serialize, thus further speeding the process along. The array can be easily recreated upon deserialization, once the array size is read. Then the individual elements are read back in. So the writeObject() implementation will be slightly faster than the default implementation would have been, and will generate slightly smaller files.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic