Search code examples
javahashmapsnakeyaml

Deeply nested hashmaps in Java


I have used this example to Accessing Deeply nested HashMaps in Java build the data structure to store node names and properties.

Here is the updated code:

class NestedMap {

private final HashMap<String, NestedMap> child;

private Map<String, Object> value = new HashMap<>();

public NestedMap() {
    child = new HashMap<>();
    setValue(null);
}

public boolean hasChild(String k) {
    return this.child.containsKey(k);
}

public NestedMap getChild(String k) {
    return this.child.get(k);
}

public void makeChild(String k) {
    this.child.put(k, new NestedMap());
}

public Map<String, Object> getValue() {
    return value;
}

public void setValue(Map<String, Object> value) {
    this.value = value;
}

}

And my usage example:

    class NestedMapIllustration {
        public static void main(String[] args) {

        NestedMap m = new NestedMap();

        m.makeChild("de");
        m.getChild("de").makeChild("content");
        m.getChild("de").getChild("content").makeChild("00");
        m.getChild("de").getChild("content").makeChild("0");
        m.getChild("de").getChild("content").makeChild("1");
        m.getChild("de").getChild("content").makeChild("01");
        m.getChild("de").getChild("content").getChild("01").makeChild("fieldsets");
        m.getChild("de").getChild("content").getChild("01").getChild("fieldsets").makeChild("0");
        m.getChild("de").getChild("content").getChild("01").getChild("fieldsets").getChild("0").makeChild("fields");
        m.getChild("de").getChild("content").getChild("01").getChild("fieldsets").getChild("0").getChild("fields").makeChild("0");
        Map<String, Object> properties = new HashMap<>();
        properties.put("key", "value");
        properties.put("key2", "value");
        m.getChild("de").getChild("content").getChild("01").getChild("fieldsets").getChild("0").getChild("fields").setValue(properties);
}

Instead of creating a new object each value I would like to always create a new HashMap where I can store the node properties.

I receive my data structure by visiting nodes in the JCR datastore and extracting their values and properties. This is how my resulting data structure should look in the output yaml file:

enter image description here

How can I do that more efficiently?


Solution

  • Solution to the problem using recursion

    public HashMap<String,Object> nestedMap(Node node) {
    
               HashMap<String, Object> map = new LinkedHashMap<>();
    
               PropertyIterator pi;
                try {
                    pi = node.getProperties();
                    //Get properties for the root node
                       while(pi.hasNext())
                        {
                           Property p = pi.nextProperty();
                           String name = p.getName();
                           String val = p.getString();
    
                           map.put(name,val);
    
                        }//end of while for properties of root node
                } catch (RepositoryException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
    
               Iterable<Node> children;
                try {
                    children = NodeUtil.getNodes(node);
                    for (Node child : children) {
    
                        if (!child.getPrimaryNodeType().getName().contains("mgnl:page")) {  
                          map.put (child.getName(), nestedMap(child));
    
                       }//end of checking if PrimaryNodeType is of type mgnl:page
                    }
    
                } catch (RepositoryException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                return map;
            }