views:

2360

answers:

3

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

SerializationUtils.clone ( object ) // apache commons method. Object shld be serializable

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

+8  A: 

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.

skaffman
Why would anyone ever want to cast anything to Serializable? Serializable is only a marker interface; casting something to Serializable isn't useful.
Jesper
Because `SerializationUtils.clone()` requires an argument on type `Serializable`, and so if your variable is of type `List`, you'll need to cast it.
skaffman
+2  A: 

List is just an interface. The question is: is your actual List implementation serializable? Speaking about the standard List implementations (ArrayList, LinkedList) from the Java run-time, most of them actually are already.

Dirk
+1  A: 

As pointed out already, List is serializable. However you have to ensure that the objects referenced/contained within the list are also serializable.

Brian Agnew