java - Arraylist 删除第一次出现,而不是一个确切的对象

标签 java arraylist

我在 jsf 中有数据表。单击添加按钮后,根据所选值加载列表。元素已成功添加到列表中。 添加到列表中,

public class CompanyProduct implements Serializable{
    private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue
        @Getter @Setter private int id;

        @ManyToOne
        @JoinColumn(name = "productId", referencedColumnName = "id")
        @Getter @Setter private Product product;

        @ManyToOne
        @JoinColumn(name = "companyId", referencedColumnName = "id")
        @Getter @Setter private Company company;

        @Setter @Getter private Double price;

        @Setter @Getter private Double sellingprice;

        @Column
        @Getter @Setter private Integer minQuantity;

        @Column
        @Getter @Setter private Integer maxQuantity;

        @Column
        @Getter @Setter private Integer leadTime;

        @ManyToOne
        @JoinColumn(name="uom_group_item_id")
        @Getter @Setter private UomGroupItem uomGroupItem;

}

public void addToAssociation() {


        List<CompanyProduct> tmpList = new ArrayList<CompanyProduct>(companyproducts);

        for (Product p : selectedProducts) {
            boolean flag = false;

            for(CompanyProduct companyProduct: tmpList){
                if(companyProduct.getUomGroupItem() == null && companyProduct.getProduct() != null && companyProduct.getProduct().getId().equals(p.getId())){
                    flag = true;
                }
            }

            if(!flag){
                addCompanyProducts(p, null);
            }

            addUomGroupProduct(p);
        }
        selectedProducts = null;
        loadLazyProducts();
    }

private void addCompanyProducts(Product p,UomGroupItem item){
        CompanyProduct sg = new CompanyProduct();
        sg.setProduct(p);
        sg.setCompany(company);
        sg.setPrice((double) (p.getMrp()==null?0f:p.getMrp()));
        sg.setSellingprice((double) (p.getMrp()==null?0f:p.getMrp()));

        if(item != null){
            sg.setUomGroupItem(item);
        }

        companyproducts.add(sg);
    }

但问题是,当我删除时,正在执行操作,

for (CompanyProduct sg : selectedToBeAssociatedProducts) {
        companyproducts.remove(sg);
    }

但它删除了列表中的第一次出现。所以我可以知道这个问题的原因吗?提前致谢。

如有任何疑问,请告诉我。

最佳答案

这是设计使然,请参阅 List API

remove(Object o) 

Removes the first occurrence of the specified element from this list, if it is present (optional operation).

如果你想删除所有出现的地方,使用 List.removeAll,像这样

list.removeAll(Collectoins.singelton(obj);

关于java - Arraylist 删除第一次出现,而不是一个确切的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46638968/

相关文章:

java - 具有多个实例但具有唯一对象标识的已排序集合

Java:在数组列表中删除期间出现运行时错误

java - 为什么 List.add(E) 返回 boolean 而 List.Add(int, E) 返回 void?

java - 将两个 ArrayList 中的数据写入 CSV

java - 在pom.xml 和settings.xml 中覆盖Maven 属性的顺序是什么?

java - 如何在 java 中使用 JAX - RS 和 tomcat 在 eclipse 中使用 Web 服务从数据库中插入和检索数据

java - 从 ArrayList 添加/删除值以获得唯一值

Java ArrayList indexOf 泛型类型

java - java中如何实现两级并行

java - 初学者尝试编写一个简单的计算器