Total Sum for each category in an array

Solution for Total Sum for each category in an array
is Given Below:

I have an array that contains multiple items. The following is my array structure:

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

)

Below code am getting total income and 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);

But am unable to get each Category total sum amount.The desired output is an array that will contain totals for each category like below:

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


any help will be appreciate

Try this.

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

output:

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

You can use class Item instead of 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; }
}

Lets say you have a list of expenses:

List<Expense> expenses = ...

Firstly, group them by category, like this:

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

Then, you can iterate over each category and do whatever you want, for example, calculate sum for each category:

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