Question

I am trying to send an ArrayList<MyObject>() over a Socket connection. MyObject implements Serializable, and I am using the ObjectOutputStream & ObjectInputStream at respective ends of the Socket with the corresponding methods on either end. On the one end I send:

output.writeObject(myList);

On the other end,

ArrayList<MyObject> myList = (ArrayList<MyObject>) input.readObject();

Now my question is if this is valid. I know that MyObject is serializable and there is no problem sending one of them over at a time. However, is this property preserved if I am sending multiple of these objects in some sort of Java collection?

Was it helpful?

Solution 2

Serializing an array list is not a problem. Because it implements Serializeable. But you need to make sure MyObject doesn't contain reference to another class that doesn't implement serializable.

And object graph is maintained when you deserialize you will get everything back minus transient variables.

OTHER TIPS

Yes. Object graphs are preserved in full generality, and ArrayList is Serializable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top