我在 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/