java对一个课程树进行无限极递归获取其中的某一个值

先看看List < Object>数据

"courseNode": [
        {
            "_id": NumberLong("1615240830262"),
            "showName": "《危险货物道路运输规则》第5部分:托运要求",
            "fullName": "《危险货物道路运输规则》第5部分:托运要求",
            "type": "00",
            "lev": NumberInt("1"),
            "time": NumberInt("0"),
            "children": [
                {
                    "_id": NumberLong("1612652508404"),
                    "showName": "1.1前言与学习目标",
                    "fullName": "《危险货物道路运输规则》第5部分:托运要求&&1.1前言与学习目标",
                    "refId": "AQ_R_W_1_1_1",
                    "mark": "7ED48EA6E89975979C33DC5901307461",
                    "type": "01",
                    "lev": NumberInt("2"),
                    "time": NumberInt("113")
                },
                {
                    "_id": NumberLong("1615103730049"),
                    "showName": "2.1学习内容-概述与技术变化",
                    "fullName": "《危险货物道路运输规则》第5部分:托运要求&&2.1学习内容-概述与技术变化",
                    "refId": "AQ_R_W_1_2_1",
                    "mark": "DEC3029FFA73A18C9C33DC5901307461",
                    "type": "01",
                    "lev": NumberInt("2"),
                    "time": NumberInt("511")
                },
                {
                    "_id": NumberLong("1606012909286"),
                    "showName": "2.2学习内容-术语及定义",
                    "fullName": "《危险货物道路运输规则》第5部分:托运要求&&2.2学习内容-术语及定义",
                    "refId": "AQ_R_W_1_2_2",
                    "mark": "307C84936DAC6D659C33DC5901307461",
                    "type": "01",
                    "lev": NumberInt("2"),
                    "time": NumberInt("486")
                },
                {
                    "_id": NumberLong("1612850636373"),
                    "showName": "3.1重点难点-集合包装与包件标志",
                    "fullName": "《危险货物道路运输规则》第5部分:托运要求&&3.1重点难点-集合包装与包件标志",
                    "refId": "AQ_R_W_1_3_1",
                    "mark": "E1283AEABF8017A69C33DC5901307461",
                    "type": "01",
                    "lev": NumberInt("2"),
                    "time": NumberInt("1032")
                },
                {
                    "_id": NumberLong("1609136498836"),
                    "showName": "3.2重点难点-集装箱、罐体与车辆标志",
                    "fullName": "《危险货物道路运输规则》第5部分:托运要求&&3.2重点难点-集装箱、罐体与车辆标志",
                    "refId": "AQ_R_W_1_3_2",
                    "mark": "FA3B960803F2BC6F9C33DC5901307461",
                    "type": "01",
                    "lev": NumberInt("2"),
                    "time": NumberInt("604")
                },
                {
                    "_id": NumberLong("1605808853392"),
                    "showName": "3.3重点难点-运输单据与安全卡",
                    "fullName": "《危险货物道路运输规则》第5部分:托运要求&&3.3重点难点-运输单据与安全卡",
                    "refId": "AQ_R_W_1_3_3",
                    "mark": "B52DD8ADA4822A419C33DC5901307461",
                    "type": "01",
                    "lev": NumberInt("2"),
                    "time": NumberInt("663")
                },
                {
                    "_id": NumberLong("1611211785021"),
                    "showName": "4.1学习小结",
                    "fullName": "《危险货物道路运输规则》第5部分:托运要求&&4.1学习小结",
                    "refId": "AQ_R_W_1_4_1",
                    "mark": "9F43986379E5968C9C33DC5901307461",
                    "type": "01",
                    "lev": NumberInt("2"),
                    "time": NumberInt("104")
                }
            ]
        },
        {
            "_id": NumberLong("1607594201602"),
            "showName": "危化品",
            "fullName": "危化品泄漏、爆炸事故案例分析",
            "type": "00",
            "lev": NumberInt("1"),
            "time": NumberInt("0"),
            "children": [
                {
                    "_id": NumberLong("1613899078638"),
                    "showName": "1.1前言",
                    "fullName": "危化品泄漏、爆炸事故案例分析&&1.1前言与学习目标",
                    "refId": "AQ_R_W_2_1_1",
                    "mark": "2BCD90DBE781B5D89C33DC5901307461",
                    "type": "01",
                    "lev": NumberInt("2"),
                    "time": NumberInt("73")
                },
                {
                    "_id": NumberLong("1609585415163"),
                    "showName": "4.1学习小结",
                    "fullName": "危化品泄漏、爆炸事故案例分析&&4.1学习小结",
                    "refId": "AQ_R_W_2_4_1",
                    "mark": "C5DC56E4B29EA4B49C33DC5901307461",
                    "type": "01",
                    "lev": NumberInt("2"),
                    "time": NumberInt("81")
                },
                {
                    "_id": NumberLong("1606719569970"),
                    "showName": "2.1学习内容-操作规范 ",
                    "fullName": "危化品泄漏、爆炸事故案例分析&&2.1学习内容-操作规范 ",
                    "refId": "AQ_R_W_2_2_1",
                    "mark": "98B32A5A4B7E7EEC9C33DC5901307461",
                    "type": "01",
                    "lev": NumberInt("2"),
                    "time": NumberInt("325")
                },
                {
                    "_id": NumberLong("1606930840867"),
                    "showName": "2.2学习内容-运输安全与应急处置",
                    "fullName": "危化品泄漏、爆炸事故案例分析&&2.2学习内容-运输安全与应急处置",
                    "refId": "AQ_R_W_2_2_2",
                    "mark": "44821F110651ECC99C33DC5901307461",
                    "type": "01",
                    "lev": NumberInt("2"),
                    "time": NumberInt("720")
                },
                {
                    "_id": NumberLong("1611567737744"),
                    "showName": "3.1重点难点-危化品泄漏事故分析(一)",
                    "fullName": "危化品泄漏、爆炸事故案例分析&&3.1重点难点-危化品泄漏事故分析(一)",
                    "refId": "AQ_R_W_2_3_1",
                    "mark": "5D5C7095CA965AC99C33DC5901307461",
                    "type": "01",
                    "lev": NumberInt("2"),
                    "time": NumberInt("813")
                },
                {
                    "_id": NumberLong("1610679458524"),
                    "showName": "3.2重点难点-危化品泄漏事故分析(二)",
                    "fullName": "危化品泄漏、爆炸事故案例分析&&3.2重点难点-危化品泄漏事故分析(二)",
                    "refId": "AQ_R_W_2_3_2",
                    "mark": "64B7840273BC389B9C33DC5901307461",
                    "type": "01",
                    "lev": NumberInt("2"),
                    "time": NumberInt("729")
                },
                {
                    "_id": NumberLong("1614993260091"),
                    "showName": "3.3重点难点-危化品爆炸事故分析",
                    "fullName": "危化品泄漏、爆炸事故案例分析&&3.3重点难点-危化品爆炸事故分析",
                    "refId": "AQ_R_W_2_3_3",
                    "mark": "05BDA5F983D87BBD9C33DC5901307461",
                    "type": "01",
                    "lev": NumberInt("2"),
                    "time": NumberInt("741")
                }
            ]
        },
        {
            "_id": NumberLong("1607594201603"),
            "showName": "新建章",
            "fullName": "、爆炸事故案例分",
            "type": "00",
            "lev": NumberInt("1"),
            "time": NumberInt("0")
        }
    ]

递归查询

public void getMark(List<CourseNodeContentMongoEntity> nodes,List<String> marks) {
    for (CourseNodeContentMongoEntity item : nodes) {
        if (CollectionUtil.isEmpty(item.getChildren())) {
            marks.add(item.getMark());
        } else {
            getMark(item.getChildren(),marks);
        }
    }
}
@Test
public void getMark1() {
    String id = "282823077873389568";
    CourseMongoEntity mongoEntity = mongoTemplate.findById(Long.valueOf(id), CourseMongoEntity.class);
    if (mongoEntity == null) {
        return;
    }
    List<CourseNodeContentMongoEntity> entityCourseNode = mongoEntity.getCourseNode();
    List<String> marks =new ArrayList<>();
    getMark(entityCourseNode,marks);
    System.out.println(marks);
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值