如何在java中序列化列表? [英] How to Serialize a list in java?
问题描述
我想深入克隆一个列表。因为我们有一个方法
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?
推荐答案
java.util的所有标准实现。
因此,即使<$ c $ c> java.util.List 本身不是 java.io.Serializable
的子类型,应该安全地将列表转换为 Serializable
,或者 LinkedList c> code>。
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
.
如果您不确定,请先复制列表(使用 new ArrayList code>),那么你知道它是可序列化的。
If you're not sure, then copy the list first (using something like new ArrayList(myList)
), then you know it's serializable.
这篇关于如何在java中序列化列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!