How to Serialize a list in java?
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.
Related videos on Youtube
Comments
-
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 over 14 yearsWhy would anyone ever want to cast anything to Serializable? Serializable is only a marker interface; casting something to Serializable isn't useful.
-
skaffman over 14 yearsBecause
SerializationUtils.clone()
requires an argument on typeSerializable
, and so if your variable is of typeList
, you'll need to cast it. -
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 over 12 yearsYes, I found this question when searching Android solutions. My addition is,
List<MyClass> myObjs = new ArrayList<MyClass>();
doesn't meet theSerializable
, butArrayList<MyClass> myObjs = new ArrayList<MyClass>();
does. -
arne.b about 12 yearsAs pointed out already,
List
is an interface that does not extendSerializable
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. -
Blundell over 9 years@EviSong because the
interface
List
is notserializable.
Your declaring your variable of typeList
. -
aakoch about 9 yearsIf 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 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 over 2 yearsList is not but implementation classes like ArrayLists are serializable. You can use them.