Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更新地图_Java_Collections - Fatal编程技术网

Java 更新地图

Java 更新地图,java,collections,Java,Collections,我有一张这样的地图: private Map<String, List<List<String>>> someMap; private List<List<String>> someList1; private List<String> someList2; // store the current payment info reportDataSubList = new ArrayList<String>()

我有一张这样的地图:

private Map<String, List<List<String>>> someMap;
private List<List<String>> someList1;
private List<String> someList2;
// store the current payment info
reportDataSubList = new ArrayList<String>();
reportDataSubList.add(clientCode);
//...                                   
reportDataList = reportData.get(clientCode);
if(reportDataList == null){
    reportDataList = new ArrayList<List<String>>();
    reportData.put(clientCode, reportDataList);
}
reportDataList.add(reportDataSubList);

请注意,列表在添加到地图后会变得清晰,之后会填充

例如,我有两个键“Apple”和“Orange”,其中有一些值。循环之后,我只得到橙色。上一个键被覆盖


编辑:在循环的每次迭代中,都会填充一个列表。循环结束时,它将被添加到映射中,添加后,列表将被清除()

有什么建议吗?谢谢


代码:[根据要求..所以请不要责怪我发布了..]

我怀疑您是否想反复添加相同的列表。您需要为每个新键创建新的列表实例

编辑:您的someKey变量是否有任何变化?您是否已使用调试器逐步进入代码并查看发生了什么

编辑:像这样做:

private Map<String, List<List<String>>> someMap;
private List<List<String>> someList1;
private List<String> someList2;
// store the current payment info
reportDataSubList = new ArrayList<String>();
reportDataSubList.add(clientCode);
//...                                   
reportDataList = reportData.get(clientCode);
if(reportDataList == null){
    reportDataList = new ArrayList<List<String>>();
    reportData.put(clientCode, reportDataList);
}
reportDataList.add(reportDataSubList);
//存储当前付款信息
reportDataSubList=newArrayList();
reportdatapublist.add(clientCode);
//...                                   
reportDataList=reportData.get(clientCode);
如果(reportDataList==null){
reportDataList=新的ArrayList();
reportData.put(clientCode,reportDataList);
}
reportDataList.add(reportdatapublist);

但我不确定您使用的数据结构是否适合此任务。正确的实体对象,甚至是XML,将是一个非常匹配的IMHO。

我怀疑您是否希望反复添加相同的列表。您需要为每个新键创建新的列表实例

编辑:您的someKey变量是否有任何变化?您是否已使用调试器逐步进入代码并查看发生了什么

编辑:像这样做:

private Map<String, List<List<String>>> someMap;
private List<List<String>> someList1;
private List<String> someList2;
// store the current payment info
reportDataSubList = new ArrayList<String>();
reportDataSubList.add(clientCode);
//...                                   
reportDataList = reportData.get(clientCode);
if(reportDataList == null){
    reportDataList = new ArrayList<List<String>>();
    reportData.put(clientCode, reportDataList);
}
reportDataList.add(reportDataSubList);
//存储当前付款信息
reportDataSubList=newArrayList();
reportdatapublist.add(clientCode);
//...                                   
reportDataList=reportData.get(clientCode);
如果(reportDataList==null){
reportDataList=新的ArrayList();
reportData.put(clientCode,reportDataList);
}
reportDataList.add(reportdatapublist);

但我不确定您使用的数据结构是否适合此任务。正确的实体对象,甚至XML,都将与IMHO相得益彰。

根据您对Lucero问题的评论:

您是否反复使用同一个列表实例,即

 someList1.add(someList2);
 someMap.put(someKey, someList1);
 someList1.clear()
 someList1.add(stuff)
 someMap.put(someOtherKey, someList1);
这将不起作用,因为someList1.clear()将清除someMap中的列表。您需要每次创建一个新的列表实例:

 someList1.add(someList2);
 someMap.put(someKey, someList1);
 someList1 = new List<...>();
 someList1.add(stuff)
 someMap.put(someOtherKey, someList1);
someList1.add(someList2);
someMap.put(someKey,someList1);
someList1=新列表();
someList1.添加(内容)
someMap.put(someOtherKey,someList1);

根据您对卢塞罗问题的评论:

您是否反复使用同一个列表实例,即

 someList1.add(someList2);
 someMap.put(someKey, someList1);
 someList1.clear()
 someList1.add(stuff)
 someMap.put(someOtherKey, someList1);
这将不起作用,因为someList1.clear()将清除someMap中的列表。您需要每次创建一个新的列表实例:

 someList1.add(someList2);
 someMap.put(someKey, someList1);
 someList1 = new List<...>();
 someList1.add(stuff)
 someMap.put(someOtherKey, someList1);
someList1.add(someList2);
someMap.put(someKey,someList1);
someList1=新列表();
someList1.添加(内容)
someMap.put(someOtherKey,someList1);

通读您的代码:

你所描述的真的不应该发生。您应该真正使用调试器,逐行检查这行代码是否真的在做您认为它在做的事情(第57行):

很可能是在您到达第二行之前触发异常,或者出现意外的重复数据等

此外,我刚才所说的仍然有效。在ln 158,而不是执行以下操作:

reportDataSubList.clear();

但是做

a.clear();
那么很清楚为什么列表是空的。您所有的条目都是相同的参考

宣布

List a = new ArrayList();

您正在创建列表对象的新实例,对该对象所做的任何操作都不会影响您已经创建的对象。

请通读您的代码:

你所描述的真的不应该发生。您应该真正使用调试器,逐行检查这行代码是否真的在做您认为它在做的事情(第57行):

很可能是在到达第二行之前触发了异常,或者存在意外的重复数据等

此外,我刚才所说的仍然有效。在ln 158,而不是执行以下操作:

reportDataSubList.clear();

但是做

a.clear();
那么很清楚为什么列表是空的。您所有的条目都是相同的参考

宣布

List a = new ArrayList();

您正在创建一个列表对象的新实例,您对该对象所做的任何操作都不会影响您已经创建的对象。

FYI:标题中不需要[JAVA],这就是为什么我们有Tag的原因。我认为您需要包括您省略的位。我怀疑您混淆了键和值,但代码或问题中没有足够的信息可以确定。在循环的每次迭代中,列表都会被填充。循环结束后,它被添加到映射中,添加后列表将被清除()。您可以发布更多代码吗?你现在提出问题的方式,循环和列表在循环之后被清除,实在是太模糊了。循环从何处开始,在何处结束,哪些列表被清除,等等。我对代码做了一些修改来说明我的观点。请看这里:但是我可以建议(不要责怪!)你有一位经验丰富的程序员来指导你一点吗?仅供参考:标题中没有[JAVA]的必要,这就是为什么我们有tagsI,我认为你需要包括你省略的部分。我怀疑您混淆了键和值,但代码或问题中没有足够的信息可以确定。在循环的每次迭代中,列表都会被填充。循环结束后,它被添加到映射中,添加后列表将被清除()。您可以发布更多代码吗?你现在提出问题的方式,循环和列表在循环之后被清除,实在是太模糊了。循环从何处开始,在何处结束,哪些列表被清除,等等。我对代码做了一些修改来说明我的观点。请看这里:但是我可以建议(不要责备我吗