How to Serialize a list in java?

Rakesh Juyal picture Rakesh Juyal · Sep 7, 2009 · Viewed 163.6k times · Source

I would like to deep clone a List. for that we are having a method

// apache commons method. This object should be serializable
SerializationUtils.clone ( object ) 

so now to clone my List i should convert that to serializable first. Is it possible to convert a List into Serializable list?

Answer

skaffman picture skaffman · Sep 7, 2009

All standard implementations of java.util.List already implement java.io.Serializable.

So even though java.util.List itself is not a subtype of java.io.Serializable, it should be safe to cast the list to Serializable, as long as you know it's one of the standard implementations like ArrayList or LinkedList.

If you're not sure, then copy the list first (using something like new ArrayList(myList)), then you know it's serializable.