小编GDe*_*ell的帖子

删除ArrayList对象的重复项并获取值的总和

我有某种类型的对象的列表。我需要删除重复的对象键字段并总结它们的值。这有点难以解释,所以让我给你举个例子。

假设您有一个 Item 类:

public class Item {

protected String name;

protected int quantity;

public String getName() {
    return name;
   }

public void setName(String name) {
    this.name = name;
   }

public int getQuantity() {
    return quantity;
   }

public void setQuantity(intquantity) {
    this.quantity = quantity;
   }

}
Run Code Online (Sandbox Code Playgroud)

你有一个项目列表:

List<Item> itemList = new ArrayList<Item>();
Run Code Online (Sandbox Code Playgroud)

填充为:

 Item item1 = new Item();
 item1.setName("mice");
 item1.setQuantity(20);

 Item item2 = new Item();
 item2.setName("keyboards");
 item2.setQuantity(30);


 Item item3 = new Item();
 item3.setName("monitors");
 item3.setQuantity(4);

 Item item4 = new Item();
 item4.setName("mice"); …
Run Code Online (Sandbox Code Playgroud)

java comparison arraylist

3
推荐指数
1
解决办法
2322
查看次数

标签 统计

arraylist ×1

comparison ×1

java ×1