代码之家  ›  专栏  ›  技术社区  ›  sagar suri

如何从包含重复项目Java的列表创建唯一列表

  •  0
  • sagar suri  · 技术社区  · 6 年前

    请仔细阅读。我尝试在列表中查找所有唯一的项目,并按照以下条件将其复制到另一个列表: 我有一个这样的pojo类:

    class MyObject {
        String name;
        int id;
        int quantity;
    
        public MyObject(String s1,int id, int s2) {
            this.name = s1;
            this.id = id;
            this.quantity = s2;
         }
    }
    

    我有一个 ArrayList 包含上述类的重复对象。我要将所有唯一的对象复制到新的 数组列表 但所有独特的物体 quantity 将根据第一个列表中重复元素的数量增加。如何做到这一点?

    例如:如果重复列表包含2个ID为1的对象。然后新列表将包含1个对象,其数量增加到2。

    我的代码:

    Set<Card> uniqueRefundItems = new LinkedHashSet<>();
    for (Card lcp : refundList) {
       if (uniqueRefundItems.contains(lcp)) {
           lcp.setQuantity(lcp.getQuantity() + 1);
       } else {
           lcp.setQuantity(1);
    
       }
       uniqueRefundItems.add(lcp);
    }
    

    但当我做一个 深拷贝 . 它不起作用。还有其他的方法吗?

    2 回复  |  直到 6 年前
        1
  •  0
  •   Joakim Danielson    6 年前

    这将创建具有更新数量的唯一对象列表

    List<MyObject> list = new ArrayList<>();
    list.add(new MyObject("AAA", 1, 0));
    list.add(new MyObject("CCC", 3, 0));
    list.add(new MyObject("AAA", 1, 0));
    list.add(new MyObject("BBB", 2, 0));
    list.add(new MyObject("AAA", 1, 0));
    
    Map<Integer, MyObject> temp = new HashMap<>();
    list.forEach( x -> {
        MyObject o = temp.get(x.id);
       if (o == null) {
            temp.put(x.id, x);
            x.quantity = 1;
        } else {
            o.quantity++;
        }
    });
    List<MyObject> result = new ArrayList<>(temp.values());
    

    请注意,此答案适用于问题中的myObject代码示例。将新对象添加到 temp map i将数量设置为1,因为这是问题中如何处理的,但可以说需要更好的逻辑。

        2
  •  0
  •   DwB    6 年前

    您的尝试已接近正确。

    下面的代码是一种“老派”(即没有流)方法:

    // don't have setters in this object.
    public class MyObject
    {
      private final int id;
      private final String name;
      private int quantity;
    
      public MyObject(final int theId, final String theName)
      {
        id = theId;
        name = theName;
    
        quantity = 1;
      }
    
      public void incrementQuantity()
      {
        quantity += 1;
      }
    
      public int getId() { ... }
      public String getName() { ... }
      public int getQuantity() { ... }
    
      public boolean equals(final Object other)
      {
        // normal equals with 1 small caveat: only compare id and name to determine equals.
      }
    }
    
    public List<MyObject> blam(final List<MyObject> refundList)
    {
      final List<MyObject> returnValue = new LinkedList<>();
      final Map<MyObject, MyObject> thingMap = new HashMap<>();
    
      for (final MyObject currentRefund : refundList)
      {
        final MyObject thing = thingMap.get(currentRefund);
    
        if (thing != null)
        {
          thing.incrementQuantity();
        }
        else
        {
          thingMap.put(currentRefund, currentRefund);
        }
      }
    
      returnValue.addAll(thingMap.values());
    
      returnReturnValue;
    }
    

    编辑:固定 put