Java 嵌套映射的复杂迭代
我有一个包含如下值的表:Java 嵌套映射的复杂迭代,java,list,dictionary,Java,List,Dictionary,我有一个包含如下值的表: Map<String, Map<Date, List<product_price_object>>> myTable = new TreeMap<>(); 我必须将每个人(列“姓名”)的日期、汽车、电脑和餐费进行累加。 应该是这样的: Map<String, Map<Date, List<product_price_object>>> myTable = new TreeMap<
Map<String, Map<Date, List<product_price_object>>> myTable = new TreeMap<>();
我必须将每个人(列“姓名”)的日期、汽车、电脑和餐费进行累加。
应该是这样的:
Map<String, Map<Date, List<product_price_object>>> myTable = new TreeMap<>();
我的数据结构如下所示:
Map<String, Map<Date, List<product_price_object>>> myTable = new TreeMap<>();
因为我会用第一排的电脑计算汽车价格
for(Entry<String, Map<Date, List<LogonuserAllowance>>> entry : history.entrySet()) {
String currentUser = entry.getKey();
user_sum_map.put(currentUser, new TreeMap<>());
Map<Date, List<LogonuserAllowance>> month_map = entry.getValue();
for(Entry<Date, List<LogonuserAllowance>> allowance : month_map.entrySet()){
calender.setTime(allowance.getKey());
currentMonth = calender.get(Calendar.MONTH);
user_sum_map.get(currentUser).put(currentMonth, new ArrayList<>());
BigDecimal newValue = null;
BigDecimal previousValue = null;
BigDecimal sum = null;
//iterate through list
for (int i = 0; i < allowance.getValue().size(); i++) {
newValue = allowance.getValue().get(i).getPrice();
previousValue = user_sum_map.get(currentUser).get(currentMonth).get(i-1);
sum = previousValue.add(newValue);
user_sum_map.get(currentUser).get(currentMonth).add(sum);
}
}
}
for(条目:history.entrySet()){
字符串currentUser=entry.getKey();
user_sum_map.put(currentUser,new TreeMap());
Map month_Map=entry.getValue();
对于(入职津贴:月\映射.entrySet()){
calender.setTime(amount.getKey());
currentMonth=Calendar.get(Calendar.MONTH);
user_sum_map.get(currentUser.put)(currentMonth,new ArrayList());
BigDecimal newValue=null;
BigDecimal previousValue=null;
BigDecimal总和=null;
//遍历列表
对于(inti=0;i
我担心,我的解决方案会很复杂,也许会有一些
在您的示例中,为什么Peter在结果表中有两个条目而不是一个?此外,如果数据在数据库中,直接获取结果的查询可能比检索所有数据并在Java中进行操作更有效。请更具体地说,您所说的每个人的累积是什么意思?第二张图片与您想要实现的相混淆。第二张图片显示,我一开始是按月份对每件事进行分组的。例如,在20.4和21.4,我买了两台电脑。20.4的ComputerA成本为2.4,21.4的ComputerB成本为12.4。现在我想知道,5月份我花了多少钱买电脑。