如何迭代嵌套映射数组,然后通过使用 stream().map(); 收集它来聚合特定字段的值;

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

我想要从值为 key = "VALUE" 的链接 HashMap 对象数组中收集值。

在下面的代码中,我使用 stream.map() 手动迭代 arrayOfObjects 的每个对象,然后获取 "VALUE" 字段的值。然后 arrayOfObjects 的每个对象都有一个子对象,它也是一个对象数组,我还单独使用 stream.map() 收集了 "VALUE" 字段的值。

        // 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);

我的数据类似于下面的结构:

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

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

    }
]

我希望我的输出数据如下所示,而不使用嵌套 LOOP:

进行迭代
[DUPLICATE, val2, val1, val2Child1, val2Child2, val1Child1, val1Child2]

首先,像你一样使用复杂的类型,HashMap<String, Object> 对象是一个列表,这不是一个好的选择,为此我建议使用 OOP 来解决你的问题,所以创建一个class 像这样:

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
}

其次,像这样存储您的数据,例如:

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")
        ))
);

最后,您可以获得这样的值:

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

输出为:

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