如何使用java8流在条件比较中递归地迭代列表

时间:2019-05-02 10:38:56

标签: java java-8

我有两组json,其中包含递归子级。 我想在该列表中找到最后一个包含“代码”的孩子,我想从该json中找到所有“代码”值。

样本1:

{
    "totalSize": 1,
    "data": [
        {
            "level": "sites",
            "children": [
                {
                    "level": "sites",
                    "children": [
                        {
                            "level": "segments",
                            "children": [
                                {
                                    "level": "assets",
                                    "code": "1"
                                },
                                {
                                    "level": "assets",
                                    "code": "2"
                                },
                                {
                                    "level": "assets",
                                    "code": "3"
                                },
                                {
                                    "level": "assets",
                                    "code": "4"
                                },
                                {
                                    "level": "assets",
                                    "code": "5"
                                },
                                {
                                    "level": "assets",
                                    "code": "6"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

样本2:

{
    "totalSize": 1,
    "data": [
        {
            "level": "sites",
            "children": [
                {
                    "level": "segments",
                    "children": [
                        {
                            "level": "assets",
                            "code": "1"
                        },
                        {
                            "level": "assets",
                            "code": "2"
                        },
                        {
                            "level": "assets",
                            "code": "3"
                        },
                        {
                            "level": "assets",
                            "code": "4"
                        },
                        {
                            "level": "assets",
                            "code": "5"
                        },
                        {
                            "level": "assets",
                            "code": "6"
                        }
                    ]
                }
            ]
        }
    ]
}

实体如下:

public class HierarchyResponse {

    private Integer totalSize;
    private List<Data> data;

}

public class Data {

    private List<Children> children;
    private String level;

}

public class Children {

    private String level;
    private List<Children> children;
}

我尝试过但没有成功:

List<Children> children = response
    .getData()
    .get(0)
    .getChildren()
    .stream().filter(t -> t.getLevel().equalsIgnoreCase("assets"))
    .collect(Collectors.toList());

1 个答案:

答案 0 :(得分:0)

您可以创建一种递归地对子项进行平面映射的方法

for (var i = 0; i <state0.length; i ++) {
   var indexOfChild = state0 [i];
   ...
}

并将其用作

private static Stream<Children> toStream(Children children) {
    if (children != null) {
        return Stream.concat(Stream.of(children), children.getChildren().stream().flatMap(c -> toStream(c)));
    } else {
        return Stream.empty();
    }
}

例如

List<Children> children = response.getData().stream()
            .flatMap(d -> d.getChildren().stream())
            .flatMap(c -> toStream(c))
            .filter(t -> t.getLevel().equalsIgnoreCase("assets"))
            .collect(Collectors.toList());

将打印Children b1as2 = new Children("assets", "2"); Children b1as1 = new Children("assets", "1"); Children b1seg1 = new Children("segments", "0", List.of(b1as1, b1as2)); Children b1s1 = new Children("sites", "0", List.of(b1seg1)); Children b1 = new Children("sites", "0", List.of(b1s1)); Data data = new Data(List.of(b1)); HierarchyResponse response = new HierarchyResponse(List.of(data));

另一个解决方案未使用Stream API

[Children{level='assets', code='1'}, Children{level='assets', code='2'}]