Java addAll(collection) vs new ArrayList(collection)

前端 未结 3 710
我寻月下人不归
我寻月下人不归 2021-01-08 00:08

Why do i get different behaviors with:

  1. Collection col2 = new ArrayList(col);

  2. Collection col2 = new ArrayList();

3条回答
  •  伪装坚强ぢ
    2021-01-08 00:59

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

    I think, easy is beautiful, just generator setter/getter method is a good habit. if you first clear, then addAll, the list need to clear all elements of list, then addAll will be extra backing array expansion operation, that's not science.

    just replacement, this variable will be point to new List, the old list will be auto-GC.

提交回复
热议问题