lambda groupingBy Object returns 复杂 JSON
lambda groupingBy Object returns complex JSON
最近开始在 Java 中使用 lambda。
Map<Category, Double> lMap = rptData.stream().collect(Collectors.groupingBy(Expense::getCategory,
Collectors.summingDouble(j -> j.getFltAmt().doubleValue())));
以上代码returns映射,其中returns输出休息服务为JSON。
{"Category(intCatId=10013, strCatName=Home Maint)":4134.99,"Category(intCatId=10019, strCatName=Lease Breakage)":2600.0,"Category(intCatId=10011, strCatName=Utility Bill)":2067.76,"Category(intCatId=10010, strCatName=Fuel)":1018.77,"Category(intCatId=10012, strCatName=Entertainment)":192.4,"Category(intCatId=6, strCatName=Shopping)":1528.25,"Category(intCatId=4, strCatName=Medicine)":128.55,"Category(intCatId=10021, strCatName=Interest)":24.61,"Category(intCatId=3, strCatName=Phone)":539.09,"Category(intCatId=10020, strCatName=Movers)":1350.0,"Category(intCatId=5, strCatName=Grocery)":3519.83,"Category(intCatId=8, strCatName=School)":311.0,"Category(intCatId=10009, strCatName=Insurance)":1117.75,"Category(intCatId=7, strCatName=Eating Out)":614.22,"Category(intCatId=1, strCatName=Vehicle)":2843.58,"Category(intCatId=10018, strCatName=Courier)":22.65,"Category(intCatId=2, strCatName=Rent)":16506.95,"TotCount":13.0,"Category(intCatId=10017, strCatName=Travel)":800.42,"Category(intCatId=10015, strCatName=Donation)":326.0}
在上面的 JSON 中,我将整个类别作为映射中的键,而我只查找 intCatId。所以输出会像
{"10013":4134.99,"10019":...}
有什么办法吗?
示例:
域
费用{
双飞
日期 dtDate
(公式"month(dtDate) - year(dtDate)")
字符串月年
(通过 intCatId 加入)
类别类别
...
}
类别{
整型 intCatId
字符串猫名
...
}
我想按年份计算(按类别和月份列出的所有费用总和)的平均值。
在上面的示例中,rptData 是包含总和(fltAmt)、monthYear 和 Category 的列表。
由于您的地图包含作为 Category 对象的键,所以输出中也存在同样的内容 JSON,因为您使用的序列化程序正在将 Category 对象转换为 JSON ......
您可以为类别对象编写一个自定义序列化程序,它将 return 类别 ID 作为其对应的 JSON 值.....将需要有关您正在使用的序列化程序的更多详细信息进一步展示如何创建自定义序列化程序。
或者另一种方法是更改地图类型并使用 CategoryId(String type) 作为键....
根据 Shamseer 评论编辑
你几乎做到了。
您只需将 Map<Category, Double>
更改为 Map<Integer, Double>
,然后在分组函数中,将 Expense::getCategory
替换为另一个 lambda e -> e.getCategory().getIntCatId()
(有一个 getter 代表 intCatId,对吗?)
这将产生以下输出:
{10013:4134.99,10019:...}
最近开始在 Java 中使用 lambda。
Map<Category, Double> lMap = rptData.stream().collect(Collectors.groupingBy(Expense::getCategory,
Collectors.summingDouble(j -> j.getFltAmt().doubleValue())));
以上代码returns映射,其中returns输出休息服务为JSON。
{"Category(intCatId=10013, strCatName=Home Maint)":4134.99,"Category(intCatId=10019, strCatName=Lease Breakage)":2600.0,"Category(intCatId=10011, strCatName=Utility Bill)":2067.76,"Category(intCatId=10010, strCatName=Fuel)":1018.77,"Category(intCatId=10012, strCatName=Entertainment)":192.4,"Category(intCatId=6, strCatName=Shopping)":1528.25,"Category(intCatId=4, strCatName=Medicine)":128.55,"Category(intCatId=10021, strCatName=Interest)":24.61,"Category(intCatId=3, strCatName=Phone)":539.09,"Category(intCatId=10020, strCatName=Movers)":1350.0,"Category(intCatId=5, strCatName=Grocery)":3519.83,"Category(intCatId=8, strCatName=School)":311.0,"Category(intCatId=10009, strCatName=Insurance)":1117.75,"Category(intCatId=7, strCatName=Eating Out)":614.22,"Category(intCatId=1, strCatName=Vehicle)":2843.58,"Category(intCatId=10018, strCatName=Courier)":22.65,"Category(intCatId=2, strCatName=Rent)":16506.95,"TotCount":13.0,"Category(intCatId=10017, strCatName=Travel)":800.42,"Category(intCatId=10015, strCatName=Donation)":326.0}
在上面的 JSON 中,我将整个类别作为映射中的键,而我只查找 intCatId。所以输出会像
{"10013":4134.99,"10019":...}
有什么办法吗?
示例:
域
费用{ 双飞 日期 dtDate (公式"month(dtDate) - year(dtDate)") 字符串月年 (通过 intCatId 加入) 类别类别 ... }
类别{ 整型 intCatId 字符串猫名 ... }
我想按年份计算(按类别和月份列出的所有费用总和)的平均值。
在上面的示例中,rptData 是包含总和(fltAmt)、monthYear 和 Category 的列表。
由于您的地图包含作为 Category 对象的键,所以输出中也存在同样的内容 JSON,因为您使用的序列化程序正在将 Category 对象转换为 JSON ......
您可以为类别对象编写一个自定义序列化程序,它将 return 类别 ID 作为其对应的 JSON 值.....将需要有关您正在使用的序列化程序的更多详细信息进一步展示如何创建自定义序列化程序。
或者另一种方法是更改地图类型并使用 CategoryId(String type) 作为键....
根据 Shamseer 评论编辑
你几乎做到了。
您只需将 Map<Category, Double>
更改为 Map<Integer, Double>
,然后在分组函数中,将 Expense::getCategory
替换为另一个 lambda e -> e.getCategory().getIntCatId()
(有一个 getter 代表 intCatId,对吗?)
这将产生以下输出:
{10013:4134.99,10019:...}