Search code examples
javatreelinkedhashmap

How to transform a flattened LinkedHashMap to one that represents a hierarchy


I'm a Java newbie and I'm having problems conceptualizing how to solve the problem of trying to create a LinkedHashMap that represents some hierarchical data.

This is as far as I've gotten. In this example, I've got a LinkedHashMap that shows the flattened result:

public static Map<String, String> myDataSet = new LinkedHashMap() {
{
    put("MY_KEY_1", "My First Label");
    put("MY_KEY_2", "My Second Label");
    put("MY_KEY_3", "My Third Label");
    put("MY_KEY_4", "My Fourth Label");
    put("MY_KEY_5", "My Fifth Label");
}
};

This is only how I create the LinkedHashMap. At this point I iterate over the body of my response and capture the values with the keys...but you don't need to see all that ugly code. In the end I've got this response where the content is an array of objects:

{
data:{
    content:[
        {
            name: "My First Label",
            value: 500,
        },
        {
            name: "My Second Label",
            value: 1500,
        },
        {
            name: "My Third Label",
            value:2500,
        },
        {
            name: "My Fourth Label",
            value: 3500,
        },
        {
            name: "My Fifth Label",
            value: 4500,
        }
    ]
}

},

Which is great, a flattened data set. But what I really want to learn is how to create a LinkedHashMap in such a way that this is the result:

{
data:{
    content:[
        {
            name: "My First Label",
            props:[
                {
                    name: "Sub label 1",
                    value: 500,
                },
                {
                    name: "Sub label 2",
                    value: 1500,
                },
            ]
        },
        {
            name: "My Second Label",
            props:[
                {
                    name: "Sub label 1",
                    value: 2500,
                },
            ]
        },
        {
            name: "My Fourth Label",
            props: [
                {
                    name: "Sub label 1",
                    value: 500,
                },
            ],
        },
        {
            name: "My Fifth Label",
            props: [
                {
                    name: "Sub label 1"
                    value: 4500
                }
            ],
        }
    ]
}

}

And I'm a little lost as to how to get started to transform this. Any tips are appreciated. Thanks.


Solution

  • This can be accomplished by setting the type of the mapped value to another Map - e.g. Map<String, Map<String, Integer>>.

    public static Map<String, Map<String, Integer>> myDataSet =
            new LinkedHashMap<String, Map<String, Integer>>() {{
        this.put("My First Label", new LinkedHashMap<String, Integer>() {{
            this.put("Sub label 1", 500);
            this.put("Sub label 2", 1500);
        }});
        this.put("My Second Label", new LinkedHashMap<String, Integer>() {{
            this.put("Sub label 1", 2500);
        }});
    }};