easy递归实现树结构数据

23 阅读1分钟

我直接上代码

  • 树对象
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RecDemoVO {

    private Long id;

    private String name;

    private Long parentId;

    private List<RecDemoVO> children;
}
  • 处理代码
public List<RecDemoVO> demo() {

    // 模拟数据库查询
    List<RecDemoVO> list = createRecDemoVOData();

    // 将数据按照parentId分组
    Map<Long, List<RecDemoVO>> parentIdToChildrenMap = list.stream()
            .collect(Collectors.groupingBy(RecDemoVO::getParentId));

    // 定义树结果集合
    List<RecDemoVO> treeList = new ArrayList<>();

    // 获取顶级节点列表
    List<RecDemoVO> topLevelNodes = parentIdToChildrenMap.get(0L);
    for (RecDemoVO topLevelNode : topLevelNodes) {
        // 构建子节点
        buildChildren(topLevelNode, parentIdToChildrenMap);
        treeList.add(topLevelNode);
    }

    return treeList;
}

private void buildChildren(RecDemoVO parentNode, Map<Long, List<RecDemoVO>> parentIdToChildrenMap) {
    // 获取子节点列表
    List<RecDemoVO> list = parentIdToChildrenMap.get(parentNode.getId());
    if (CollUtil.isEmpty(list)) {
        return;
    }
    for (RecDemoVO recDemoVO : list) {
        // 构建子节点
        List<RecDemoVO> children = parentNode.getChildren();
        if (children == null) {
            children = new ArrayList<>();
            children.add(recDemoVO);
            parentNode.setChildren(children);
        } else {
            children.add(recDemoVO);
        }
        // 递归构建子节点的子节点
        buildChildren(recDemoVO, parentIdToChildrenMap);
    }
}

private List<RecDemoVO> createRecDemoVOData() {
    List<RecDemoVO> list = new ArrayList<>();
    list.add(RecDemoVO.builder().id(1L).name("1").parentId(0L).build());
    list.add(RecDemoVO.builder().id(2L).name("2").parentId(0L).build());
    list.add(RecDemoVO.builder().id(3L).name("3").parentId(0L).build());
    list.add(RecDemoVO.builder().id(4L).name("1.1").parentId(1L).build());
    list.add(RecDemoVO.builder().id(5L).name("1.2").parentId(1L).build());
    list.add(RecDemoVO.builder().id(6L).name("1.2.1").parentId(5L).build());
    list.add(RecDemoVO.builder().id(7L).name("2.1").parentId(2L).build());
    list.add(RecDemoVO.builder().id(8L).name("2.1.1").parentId(7L).build());
    list.add(RecDemoVO.builder().id(9L).name("2.1.1.1").parentId(8L).build());
    return list;
}
  • 处理结果
[
  {
    "id": 1,
    "name": "1",
    "parentId": 0,
    "children": [
      {
        "id": 4,
        "name": "1.1",
        "parentId": 1,
        "children": null
      },
      {
        "id": 5,
        "name": "1.2",
        "parentId": 1,
        "children": [
          {
            "id": 6,
            "name": "1.2.1",
            "parentId": 5,
            "children": null
          }
        ]
      }
    ]
  },
  {
    "id": 2,
    "name": "2",
    "parentId": 0,
    "children": [
      {
        "id": 7,
        "name": "2.1",
        "parentId": 2,
        "children": [
          {
            "id": 8,
            "name": "2.1.1",
            "parentId": 7,
            "children": [
              {
                "id": 9,
                "name": "2.1.1.1",
                "parentId": 8,
                "children": null
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "id": 3,
    "name": "3",
    "parentId": 0,
    "children": null
  }
]

4b7a03cd44d947c6ad0af1a558577c3d.gif