// http://howtodoinjava.com/core-java/cloning/a-guide-to-object-cloning-in-java/
// http://stackoverflow.com/questions/2156120/java-recommended-solution-for-deep-cloning-copying-an-instance
测试了几个,不是
// method 1, ok
// List<T> dst = (List<T>)SerializationUtils.clone(ArrayList.class.cast(src)); //too slow
/*
//method 2, ng
List<T> dst = new ArrayList<T>();
CollectionUtils.addAll(dst, new Object[src.size()]);
Collections.copy(dst, src);
*/
/*
//method 3, ng
T[] srcT = toArray(src);
T[] dstT = (T[])Array.newInstance(srcT.getClass().getComponentType(), src.size());
System.arraycopy(srcT, 0, dstT, 0, srcT.length);
List<T> dst = Arrays.asList(dstT);
*/
/*
//method 4, ng, need Clonable
List<T> dst = new ArrayList<T>();
for (T o: src)
{
T cloned = null;
if (o instanceof Cloneable)
{
//cloned = o.clone();
try
{
cloned = (T)(o.getClass().getMethod("clone").invoke(o));
} catch (Exception e)
{
e.printStackTrace();
}
}
else {
cloned = SerializationUtils.clone(o);
}
dst.add(cloned);
}
*/
// method 5, ok
List<T> dst = (List<T>) cloner.deepClone(src);
没有评论:
发表评论