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?

有帮助吗?

解决方案 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.

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top