List常见的复制方式:
首先准备一个实体类:
public class User {
private String userName;
private Integer age;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User(String userName, Integer age) {
this.userName = userName;
this.age = age;
}
public User() {
}
@Override public String toString() {
return "User{" + "userName='" + userName + '\'' + ", age=" + age + '}';
}
}
其次构造原始List集合
private List sourceList = new ArrayList<>();
@Before public void setUp() throws Exception {
sourceList.add(new User("Tom",21));
sourceList.add(new User("Lucy",22));
sourceList.add(new User("Jack",23));
}
@Test
public void shallowCopyAddAll() {
System.out.println("Copy before -> sourceList-> "+ sourceList);
System.out.println("**********************************************");
List targetList = new ArrayList<>();
targetList.addAll(sourceList);
targetList.get(0).setAge(30);
System.out.println("After the copy -> sourceList-> "+sourceList);
System.out.println("After the copy -> targetList-> "+targetList);
}
结果:
Copy before -> sourceList-> [User{userName='Tom', age=21}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
**********************************************
After the copy -> sourceList-> [User{userName='Tom', age=30}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
After the copy -> targetList-> [User{userName='Tom', age=30}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
修改克隆集合中的值,源集合也随之发生了变化。
@Test
public void shallowCopy() {
System.out.println("Copy before -> sourceList-> "+ sourceList);
System.out.println("**********************************************");
List targetList = new ArrayList<>(sourceList);
targetList.get(0).setAge(40);
System.out.println("After the copy -> sourceList-> "+sourceList);
System.out.println("After the copy -> targetList-> "+targetList);
}
结果:
Copy before -> sourceList-> [User{userName='Tom', age=21}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
**********************************************
After the copy -> sourceList-> [User{userName='Tom', age=40}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
After the copy -> targetList-> [User{userName='Tom', age=40}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
修改克隆集合中的值,源集合也随之发生了变化。
@Test
public void shallowCopySysCopy() {
System.out.println("Copy before -> sourceList-> "+ sourceList);
System.out.println("**********************************************");
User[] targets = new User[sourceList.size()];
Object[] sources = sourceList.toArray();
System.arraycopy(sources,0,targets,0,sourceList.size());
targets[0].setAge(50);
System.out.println("After the copy -> sourceList-> "+ Arrays.asList(sources));
System.out.println("After the copy -> targetList-> "+Arrays.asList(targets));
}
结果:
Copy before -> sourceList-> [User{userName='Tom', age=21}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
**********************************************
After the copy -> sourceList-> [User{userName='Tom', age=50}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
After the copy -> targetList-> [User{userName='Tom', age=50}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
修改克隆集合中的值,源集合也随之发生了变化。
@Test
public void shallowCopyCollectionsCopy() {
System.out.println("Copy before -> sourceList-> "+ sourceList);
System.out.println("**********************************************");
List targetList = new ArrayList<>();
Collections.addAll(targetList,new User[sourceList.size()]);
Collections.copy(targetList,sourceList);
targetList.get(0).setAge(51);
System.out.println("After the copy -> sourceList-> "+ sourceList);
System.out.println("After the copy -> targetList-> "+targetList);
}
结果:
Copy before -> sourceList-> [User{userName='Tom', age=21}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
**********************************************
After the copy -> sourceList-> [User{userName='Tom', age=51}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
After the copy -> targetList-> [User{userName='Tom', age=51}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
修改克隆集合中的值,源集合也随之发生了变化。
@Test
public void shallowCopyCollectionsAddAll() {
System.out.println("Copy before -> sourceList-> "+ sourceList);
System.out.println("**********************************************");
List targetList = new ArrayList<>();
User[] sources = new User[sourceList.size()];
for (int i = 0; i < sourceList.size(); i++) {
sources[i] = sourceList.get(i);
}
Collections.addAll(targetList,sources);
targetList.get(0).setAge(52);
System.out.println("After the copy -> sourceList-> "+ sourceList);
System.out.println("After the copy -> targetList-> "+targetList);
}
结果:
Copy before -> sourceList-> [User{userName='Tom', age=21}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
**********************************************
After the copy -> sourceList-> [User{userName='Tom', age=52}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
After the copy -> targetList-> [User{userName='Tom', age=52}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
修改克隆集合中的值,源集合也随之发生了变化。
@Test
public void shallowCopyCycle() {
System.out.println("Copy before -> sourceList-> "+ sourceList);
System.out.println("**********************************************");
List targetList = new ArrayList<>();
for (int i = 0; i < sourceList.size(); i++) {
targetList.add(sourceList.get(i));
}
targetList.get(0).setAge(60);
System.out.println("After the copy -> sourceList-> "+sourceList);
System.out.println("After the copy -> targetList-> "+targetList);
}
结果:
Copy before -> sourceList-> [User{userName='Tom', age=21}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
**********************************************
After the copy -> sourceList-> [User{userName='Tom', age=60}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
After the copy -> targetList-> [User{userName='Tom', age=60}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
修改克隆集合中的值,源集合也随之发生了变化。
@Test
public void shallowCopyForEach() {
System.out.println("Copy before -> sourceList-> "+ sourceList);
System.out.println("**********************************************");
List targetList = new ArrayList<>();
sourceList.forEach(user -> {
targetList.add(user);
});
targetList.get(0).setAge(70);
System.out.println("After the copy -> sourceList-> "+sourceList);
System.out.println("After the copy -> After the copy -> targetList-> "+targetList);
}
结果:
Copy before -> sourceList-> [User{userName='Tom', age=21}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
**********************************************
After the copy -> sourceList-> [User{userName='Tom', age=70}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
After the copy -> After the copy -> targetList-> [User{userName='Tom', age=70}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
修改克隆集合中的值,源集合也随之发生了变化。
优化实体类:
public class User implements Serializable {
private String userName;
private Integer age;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User(String userName, Integer age) {
this.userName = userName;
this.age = age;
}
public User() {
}
@Override public String toString() {
return "User{" + "userName='" + userName + '\'' + ", age=" + age + '}';
}
}
工具类:
public static List deepCopy(List sourceList) throws IOException, ClassNotFoundException {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(sourceList);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
@SuppressWarnings("unchecked")
List dest = (List) in.readObject();
return dest;
}
使用方式:
@Test
public void shallowCopyDeepCopy() throws IOException, ClassNotFoundException {
System.out.println("Copy before -> sourceList-> "+ sourceList);
System.out.println("**********************************************");
List targetList = ListUtils.deepCopy(sourceList);
targetList.get(0).setAge(80);
targetList.get(1).setAge(91);
System.out.println("After the copy -> sourceList-> "+sourceList);
System.out.println("After the copy -> After the copy -> targetList-> "+targetList);
}
结果:
Copy before -> sourceList-> [User{userName='Tom', age=21}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
**********************************************
After the copy -> sourceList-> [User{userName='Tom', age=21}, User{userName='Lucy', age=22}, User{userName='Jack', age=23}]
After the copy -> After the copy -> targetList-> [User{userName='Tom', age=80}, User{userName='Lucy', age=91}, User{userName='Jack', age=23}]
博主的一些对List集合操作的工具类源码地址:
https://github.com/jinchunzhao