嗨伙计我已经看了下面提到问题的答案,但它没有帮助,因为他们给出静态值的答案,而我需要从动态approch获得价值
How to put/get values into/from Nested HashMap
Getting value from Nested HashMap into another Map
How do I access nested HashMaps in Java?
这是我的情景:
这是我从HashMap添加和检索数据的类: -
HashMap<Integer, String> xmlQueryNode = new HashMap<Integer, String>();
HashMap<Integer, HashMap<Integer, String>> splunkHashMap = new HashMap<Integer, HashMap<Integer, String>>();
id = 0;
xmlQueryNode.put(0, "F0name");
xmlQueryNode.put(1, "L0name");
xmlQueryNode.put(2, "M0name");
splunkHashMap.put(id, xmlQueryNode);
id = 1;
xmlQueryNode.put(0, "F1name");
xmlQueryNode.put(1, "L1name");
xmlQueryNode.put(2, "M1name");
splunkHashMap.put(id, xmlQueryNode);
id = 2;
xmlQueryNode.put(0, "F2name");
xmlQueryNode.put(1, "L2name");
xmlQueryNode.put(2, "M2name");
splunkHashMap.put(id, xmlQueryNode);
id = 3;
xmlQueryNode.put(0, "F3name");
xmlQueryNode.put(1, "L3name");
xmlQueryNode.put(2, "M3name");
splunkHashMap.put(id, xmlQueryNode);
for (Integer key : splunkHashMap.keySet()) {
HashMap<Integer, String> temp = new HashMap<Integer, String>();
//System.out.println(key);
temp = otsdbQueryData.get(key);
for(Integer key1: temp.keySet()){
System.out.println(temp.get(key1));
}
}
此代码的问题在于我得到所有4个条目的输出: -
F3name
L3name
M3name
F3name
L3name
M3name
F3name
L3name
M3name
F3name
L3name
M3name
任何人都可以指出我在哪里犯错误,因为我的哈希映射被最后插入的hashmap覆盖了吗?
答案 0 :(得分:2)
在放入内部hashmap之后,您需要重新初始化该地图。
xmlQueryNode = new HashMap<Integer,String>();
xmlQueryNode.put(0, "F1name");
xmlQueryNode.put(1, "L1name");
xmlQueryNode.put(2, "M1name");
否则它将更新相同的参考。
请检查更新的代码。
int id =0;
HashMap<Integer, String> xmlQueryNode = new HashMap<Integer, String>();
HashMap<Integer, HashMap<Integer, String>> splunkHashMap = new HashMap<Integer, HashMap<Integer, String>>();
id = 0;
xmlQueryNode.put(0, "F0name");
xmlQueryNode.put(1, "L0name");
xmlQueryNode.put(2, "M0name");
splunkHashMap.put(id, xmlQueryNode);
xmlQueryNode = new HashMap<Integer, String>();
id = 1;
xmlQueryNode.put(0, "F1name");
xmlQueryNode.put(1, "L1name");
xmlQueryNode.put(2, "M1name");
splunkHashMap.put(id, xmlQueryNode);
xmlQueryNode = new HashMap<Integer, String>();
id = 2;
xmlQueryNode.put(0, "F2name");
xmlQueryNode.put(1, "L2name");
xmlQueryNode.put(2, "M2name");
splunkHashMap.put(id, xmlQueryNode);
xmlQueryNode = new HashMap<Integer, String>();
id = 3;
xmlQueryNode.put(0, "F3name");
xmlQueryNode.put(1, "L3name");
xmlQueryNode.put(2, "M3name");
splunkHashMap.put(id, xmlQueryNode);
for (Integer key : splunkHashMap.keySet()) {
HashMap<Integer, String> temp = new HashMap<Integer, String>();
//System.out.println(key);
temp = splunkHashMap.get(key);
for(Integer key1: temp.keySet()){
System.out.println(temp.get(key1));
}
}
答案 1 :(得分:0)
xmlQueryNode引用被反复覆盖。检查以下代码。
HashMap<Integer, String> xmlQueryNode = new HashMap<Integer, String>();
HashMap<Integer, HashMap<Integer, String>> splunkHashMap = new HashMap<Integer, HashMap<Integer, String>>();
id = 0;
xmlQueryNode.put(0, "F0name");
xmlQueryNode.put(1, "L0name");
xmlQueryNode.put(2, "M0name");
splunkHashMap.put(id, xmlQueryNode);
id = 1;
xmlQueryNode = new HashMap<Integer,String>();
xmlQueryNode.put(0, "F1name");
xmlQueryNode.put(1, "L1name");
xmlQueryNode.put(2, "M1name");
splunkHashMap.put(id, xmlQueryNode);
id = 2;
xmlQueryNode = new HashMap<Integer,String>();
xmlQueryNode.put(0, "F2name");
xmlQueryNode.put(1, "L2name");
xmlQueryNode.put(2, "M2name");
splunkHashMap.put(id, xmlQueryNode);
id = 3;
xmlQueryNode = new HashMap<Integer,String>();
xmlQueryNode.put(0, "F3name");
xmlQueryNode.put(1, "L3name");
xmlQueryNode.put(2, "M3name");
splunkHashMap.put(id, xmlQueryNode);
for (Integer key : splunkHashMap.keySet()) {
HashMap<Integer, String> temp = new HashMap<Integer, String>();
//System.out.println(key);
temp = otsdbQueryData.get(key);
for(Integer key1: temp.keySet()){
System.out.println(temp.get(key1));
}
}