本文共 2057 字,大约阅读时间需要 6 分钟。
有一个测试方法需要实现:
测试方法test2的实现步骤如下:
步骤一:初始化数据
首先,创建一个用于存储历史数据的列表historyList:
historyList初始化为三个map对象,每个map包含两个键值对:levels和counts:
List步骤二:创建临时列表
接下来,创建一个临时列表tempList,用于存储需要更新的数据:
List步骤三:更新历史数据
遍历tempList中的每个map,检查是否有与historyList中相同的levels值:
if(tempList.size() > 0) { for (Mapmap : historyList) { String key = String.valueOf(map.get("levels")); String value = String.valueOf(map.get("counts")); for (Map tempmap : tempList) { String tempkey = String.valueOf(tempmap.get("levels")); String tempvalue = String.valueOf(tempmap.get("counts")); if(tempkey.equals(key)) { value = tempvalue; break; } } map.put("counts", value); } } 步骤四:计算总和
最后,计算tempList中所有counts值的总和:
int upcount = 0; for (Mapmap : tempList) { upcount += Integer.parseInt(String.valueOf(map.get("counts"))); } System.out.println(upcount); 输出结果如下:
System.out.println("tempList: " + tempList.toString()); System.out.println("historyList: " + historyList.toString());
转载地址:http://pzufk.baihongyu.com/