0

I have an ArrayList with one String value and two Integer values. I want the sum of integers under the same String value. For example

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

So final list should come as

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

I was able to get the sum using one String and one Integer.

this is what i used for one String and one 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);
    }
}

I tried to do as below but no luck, I was not able to take the sum of the two integers inside the method.

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

    public QuantityData() {
    }
}

Method....

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 Answers 3

2

If I understood your goal correctly, you don't need ArrayLists at all.

Have you tried something like this?

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you. i also found a way using arraylist. but this is less code
0

I don't entirely understand what your input data structure looks like, but I'll do my best.

Let's say you have an input data structure that looks something like this:

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

then you can do something like this:

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;
}

Comments

0

this is what I figured out.

 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);
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.