即使清除后,我如何才能使列表仍然包含其元素



我应该如何设置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;
}

如果您不想清除该列表,只需删除该列表的call to list.clear((。

最新更新