Reputation: 1338
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?
Upvotes: 0
Views: 140
Reputation: 1859
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.
Upvotes: 1
Reputation: 310869
Yes. Object graphs are preserved in full generality, and ArrayList
is Serializable.
Upvotes: 3