Java addAll(集合)与新的ArrayList(集合)

为什么我会得到不同的行为:

  1. Collection col2 = new ArrayList(col);

  2. Collection col2 = new ArrayList();
    col2.addAll(col)

我正在与观众合作,代码很复杂,我试图解释问题的“根”。 另一个有趣的事实是下一个……

 //IF i use this code i have the correct behavior in my app: public void updateCollection(Collection col) { this.objectCollection.clear(); this.objectCollection.addAll(col); } //IF i use this code i have unexpected behavior in my app: public void updateCollection(Collection col) { this.objectCollection=new ArrayList(col); } 

此代码有效:

 public void updateCollection(Collection col) { this.objectCollection.clear(); this.objectCollection.addAll(col); } 

但这引入了一些问题:

 public void updateCollection(Collection col) { this.objectCollection=new ArrayList(col); } 

我怀疑你的第一种方法的这种变化会引入相同的问题:

 public void updateCollection(Collection col) { this.objectCollection = new ArrayList(); this.objectCollection.clear(); this.objectCollection.addAll(col); } 

为什么? 显然你在某处使用了另一个对objectCollection的引用。 在代码中的某个地方,另一个对象说(例如):

myCopyOfObjectCollection = theOtherObject.objectCollection;

如果您正在使用getter,这不会改变基础行为 – 您仍然会保留另一个参考。

因此,如果在初始分配时,例如,集合包含{1,2,3},您可以从以下开始:

  • this.objectCollection:{1,2,3}
  • that.copyOfObjectCollection:{1,2,3}

当你为this.objectCollection分配一个新的 ArrayList,并用{4,5,6}填充它时,你得到这个:

  • this.objectCollection:{4,5,6}
  • that.copyOfObjectCollection:{1,2,3}

“那”仍然指向原始的ArrayList。

 Collection col2 = new ArrayList(col); 

将创建一个大小为col.size() (+ 10%)的新ArrayList ,并将col所有元素复制到该数组中。

 Collection col2 = new ArrayList(); 

将创建一个初始大小为10的新ArrayList(至少在Sun实现中)。

 col2.addAll(col); 

col所有元素复制到col2 ArrayList的末尾,如果需要,放大后备数组大小。

因此,根据您的col集合大小,行为会有所不同,但不会太多。

最好使用第一个选项 – 这将避免至少一个额外的后备arrays扩展操作。

  public List getAdminImIdsWithValidShortNames(){ return adminImIdsWithValidShortNames; } public void setAdminImIdsWithValidShortNames(List adminImIdsWithValidShortNames){ this.adminImIdsWithValidShortNames=adminImIdsWithValidShortNames; } 

我认为,简单就是美观,只需发电机定位器/吸气方法就是一个好习惯。 如果你先清除,那么addAll,列表需要清除列表中的所有元素,然后addAll将是额外的支持数组扩展操作,那不是科学。

只需更换,这个变量将指向新的List,旧的列表将是自动GC。