【问题标题】:Add value on other variables if a variable of an element in list is duplicate如果列表中元素的变量重复,则在其他变量上添加值
【发布时间】:2022-11-04 00:04:33
【问题描述】:

我的列表由变量 Type(String)、Amount(Double) 和 Quantity(Integer) 组成,它看起来像这样:

Type: Type A, Amount : 55.0, Quantity : 0
Type: Type A, Amount : 55.0, Quantity : 5
Type: Type A, Amount : 44.35, Quantity : 6
Type: Type A, Amount : 55.0, Quantity : 0
Type: Type B, Amount : 7.0, Quantity : 1
Type: Type B, Amount : 7.0, Quantity : 1
Type: Type C, Amount : 1613.57, Quantity : 0
Type: Type C, Amount : 1613.57, Quantity : 1

所以我试图循环我的数组以查找重复项,如果重复则添加数量。结果将是这样的:

Type: Type A, Amount : 209.35.0, Quantity : 11
Type: Type B, Amount : 14.0, Quantity : 2
Type: Type C, Amount : 3227.14, Quantity : 1

我尝试的是创建另一个列表,将列表添加到新列表,然后比较它们,但没有用

List<Type> newList = new ArrayList();
        for(int k = 0; k < typeList.size(); k++) {
            Type type= new Type();
            Double totalAmount = Double.parseDouble("0");
            type.setTypeName(typeList.get(k).getTypeName());
            type.setAmount(chargeTypeList.get(k).getAmount());
            newList.add(k, type);
            if(typeList.get(k).getChargeTypeName().equalsIgnoreCase(newList.get(k).getiTypeName())) {
                totalAmount += typeList.get(k).getAmount();
            }
        }

我不希望对值进行硬编码以检查重复类型

【问题讨论】:

    标签: java arraylist duplicates


    【解决方案1】:

    您可能应该将这些值放入 M​​ap 中,这样可以保证每个键只有一个元素。使用地图来表示一些数量是很常见的事物我们将事物存储为密钥并跟踪我们在密钥中拥有多少这些事物。

    您可以使用 compute 然后将元素添加到列表中。

    你目前拥有的:

    record Data(String type, Double amount, Integer quantity) {}
    

    什么可以更好地代表您的数据:

    record Datav2(Double amount, Integer quantity) {}
    

    将 Datav2 存储在地图中并添加元素。

    var map = new HashMap<>(Map.of("A", new Datav2( 2.0, 3)));
            
    // add element to map equivalent to Data("A", 3.0, 3)
    map.compute("A", (k, v) -> {
        if (v == null) {
            v = new Datav2(0.0, 0); 
        }
        return new Datav2(v.amount + 3.0, v.quantity + 3);
    });
    

    如果您出于某种原因需要从列表开始,您可以使用Stream API 将列表转换为地图。

    var list = List.of(new Data("A", 2.0, 3),
           new Data("A", 3.0, 3),
           new Data("C", 2.0, 1),
           new Data("B", 10.0, 3),
           new Data("B", 2.0, 5)
    );
    
    var collected = list
             .stream()
             .collect(Collectors.toMap(
                           // what will the key be
                           Data::type,
                           // what will the value be
                           data -> new Datav2(data.amount, data.quantity),
                           // how do we combine two values if they have the same key
                           (d1, d2) -> new Datav2(d1.amount + d2.amount, d1.quantity + d2.quantity)
             ));
    System.out.println(collected);
    

    {A=Datav2[amount=5.0, quantity=6], B=Datav2[amount=12.0, quantity=8], C=Datav2[amount=2.0, quantity=1]}

    【讨论】:

      猜你喜欢
      • 2019-08-27
      • 2015-08-11
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      相关资源
      最近更新 更多