数组中每个类别的总和

Total Sum for each category in an array

我有一个包含多个项目的数组。以下是我的数组结构:

列表 ListCateory (

[0] ListName {
    name = Salary;
    amount = 40000;
    date = 2020-05-01 16:00:00 +0000;
    Category = Main Incomes;
    Type = Income;
},
[1] ListName{
    name = Diff;
    amount =1500;
    date = 2020-05-22 16:00:00 +0000;
    Category= Misc;
    Type = Expense;
},
[2] ListName{
    name = Food;
    amount =100;
    date = 2020-05-18 16:00:00 +0000;
    Category = Grocery;
    Type = Expense;
},
[3] ListName{
    name =  Food;
    amount = 600;
    date = 2020-05-24 16:00:00 +0000;
    Category= Grocery;
    Type = Expense;
}
[4] ListName{
    name = Travel;
    amount =400;
    date = 2020-05-18 16:00:00 +0000;
    Category = Travel;
    Type = Expense;
},

)

下面的代码是获取总收入和支出

` Long totalExpense = 0l; Long totalIncome = 0l;

    for (int i = 0; i < cexpenses.size(); i++) {

        if (cexpenses.get(i).getType().contains("Income")  ) {
           totalIncome += cexpenses.get(i).getAmount();
        } else  if (cexpenses.get(i).getType().equals("Expense")  ) {
            totalExpense += cexpenses.get(i).getAmount();
        }
    }

    
    System.out.println("Expense " + totalExpense);
    System.out.println("Income " + totalIncome);

但我无法获得每个类别的总和 amount.The 所需的输出是一个包含每个类别总计的数组,如下所示:

[0] X-Array {
    category = Main Incomes;
    amount = XXXX;
};
[1] X-Array {
    category = Grocery;
    amount = XXXX;
 };

' 任何帮助将不胜感激

假设您有一份费用清单:

List<Expense> expenses = ...

首先,将它们按类别分组,如下所示:

Map<String, List<Expense>> expensesByCategory =
    expenses.stream().collect(Collectors.groupingBy(Expense::getCategory));

然后,您可以遍历每个类别并执行任何您想要的操作,例如计算每个类别的总和:

for (String category : expensesByCategory.keySet()) {
    List<Expense> categoryExpenses = expensesByCategory.get(category);
    System.out.println(sum(categoryExpenses));
}

试试这个。

enum Category { MainIncomes, Misc, Grocery, Travel }
enum Type { Income, Expense }

record Item(String name, int amount, String date, Category category, Type type) {}

public static void main(String[] args) {
    List<Item> list = List.of(
        new Item("Salary", 40000, "2020-05-01 16:00:00 +0000", Category.MainIncomes, Type.Income),
        new Item("Diff", 1500, "2020-05-22 16:00:00 +0000", Category.Misc, Type.Expense),
        new Item("Food", 100, "2020-05-18 16:00:00 +0000", Category.Grocery, Type.Expense),
        new Item("Food", 600, "2020-05-24 16:00:00 +0000", Category.Grocery, Type.Expense),
        new Item("Travel", 400, "2020-05-18 16:00:00 +0000", Category.Travel, Type.Expense));
    Map<Category, Integer> totalSum = list.stream()
        .collect(Collectors.groupingBy(Item::category,
            Collectors.summingInt(Item::amount)));
    System.out.println(totalSum);
}

输出:

{Grocery=700, MainIncomes=40000, Misc=1500, Travel=400}

您可以使用 class Item 而不是 record Item

public class Item {
    String name;
    int amount;
    String date;
    Category category;
    Type type;

    public Item(String name, int amount, String date, Category category, Type type) {
        this.name = name;
        this.amount = amount;
        this.date = date;
        this.category = category;
        this.type = type;
    }

    public String name() { return name; }
    public int amount() { return amount; }
    public String date() { return date; }
    public Category category() { return category; }
    public Type type() { return type; }
}