简体   繁体   English

如何使静态递归方法返回HashMap?

[英]How do I make a static recursive method return a HashMap?

I am in the proces of creating a helper method that will allow me to traverse an XML document and put each element in a data HashMap and then return it. 我正在创建一个帮助器方法的过程,它允许我遍历XML文档并将每个元素放在数据HashMap中然后返回它。 Here is the method I've been working on : 这是我一直在研究的方法:

private static HashMap<String, String> traverseNodes( Node n, HashMap<String,String> data ) 
{
    NodeList children = n.getChildNodes();
    if( children != null ) {
      for( int i = 0; i < children.getLength(); i++ ) {
        Node childNode = children.item( i );

        String nodeName = childNode.getNodeName();
        if(childNode instanceof Element)
        {
            data.put(nodeName, getStringByTag(nodeName, (Element)childNode));
            Log.d("traversal", childNode.getNodeName() + " was saved in hashmap");
        }


        else
            Log.d("traversal", childNode.getNodeName() + " Is not an Element type");

        System.out.println( "node name = " + childNode.getNodeName() );
        traverseNodes( childNode, data );
      }
    }

    return data;

}

I tried running the example and although I get messages saying that "childNode.getNodeName() + "was saved in hashmap". but the HashMap returned is empty. 我尝试运行该示例,虽然我收到消息说“childNode.getNodeName()+”已保存在hashmap中。但返回的HashMap为空。

What am I doing wrong? 我究竟做错了什么?

EDIT! 编辑! Bonus Question : I edited the code to reflect the changes suggested. 奖金问题:我编辑了代码以反映建议的更改。 It seems, however, that the method itself doesn't save the values of my XML Document. 但是,似乎该方法本身不保存我的XML文档的值。 Are there any problems with the logic in the method ? 方法中的逻辑有问题吗?

You're creating a new map on each call. 您正在为每次通话创建新地图。 You need to pass the existing map into the recursive call. 您需要将现有的地图传递递归调用。 It should probably look like this: 它应该看起来像这样:

private static Map<String, String> traverseNodes(Node n) {
    Map<String, String> map = new HashMap<String, String>();
    traverseNodesRecurse(n, map);
    return map;
}

private static void traverseNodesRecurse(Node n, Map<String, String> map) {
    // Logic as per question
    // Recursive call (in the loop, etc)
    traverseNodes(childNode, map);

    // No need for a return statement
}

我会说将HashMap保留在方法之外并将其作为参数传递 -

private static HashMap<String, String> traverseNodes( Node n, HashMap<String, String> data  ) 

I think you need something like that 我想你需要这样的东西

private static HashMap<String, String> traverseNodes( Node n ) {
    HashMap<String, String> data = new HashMap<String, String>();
    traverseNodes( n, data );
    return data;
}

private static void traverseNodes( Node n, HashMap<String, String> data ) {
    // Here is your recursive code that do data.put(key, value)
}
private static HashMap<String, String> data = new HashMap<String, String>();

private static traverseNodes( Node n) 
{
    NodeList children = n.getChildNodes();
    if( children != null ) {
      for( int i = 0; i < children.getLength(); i++ ) {


        Node childNode = children.item( i );

        if(childNode instanceof Element)
        {

            String nodeName = childNode.getNodeName();
            data.put(nodeName, getStringByTag(nodeName, (Element)childNode));
            Log.d("traversal", childNode.getNodeName() + " was saved in hashmap");
        }


        else
            Log.d("traversal", childNode.getNodeName() + " Is not an Element type");

        System.out.println( "node name = " + childNode.getNodeName() );
        traverseNodes( childNode);
      }
    }

    return data;

}

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

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