数组中每个类别的总和
Posted
技术标签:
【中文标题】数组中每个类别的总和【英文标题】:Total Sum for each category in an array 【发布时间】:2021-10-07 17:29:19 【问题描述】:我有一个包含多个项目的数组。以下是我的数组结构:
列出 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; 长总收入 = 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);
但我无法获得每个类别的总金额。所需的输出是一个数组,其中包含每个类别的总数,如下所示:
[0] X-Array
category = Main Incomes;
amount = XXXX;
;
[1] X-Array
category = Grocery;
amount = XXXX;
;
' 任何帮助将不胜感激
【问题讨论】:
【参考方案1】:试试这个。
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;
【讨论】:
这是一种更简洁的解决方案,可以在不创建中间集合的情况下收集总金额。【参考方案2】:假设您有一份费用清单:
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));
【讨论】:
我收到错误错误:找不到符号 System.out.println(sum(categoryExpenses)); ^ 符号:方法 sum(Listsum
函数不存在,我离开了这个函数的实现,因为你已经有一个可以计算总和的代码以上是关于数组中每个类别的总和的主要内容,如果未能解决你的问题,请参考以下文章