Java addAll(collection) vs new ArrayList(collection)
2022-09-03 06:02:33
为什么我会有不同的行为:
Collection col2 = new ArrayList(col);
Collection col2 = new ArrayList();
col2.addAll(col)
我正在与观众合作,代码很复杂,我试图解释问题的“根源”。另一个有趣的事实是下一个...
//IF i use this code i have the correct behavior in my app:
public void updateCollection(Collection<Object> col) {
this.objectCollection.clear();
this.objectCollection.addAll(col);
}
//IF i use this code i have unexpected behavior in my app:
public void updateCollection(Collection<Object> col) {
this.objectCollection=new ArrayList(col);
}