使用Map获取ArrayList中相同String名称内的整数之和

时间:2018-05-31 13:40:43

标签: android dictionary arraylist

我有一个ArrayList,其中包含一个String值和两个Integer值。我希望在相同的String值下使用整数之和。例如

  • aaa , 1 , 2
  • aaa , 2 , 1
  • bbb , 1 , 2
  • ccc , 3 , 3
  • ccc , 1 , 2
  • ccc , 2 , 2

所以最终名单应该是

  • aaa , 3, 3
  • bbb , 1, 2
  • ccc , 6, 7

我能够使用一个String和一个Integer获得总和。

这是我用于一个String和一个Integer的内容。

ArrayList<InvoiceData> invoiceHeaderList = new ArrayList<>();
invoiceHeaderList.clear();
Map<String, Integer> sumMap = new TreeMap<String, Integer>();

for (ItemData dashboardOneData : invoiceList) {
    String key = dashboardOneData.getInvoiceNo() + "#" + dashboardOneData.getInvoiceDate();
    if (sumMap.containsKey(key)) {
        int sum = sumMap.get(key);
        sum += dashboardOneData.getMatQty();
        sumMap.put(key, sum);
    } else {
        sumMap.put(key, dashboardOneData.getMatQty());
    }
}

for (Map.Entry<String, Integer> e : sumMap.entrySet()) {
    String[] splitInvoice;
    InvoiceData invTempData = new InvoiceData();
    if (e.getKey() != null || !e.getKey().equals("")) {
        splitInvoice = e.getKey().split("#+");
        invTempData.setInvoiceNo(splitInvoice[0]);
        invTempData.setInvoiceDate(splitInvoice[1]);
        invTempData.setInvoiceQty(e.getValue());
        invoiceHeaderList.add(invTempData);
    }
}

我试着按照以下方式做但没有运气,我无法在方法中取两个整数的总和。

public class QuantityData {
    private int oriQty;
    private int newQty;

    public QuantityData() {
    }
}

...方法

ArrayList<InvoiceData> invoiceHeaderList = new ArrayList<>();
invoiceHeaderList.clear();
Map<String, List<QuantityData>> sumMap = new TreeMap<String, List<QuantityData>>();

for (ItemData dashboardOneData : invoiceList) {
    String key = dashboardOneData.getInvoiceNo() + "#" + dashboardOneData.getInvoiceDate();
    QuantityData qtyData = new QuantityData();
    if (sumMap.containsKey(key)) {
        ArrayList<QuantityData> qtyArrayList = new ArrayList<>();

        qtyArrayList.add(qtyData);
        sumMap.put(key, qtyArrayList);

    } else {
        ArrayList<QuantityData> qtyArrayList = new ArrayList<>();
        qtyData.setOriQty(dashboardOneData.getMatQty());
        qtyData.setNewQty(dashboardOneData.getNewMatQty());
        qtyArrayList.add(qtyData);
        sumMap.put(key, qtyArrayList);
    }
}

for (Map.Entry<String, List<QuantityData>> e : sumMap.entrySet()) {
    String[] splitInvoice;
    InvoiceData invTempData = new InvoiceData();
    if (e.getKey() != null || !e.getKey().equals("")) {

    }
}

3 个答案:

答案 0 :(得分:2)

如果我理解你的目标,你根本就不需要ArrayLists。

你尝试过这样的事吗?

Map<String, QuantityData> sumMap = new TreeMap<String, QuantityData>();
QuantityData qtyData;
for (ItemData dashboardOneData : invoiceList) {
    String key = dashboardOneData.getInvoiceNo() + "#" + dashboardOneData.getInvoiceDate();
    if (sumMap.containsKey(key)) {
        qtyData = sumMap.get(key)
        qtyData.setOriQty(qtyData.getOriQty() + dashboardOneData.getMatQty());
        qtyData.setNewQty(qtyData.getNewQty() + dashboardOneData.getNewQty());
    } else {
        qtyData = new QuantityData();
        qtyData.setOriQty(dashboardOneData.getMatQty());
        qtyData.setNewQty(dashboardOneData.getNewMatQty());
    }
    sumMap.put(key, qtyData);
}

答案 1 :(得分:0)

我不完全理解您的输入数据结构是什么样的,但我会尽我所能。

假设您有一个类似于此的输入数据结构:

@Data //using Lombok @Data annotation
class Data{
    String key;
    Integer[] values;
}

然后你可以这样做:

Map<String, Integer[]> getSummedDataByKey(List<Data> items){
    Map<String, Integer[]> summedData;
    for (Data d : items){
        if (!summedData.contains(d.getKey())){
            summedData.put(d.getKey(), d.getValues());
        }else{
            Integer[] oldValues = summedData.get(d.getKey());
            for (int i = 0; i < oldValues.length; i++){
                oldValues[i] += d.getValues()[i];
            }
            summedData.put(d.getKey(), oldvalues);
        }
    }
    return summedData;
}

答案 2 :(得分:0)

这就是我想到的。

 for (ItemData dashboardOneData : invoiceList) {
            String key = dashboardOneData.getInvoiceNo() + "#" + dashboardOneData.getInvoiceDate();
            QuantityData qtyData = new QuantityData();
            if (sumMap.containsKey(key)) {
                ArrayList<QuantityData> qtyArrayList = new ArrayList<>();
                List<QuantityData> bobs = sumMap.get(key);
                int sum1 = bobs.get(0).getOriQty();
                int sum2 = bobs.get(0).getNewQty();
                sum1 += dashboardOneData.getMatQty();
                sum2 += dashboardOneData.getNewMatQty();
                qtyData.setOriQty(sum1);
                qtyData.setNewQty(sum2);
                qtyArrayList.add(qtyData);
                sumMap.put(key, qtyArrayList);

            } else {
                ArrayList<QuantityData> qtyArrayList = new ArrayList<>();
                qtyData.setOriQty(dashboardOneData.getMatQty());
                qtyData.setNewQty(dashboardOneData.getNewMatQty());
                qtyArrayList.add(qtyData);
                sumMap.put(key, qtyArrayList);
            }
        }