Search code examples
java-8nestedjava-streamlinkedhashmap

How to iterate array of nested map, then aggregate values of a specific field by collecting it using stream().map();


I want the to collect the values from the array of Linked HashMap Objects with the value of key = "VALUE".

In my code below, I manually iterated each object of arrayOfObjects using stream.map() then get the value of "VALUE" fields. Then every object of arrayOfObjects has a child which is an array of objects also, I also collected the value of "VALUE" fields using stream.map() individually.

        // Object 1
        HashMap<String, Object> outerObject1 = new LinkedHashMap<>();
        outerObject1.put("VALUE", "val1");
        // Child 1 of Object 1
        HashMap<String, Object> inner1Child1 = new LinkedHashMap<>();
        inner1Child1.put("VALUE", "val1Child1");
        // Child 2 of Object 1
        HashMap<String, Object> inner1Child2 = new LinkedHashMap<>();
        inner1Child2.put("VALUE", "val1Child2");
        // Child 3 of Object 1
        HashMap<String, Object> inner1Child3 = new LinkedHashMap<>();
        inner1Child3.put("VALUE", "DUPLICATE");

        List<HashMap<String, Object>> childrenOfOuterObject1 = new ArrayList<>(
                Arrays.asList(inner1Child1, inner1Child2, inner1Child3));
        outerObject1.put("CHILD", childrenOfOuterObject1);

        // Object 2
        HashMap<String, Object> outerObject2 = new LinkedHashMap<>();
        outerObject2.put("VALUE", "val2");
        // Child 1 of Object 2
        HashMap<String, Object> inner2Child1 = new LinkedHashMap<>();
        inner2Child1.put("VALUE", "val2Child1");
        // Child 2 of Object 2
        HashMap<String, Object> inner2Child2 = new LinkedHashMap<>();
        inner2Child2.put("VALUE", "val2Child2");
        // Child 3 of Object 2
        HashMap<String, Object> inner2Child3 = new LinkedHashMap<>();
        inner2Child3.put("VALUE", "DUPLICATE");

        List<HashMap<String, Object>> childrenOfOuterObject2 = new ArrayList<>(
                Arrays.asList(inner2Child1, inner2Child2, inner2Child3));
        outerObject2.put("CHILD", childrenOfOuterObject2);

        List<HashMap<String, Object>> arrayOfObjects = new ArrayList<>(Arrays.asList(outerObject1, outerObject2));

        List<String> outerValues = arrayOfObjects.stream().map(obj -> (String) obj.get("VALUE")).collect(
                Collectors.toList());
        System.out.println(outerValues);

        List<HashMap<String, Object>> innerChildren1 = (List<HashMap<String, Object>>)outerObject1.get("CHILD");
        List<String> innerValues1 = innerChildren1.stream().map(obj -> (String) obj.get("VALUE")).collect(Collectors.toList());
        System.out.println(innerValues1);

        List<HashMap<String, Object>> innerChildren2 = (List<HashMap<String, Object>>)outerObject2.get("CHILD");
        List<String> innerValues2 = innerChildren2.stream().map(obj -> (String) obj.get("VALUE")).collect(Collectors.toList());
        System.out.println(innerValues2);

        Set<String> aggregatedValues = new HashSet<>(outerValues);
        aggregatedValues.addAll(innerValues1);
        aggregatedValues.addAll(innerValues2);

        System.out.println(aggregatedValues);

My data is similar to the structure below:

[
    {
        "VALUE": "val1",
        "CHILD": [
            {
                "VALUE": "val1Child1"
            },
            {
                "VALUE": "val1Child2"
            },
            {
                "VALUE": "DUPLICATE"
            }
        ]

    },
    {
        "VALUE": "val2",
        "CHILD": [
            {
                "VALUE": "val2Child1"
            },
            {
                "VALUE": "val2Child2"
            },
            {
                "VALUE": "DUPLICATE"
            }
        ]

    }
]

I want my output data to be like below with out iterating using nested LOOP:

[DUPLICATE, val2, val1, val2Child1, val2Child2, val1Child1, val1Child2]

Solution

  • First of all, using a complex Type as you do, HashMap<String, Object> where Object is a List, is not a good choice, for that I would suggest to use OOP to solve your problem, so create a class like this:

    public class MyObject {
        private String value;
        private List<MyObject> child;
    
        public MyObject(String value) {
            this.value = value;
        }
    
        public MyObject(String value, List<MyObject> child) {
            this.value = value;
            this.child = child;
        }
        // Getters and setters
    }
    

    Second, store your data like this for example:

    List<MyObject> parents = Arrays.asList(
            new MyObject("val1", Arrays.asList(
                    new MyObject("val1Child1"),
                    new MyObject("val1Child2"),
                    new MyObject("DUPLICATE")
            )),
            new MyObject("val2", Arrays.asList(
                    new MyObject("val2Child1"),
                    new MyObject("val2Child2"),
                    new MyObject("DUPLICATE")
            ))
    );
    

    and finally, you can get the values like this:

    Set<String> result = parents.stream()
            .flatMap(p -> {
                Set<String> values = new HashSet<>();
                values.add(p.getValue());
                values.addAll(p.getChild().stream()
                        .map(MyObject::getValue)
                        .collect(Collectors.toSet()));
                return values.stream();
            }).collect(Collectors.toSet());
    

    and output are:

    [DUPLICATE, val2, val1, val2Child1, val2Child2, val1Child1, val1Child2]