java 无限级分类

2024-03-07  本文已影响0人  夜空中乄最亮的星

数据结构

{
    "productCode":100,
    "menuList": [
        {
            "code": 1001,
            "parentCode": null,
            "name": "触达预览",
            "sort": 0
        },{
            "code": 1002,
            "parentCode": null,
            "name": "营销策略",
            "sort": 0
        },{
            "code": 1003,
            "parentCode": null,
            "name": "内容与资产",
            "sort": 0
        },{
            "code": 2001,
            "parentCode": "1001",
            "name": "业务总览",
            "sort": 0
        },
        {
            "code": 2002,
            "parentCode": "1002",
            "name": "流程画布",
            "sort": 0
        },
        {
            "code": 3001,
            "parentCode": "2002",
            "name": "触达任务",
            "sort": 1
        }
    ]
}

代码

private static List<MenuTreeVO> getTree(List<MenuTreeVO> list){
        if (ObjectUtil.isEmpty(list)){
            return null;
        }
        //构造以菜单CODE为key 对象为value的map
        Map<String,MenuTreeVO> map = list.stream().collect(Collectors.toMap(MenuTreeVO::getCode, item -> item));

        //遍历原list,在map中查找,若存在父级则追加到他的children中
        list.stream().filter(o->StrUtil.isNotBlank(o.getParentCode())).forEach(item -> {
            Optional.ofNullable(map.get(item.getParentCode()))
                    .ifPresent(parent -> {
                        List<MenuTreeVO> children = parent.getChildren();
                        if (children == null) {
                            children = new ArrayList<>();
                            parent.setChildren(children);
                        }
                        children.add(item);
                    });
        });
        //节省内存占用
        map.clear();
        //只取出顶级分类
        return list.stream().filter(item -> StrUtil.isBlank(item.getParentCode())).collect(Collectors.toList());
    }

结果测试

[
        {
            "id": 1765987779571957761,
            "code": "1001",
            "parentCode": null,
            "name": "触达预览",
            "sort": 0,
            "checked": false,
            "children": [
                {
                    "id": 1765987779571957764,
                    "code": "2001",
                    "parentCode": "1001",
                    "name": "业务总览",
                    "sort": 0,
                    "checked": false,
                    "children": null
                }
            ]
        },
        {
            "id": 1765987779571957762,
            "code": "1002",
            "parentCode": null,
            "name": "营销策略",
            "sort": 0,
            "checked": false,
            "children": [
                {
                    "id": 1765987779571957765,
                    "code": "2002",
                    "parentCode": "1002",
                    "name": "流程画布",
                    "sort": 0,
                    "checked": false,
                    "children": [
                        {
                            "id": 1765987779571957766,
                            "code": "3001",
                            "parentCode": "2002",
                            "name": "触达任务",
                            "sort": 1,
                            "checked": false,
                            "children": null
                        }
                    ]
                }
            ]
        },
        {
            "id": 1765987779571957763,
            "code": "1003",
            "parentCode": null,
            "name": "内容与资产",
            "sort": 0,
            "checked": false,
            "children": null
        }
    ]
上一篇 下一篇

猜你喜欢

热点阅读