Reputation: 3126
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
Upvotes: 1
Views: 4887
Reputation: 311163
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());
}
Upvotes: 3
Reputation: 73
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);
}
}
}
Upvotes: 3
Reputation: 72636
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().
Upvotes: 2