[英]Iterating through list and summing up all amounts where types are the same
I need to add up all amounts in MonthlyExpenses
list where types are equal.我需要将类型相同的MonthlyExpenses
列表中的所有金额MonthlyExpenses
。
In my code I am appending expenses
and obligations
to same list of MonthlyExpenses
.在我的代码中,我将expenses
和obligations
附加到相同的MonthlyExpenses
列表中。
Here is my code:这是我的代码:
List<MonthlyExpenses> monthlyExpenses = new ArrayList<MonthlyExpenses>();
List<Expenses> expenses = getExpenses(borrower);
List<Obligations> obligations = getObligations(borrower);
if(expenses!=null){
monthlyExpenses.addAll(createMonthlyExpenses(expenses));
}
if(obligations!=null){
monthlyExpenses.addAll(createMonthlyObligations(obligations));
}
...
public class MonthlyExpenses {
private String type = null;
private BigDecimal amount = null;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
}
The 1st IF statement
, if executed returns:第1st IF statement
,如果执行返回:
class MonthlyExpenses{
type: FOOD
amount: 150.00
}, class MonthlyExpenses{
type: UTILITIES
amount: 250.00
}, class MonthlyExpenses{
type: TRANSPORTATION
amount: 350.00
}, class MonthlyExpenses{
type: CHILD CARE
amount: 450.00
}, class MonthlyExpenses{
type: CREDIT CARDS
amount: 878.00
}, class MonthlyExpenses{
type: Other
amount: 2888.64
}
The 2nd IF statement
, if executed returns: 2nd IF statement
,如果执行返回:
class MonthlyExpenses{
type: AUTO LOANS
amount: 200.00
}, class MonthlyExpenses{
type: CREDIT CARDS
amount: 300.00
}, class MonthlyExpenses{
type: INSTALLMENT LOANS
amount: 50.00
}, class MonthlyExpenses{
type: ALIMONY/SUPPORT
amount: 75.00
}, class MonthlyExpenses{
type: Other
amount: 10096.87
}
How do I check in List<MonthlyExpenses>
if types are equal and sum up those amounts and return that new list?如果类型相等,我如何检查List<MonthlyExpenses>
并总结这些金额并返回该新列表?
You can achieve this by using streams
and collectors
:您可以通过使用streams
和collectors
来实现这一点:
private static List<MonthlyExpense> createMonthlyExpenses(List<MonthlyExpense> expenses) {
Map<String, Double> sums = expenses.stream()
.collect(Collectors.groupingBy(
MonthlyExpense::getType,
Collectors.summingDouble(MonthlyExpense::getAmount))
);
return sums.entrySet().stream()
.map(entry -> new MonthlyExpense(entry.getKey(), entry.getValue()))
.sorted(Comparator.comparing(MonthlyExpense::getType))
.collect(Collectors.toList());
}
The way this work is by using collect
on all expenses grouping them by ( groupBy
) their type
and using as a summing downstream collector.该方法这项工作是通过collect
由(将它们分组所有费用groupBy
)的type
和使用作为总结下游收集器。 Then, you use this map (between types and total amounts) to create the objects with total expenses that you require.然后,您使用此映射(在类型和总金额之间)创建具有所需总费用的对象。 The following class is a full working application of this in action, for you to play with:下面的类是一个完整的工作应用程序,供您使用:
import java.util.*;
import java.util.stream.Collectors;
public class FinancialCalculator {
static class MonthlyExpense {
private String type;
private double amount;
public MonthlyExpense(String type, double amount) {
this.type = type;
this.amount = amount;
}
public String getType() { return type; }
public double getAmount() { return amount; }
public String toString() { return String.format("%s: %.2f", type, amount); }
}
private static List<MonthlyExpense> createMonthlyExpenses(List<MonthlyExpense> expenses) {
Map<String, Double> sums = expenses.stream()
.collect(Collectors.groupingBy(
MonthlyExpense::getType,
Collectors.summingDouble(MonthlyExpense::getAmount))
);
return sums.entrySet().stream()
.map(entry -> new MonthlyExpense(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
}
public static void main(String[] args) {
MonthlyExpense[] expenses = new MonthlyExpense[] {
new MonthlyExpense("UTILITIES", 75),
new MonthlyExpense("CREDIT", 1000),
new MonthlyExpense("UTILITIES", 50),
new MonthlyExpense("CREDIT", 2000),
new MonthlyExpense("UTILITIES", 150),
new MonthlyExpense("CAR", 344)
};
System.out.println(createMonthlyExpenses(Arrays.asList(expenses)));
}
}
Complete code on GitHub GitHub 上的完整代码
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.