我如何克隆一个数组列表,也克隆其项目在Java?
例如,我有:
ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = ....something to do with dogs....
我希望clonedList中的对象与dogs list中的对象不一样。
我如何克隆一个数组列表,也克隆其项目在Java?
例如,我有:
ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = ....something to do with dogs....
我希望clonedList中的对象与dogs list中的对象不一样。
当前回答
Java 8提供了一种优雅而简洁地调用元素狗的复制构造函数或克隆方法的新方法:流、lambdas和收集器。
拷贝构造函数:
List<Dog> clonedDogs = dogs.stream().map(Dog::new).collect(toList());
表达式Dog::new被称为方法引用。它创建了一个函数对象,调用Dog上的构造函数,该构造函数以另一个Dog作为参数。
克隆方法[1]:
List<Dog> clonedDogs = dogs.stream().map(Dog::clone).collect(toList());
得到一个数组列表作为结果
或者,如果你必须得到一个数组列表(以防你以后想修改它):
ArrayList<Dog> clonedDogs = dogs.stream().map(Dog::new).collect(toCollection(ArrayList::new));
及时更新列表
如果你不需要保留狗列表的原始内容,你可以使用replaceAll方法并在适当的地方更新列表:
dogs.replaceAll(Dog::new);
所有示例都假设导入静态java.util.stream.Collectors.*;
数组列表收集器
上一个例子中的收集器可以被做成一个util方法。因为这是一件很常见的事情,我个人喜欢它是短而漂亮的。是这样的:
ArrayList<Dog> clonedDogs = dogs.stream().map(d -> d.clone()).collect(toArrayList());
public static <T> Collector<T, ?, ArrayList<T>> toArrayList() {
return Collectors.toCollection(ArrayList::new);
}
[1] CloneNotSupportedException异常说明:
为了使这个解决方案工作,Dog的克隆方法不能声明它抛出CloneNotSupportedException。原因是map的参数不允许抛出任何受控异常。
是这样的:
// Note: Method is public and returns Dog, not Object
@Override
public Dog clone() /* Note: No throws clause here */ { ...
然而,这应该不是一个大问题,因为这是最佳实践。(例如,effective Java给出了这个建议。)
感谢Gustavo注意到这一点。
其他回答
下面的方法对我有用。
在Dog.java
public Class Dog{
private String a,b;
public Dog(){} //no args constructor
public Dog(Dog d){ // copy constructor
this.a=d.a;
this.b=d.b;
}
}
-------------------------
private List<Dog> createCopy(List<Dog> dogs) {
List<Dog> newDogsList= new ArrayList<>();
if (CollectionUtils.isNotEmpty(dogs)) {
dogs.stream().forEach(dog-> newDogsList.add((Dog) SerializationUtils.clone(dog)));
}
return newDogsList;
}
在这里,由createCopy方法创建的新列表是通过SerializationUtils.clone()创建的。 因此,对新列表所做的任何更改都不会影响原始列表
简单的方法,使用common -lang-2.3.jar的java库克隆列表
链接下载commons-lang-2.3.jar
如何使用
oldList.........
List<YourObject> newList = new ArrayList<YourObject>();
foreach(YourObject obj : oldList){
newList.add((YourObject)SerializationUtils.clone(obj));
}
我希望这篇文章能有所帮助。
:D
我想我找到了一个非常简单的方法来创建一个深度复制数组列表。假设你想复制一个字符串数组列表arrayA。
ArrayList<String>arrayB = new ArrayList<String>();
arrayB.addAll(arrayA);
如果对你不起作用请告诉我。
您将需要迭代这些项,并逐个克隆它们,将克隆放入结果数组中。
public static List<Dog> cloneList(List<Dog> list) {
List<Dog> clone = new ArrayList<Dog>(list.size());
for (Dog item : list) clone.add(item.clone());
return clone;
}
显然,要做到这一点,必须让Dog类实现Cloneable接口并重写clone()方法。
其他的海报是正确的:你需要迭代列表并复制到一个新的列表。
然而…… 如果列表中的对象是不可变的-你不需要克隆它们。如果你的对象有一个复杂的对象图,它们也需要是不可变的。
不可变性的另一个好处是它们也是线程安全的。