87

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?

Schuyler
  • 509
  • 1
  • 9
  • 19
Rakesh Juyal
  • 35,919
  • 68
  • 173
  • 214

3 Answers3

183

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.

Nader Shirazie
  • 10,736
  • 2
  • 37
  • 43
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 1
    Why would anyone ever want to cast anything to Serializable? Serializable is only a marker interface; casting something to Serializable isn't useful. – Jesper Sep 07 '09 at 11:35
  • 20
    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 Sep 07 '09 at 13:03
  • 2
    @Jesper: It's a marker interface that says it's safe to serialize the class. Android uses the Serializable interface for many things; passing along intent extras, shared preferences, etc. – Zack Marrapese May 10 '11 at 13:27
  • 11
    Yes, I found this question when searching Android solutions. My addition is, `List myObjs = new ArrayList();` doesn't meet the `Serializable`, but `ArrayList myObjs = new ArrayList();` does. – Evi Song Dec 13 '11 at 17:34
  • @EviSong because the `interface` `List` is **not** `serializable.` Your declaring your variable of type `List`. – Blundell Oct 08 '14 at 07:53
  • If I have a bean that contains a List of serializable objects, should I put a check in the setter to make sure the List is serializable since I won't know what kind of list is being set? – aakoch Feb 25 '15 at 19:18
  • 3
    @Adam It might be best to guarantee safety at compile-time instead by using the signature ` & Serializable> setFooList(T list)`, which requires your caller to pass you an instance of some List implementation that is also Serializable. – Theodore Murdock Sep 22 '15 at 21:49
36

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

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 2
    As pointed out already, `List` is an interface that does not extend `Serializable` or any interface that does so. `List`s that are not serializable may not be common in standard libraries, but that does not make the first sentence right. – arne.b Feb 17 '12 at 12:27
  • List is not but implementation classes like ArrayLists are serializable. You can use them. – MG Developer Oct 27 '21 at 00:34
15

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
  • 30,623
  • 8
  • 82
  • 102