我正在编写通过 XML 文件递归迭代并填充 HashMap 的 HashMap 的代码。我已经能够填充哈希图,它看起来不错。但是,当我运行此命令时
System.out.println(map.containsKey("Mary"));
它总是假的。真的不知道为什么它总是返回假。之后我还发布了我的递归代码和哈希图的内容
map.toString()
map is { Mary
={24
={established
={western
={Profile=m}}, torn-down
={western
={Profile=b}, eastern
={Profile=m}}}, 44
={established
={western
={Profile=g}, eastern
={Profile=s}}, torn-down
={western
={Profile=j}, western
={Profile=f}}}}, Martha
={24
={established
={western
={Profile=a}}, torn-down
={western
={Profile=b}, eastern
={Profile=n}}}, 44
={established
={western
={Profile=s}, eastern
={Profile=j}}, torn-down
={western
={Profile=k}, eastern
={Profile=g}}}}}
递归代码是:
NodeList l = doc.getElementsByTagName("Branches");
Node n = l.item(0);
map = new HashMap();
recurse(n, map);
private void recurse(Node n, HashMap map){
if (n.hasChildNodes()){
NodeList nl = n.getChildNodes();
for(int i= 0; i< nl.getLength(); i++){
Node node = nl.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE){
if (!node.getNodeName().equals("Profile") ){
map.put(node.getFirstChild().getNodeValue(), new HashMap());
recurse(node, (HashMap)map.get(node.getFirstChild().getNodeValue()));
}
else {
map.put("Profile", node.getFirstChild().getNodeValue());
}
}
}
}
}
谢谢!