Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize a linked list implemented in java?

I read online that serialization can be omitted for derived objects by declaring them as transient. But, in case of linked list the links are memory references between objects. So, should I convert it to array and store the array representation?

like image 929
Boolean Avatar asked Apr 19 '26 03:04

Boolean


1 Answers

Here's how Java serializes LinkedList: it fetches all elements and writes them to the ObjectOutputStream, together with the size. And of course declares the header entry transient

See the writeObject and readObject methods of LinkedList:

// Write out any hidden serialization magic
s.defaultWriteObject();

// Write out size
s.writeInt(size);

// Write out all elements in the proper order.
for (Entry e = header.next; e != header; e = e.next)
    s.writeObject(e.element);
like image 87
Bozho Avatar answered May 04 '26 09:05

Bozho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!