How to Serialize a list in java?

179,434

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
179,434

Related videos on Youtube

Rakesh Juyal
Author by

Rakesh Juyal

Actively seeking new opportunities.

Updated on June 21, 2020

Comments

  • Rakesh Juyal
    Rakesh Juyal almost 4 years

    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?

  • Jesper
    Jesper over 14 years
    Why would anyone ever want to cast anything to Serializable? Serializable is only a marker interface; casting something to Serializable isn't useful.
  • skaffman
    skaffman over 14 years
    Because SerializationUtils.clone() requires an argument on type Serializable, and so if your variable is of type List, you'll need to cast it.
  • Zack Marrapese
    Zack Marrapese almost 13 years
    @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.
  • Evi Song
    Evi Song over 12 years
    Yes, I found this question when searching Android solutions. My addition is, List<MyClass> myObjs = new ArrayList<MyClass>(); doesn't meet the Serializable, but ArrayList<MyClass> myObjs = new ArrayList<MyClass>(); does.
  • arne.b
    arne.b about 12 years
    As pointed out already, List is an interface that does not extend Serializable or any interface that does so. Lists that are not serializable may not be common in standard libraries, but that does not make the first sentence right.
  • Blundell
    Blundell over 9 years
    @EviSong because the interface List is not serializable. Your declaring your variable of type List.
  • aakoch
    aakoch about 9 years
    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?
  • Theodore Murdock
    Theodore Murdock over 8 years
    @Adam It might be best to guarantee safety at compile-time instead by using the signature <T extends List<Foo> & Serializable> setFooList(T list), which requires your caller to pass you an instance of some List implementation that is also Serializable.
  • MG Developer
    MG Developer over 2 years
    List is not but implementation classes like ArrayLists are serializable. You can use them.