最近开始在 Java 中使用 lambda。
Map<Category, Double> lMap = rptData.stream().collect(Collectors.groupingBy(Expense::getCategory,
Collectors.summingDouble(j -> j.getFltAmt().doubleValue())));
上面的代码返回 map,它以 JSON 形式返回 out of rest 服务。
{"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 中,我将整个 Category 作为 map 中的键,而我只是在寻找 intCatId。所以输出会像
{"10013":4134.99,"10019":...}
有什么办法吗?
例子:
领域
费用{ Double fltAmt Date dtDate (formula "month(dtDate) - year(dtDate)") String monthYear (joinedby intCatId) Category category ... }
类别{ int intCatId String CatName ... }
我想按月年获得(按类别和月年的所有费用金额的总和)的平均值。
在上面的示例中,rptData 是带有 sum(fltAmt)、monthYear 和 Category 的列表。