簡體   English   中英

遍歷列表並總結類型相同的所有金額

[英]Iterating through list and summing up all amounts where types are the same

我需要將類型相同的MonthlyExpenses列表中的所有金額MonthlyExpenses

在我的代碼中,我將expensesobligations附加到相同的MonthlyExpenses列表中。

這是我的代碼:

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

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
}

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
}

如果類型相等,我如何檢查List<MonthlyExpenses>並總結這些金額並返回該新列表?

您可以通過使用streamscollectors來實現這一點:

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

該方法這項工作是通過collect由(將它們分組所有費用groupBy )的type和使用作為總結下游收集器。 然后,您使用此映射(在類型和總金額之間)創建具有所需總費用的對象。 下面的類是一個完整的工作應用程序,供您使用:

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

GitHub 上的完整代碼

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM