我应该如何在foreach循环中将密钥设置为列表,然后重设上述列表,以使其在不清除的情况下获取其他值。
这是我的代码:
public static Map<String, List<Integer>> trend(Map<String, List<Integer>> companies){
List<Integer> list = new LinkedList<>();
List<Integer> list2 = new LinkedList<>();
Map<String, List<Integer>> finalMap = new HashMap<>();
int difference = 0;
for(Map.Entry<String, List<Integer>> entry : companies.entrySet()) {
list = entry.getValue();
for(int i = 0; i <= list.size() - 1; i++) {
if(i < list.size() - 1) {
difference = list.get(i+1) - list.get(i);
list2.add(difference);
}
}
finalMap.put(entry.getKey(), list2);
list.clear();
list2.clear();
}
return finalMap;
}
答案 0 :(得分:0)
如果您不想清除该列表,只需删除对该列表的list.clear()调用即可。