aspose file tools*
The moose likes Java in General and the fly likes Serialization of a Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Serialization of a Collection" Watch "Serialization of a Collection" New topic
Author

Serialization of a Collection

Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
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

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Oct 12, 2000
Posts: 124
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

Joined: Jan 30, 2000
Posts: 18671
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialization of a Collection
 
Similar Threads
Passing beans back from the jsp
Pass ArrayCollection from Flex to Java
volatile Vs synchronized
Serializing an ArrayList
load balancing & session management