1

I have an ArrayList which having many objects. I want to do sum of values of the same property name.

Examples Data in Array List which is Objects of ProfitAndLossDataDO

      "Michel" , 5000
      "Alex" , 5000
      "Edvin" , 4000
      "Sosha" , 3000
      "Michel" , 2000
      "Alex" , 3000

And the Result Would be (Sum of values when person are same)-

      "Michel" ,7000
      "Alex" ,  8000
      "Edvin" , 4000
      "Sosha" , 3000

The Logic class ProfitAndLossDataDO

public class ProfitAndLossDataDO {

String ledgerName;
double ledgerAmount;

public ProfitAndLossDataDO() {
}

public ProfitAndLossDataDO(String ledgerName, double ledgerAmount) {
    this.ledgerName = ledgerName;
    this.ledgerAmount = ledgerAmount;
}



public String getLedgerName() {
    return ledgerName;
}

public void setLedgerName(String ledgerName) {
    this.ledgerName = ledgerName;
}

public double getLedgerAmount() {
    return ledgerAmount;
}

public void setLedgerAmount(double ledgerAmount) {
    this.ledgerAmount = ledgerAmount;
}


@Override
public String toString()
{
            return ("ledger name : "+this.getLedgerName()+" Amount : "+this.getLedgerAmount());
}
}

Thanks

1
  • @Eran not able to find sum Commented Sep 6, 2014 at 10:10

3 Answers 3

3

I'd iterate the list and collect the results in a map:

public static List<ProfitAndLossDataDO> sumPerLedgerName
    (List<ProfitAndLossDataDO> list) {

    Map<String, ProfitAndLossDataDO> map = new HashMap<>();

    for (ProfitAndLossDataDO p : list) {
        String name = p.getLedgerName();
        ProfitAndLossDataDO sum = map.get(name);
        if (sum == null) {
            sum = new ProfitAndLossDataDO(name, 0.0);
            map.put(name, sum);
        }
        sum.setLedgerAmount(sum.getLedgerAmount() + p.getLedgerAmount());
    }
    return new ArrayList<>(map.values());
}

EDIT:

Java 8's enhancements to the Map interface allow us to implement this method in a slightly more elegant way, with the annoying if block in the middle:

public static List<ProfitAndLossDataDO> sumPerLedgerName
    (List<ProfitAndLossDataDO> list) {

    Map<String, ProfitAndLossDataDO> map = new HashMap<>();

    for (ProfitAndLossDataDO p : list) {
        String name = p.getLedgerName();
        ProfitAndLossDataDO sum = map.computeIfAbsent(name, n -> new ProfitAndLossDataDO(n, 0.0));
        sum.setLedgerAmount(sum.getLedgerAmount() + p.getLedgerAmount());
    }
    return new ArrayList<>(map.values());
}
Sign up to request clarification or add additional context in comments.

Comments

3

You can use a Hash Map as aleroot suggested. Use name as the key and sum as the value. Whenever you need to insert value just check such entry exists with the same key and update it.

public class ProfitAndLossDataDO {

HashMap<String, Double> data = new HashMap<String, Double>();

public ProfitAndLossDataDO() {

}

public void updateLedger(String ledgerName, double ledgerAmount) {
    double temp;
    if(data.containsKey(ledgerName)) {
        temp = data.get(ledgerName)+ledgerAmount;
        data.put(ledgerName,temp);
    }
    else {
        data.put(ledgerName,ledgerAmount);
    }

}
}

1 Comment

If you want to maintain insertion order use LinkedHashMap.
2

Why you don't just put them in an Map based collection with key/value pairs ?

I think that in this case a Map based collection is more suitable, for the data you have and the context in which you want to use it.

If you need to preserve the single items inside the list, you could use a linked list as Value of the map .

For example :

HashMap<String, LinkedList<Double>> cache = new HashMap<String, LinkedList<Double>>();

and SUM each element of the value LinkedList to find the total amount ... You could even wrap the LinkedList of doubles into a container object with helper functions like getTotal() or Sum().

2 Comments

okey if i used hash based it only insert uniue value. but how can i get some of these value. show some small example
Take a look at the updated answer where you can find a more in detail explanation

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.