简体   繁体   English

从HashMaps的HashMap中检索对象的问题

[英]Issues with retrieving objects from a HashMap of HashMaps

I am writing code that iterates recursively through an XML file and populates a HashMap of HashMaps. 我正在编写代码,通过XML文件递归迭代并填充HashMaps的HashMap。 I have been able to populate the hashmap and it looks OK. 我已经能够填充hashmap,看起来没问题。 However, when I run this command 但是,当我运行此命令时

 System.out.println(map.containsKey("Mary"));

Its always false. 它总是错误的。 Really not sure why it always returns false. 真的不确定为什么它总是返回false。 I have also posted my recursive code and contents of the hashmap after 我之后也发布了我的递归代码和hashmap的内容

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}}}}}

Recursive code is: 递归代码是:

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());
                }


            }
        }       
    }

}   

Thanks! 谢谢!

As requested by the OP- and since the Discussion in comments solved his issue - I am wrapping it as answer - for future readers: 根据OP的要求,以及评论中的讨论解决了他的问题 - 我将其作为答案包装 - 为未来的读者:

First, you should check if your keys are indeed String objects. 首先,您应该检查您的密钥是否确实是String对象。 You can do it by adding the line: 您可以通过添加以下行来完成:

System.out.println(map.keySet().iterator().next().getClass());

Next, after we have established your keys are indeed String s, we want to check if you have invisible characters or not needed white spaces, we will do it by adding the following lines: 接下来,在我们确定您的密钥确实是String ,我们想要检查您是否有不可见的字符或不需要的空格,我们将通过添加以下行来完成:

String s = (String)map.keySet().iterator().next(); 
System.out.println("val=" + s + " length=" + s.length());

If there is indeed invisible characters - we will know it since the length will not match. 如果确实存在不可见的字符 - 我们将知道它,因为长度不匹配。

If it is indeed the reason [and it is, as the OP said], you will have to process your strings when reading the XML to exclude this characters. 如果确实是[正如OP所说的那样]的原因,那么在读取XML时必须处理字符串以排除这些字符。

Also, as a side note - I'd recommend avoiding using raw types, and will suggest you to use generic types whenever it is possible. 另外,作为附注 - 我建议避免使用原始类型,并建议您尽可能使用泛型类型 It will give you both more readable code, and both type safety ! 它将为您提供更易读的代码,并且类型安全
If you want unlimited nesting, then I would have used the composite design pattern to achieve it. 如果你想要无限制的嵌套,那么我会使用复合设计模式来实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM