如何将一个平铺的数据模型转化为树状结构(优雅实现)

假设我们有一个平铺的数据:生产单元列表

原始的数据模型为:

public class ProduceUnitInfo implements Serializable {

    private static final long serialVersionUID = 5459052298447161811L;

    @Excel(name = "一级生产单元编码")
    @TableField(value = "firstLevelProductionUnitCode")
    private String firstLevelProductionUnitCode; //一级生产单元编码

    @Excel(name = "一级生产单元名称")
    @TableField(value = "firstLevelProductionUnitName")
    private String firstLevelProductionUnitName;//一级生产单元名称

    @Excel(name = "二级生产单元编码")
    @TableField(value = "secdLevelProductionUnitCode")
    private String secondLevelProductionUnitCode;//二级生产单元编码

    @Excel(name = "二级生产单元名称")
    @TableField(value = "secdLevelProductionUnitName")
    private String secondLevelProductionUnitName;//二级生产单元名称

    @Excel(name = "三级生产单元编码")
    @TableField(value = "thirdLevelProductionUnitCode")
    private String thirdLevelProductionUnitCode;//三级生产单元编码

    @Excel(name = "三级生产单元名称")
    @TableField(value = "thirdLevelProductionUnitName")
    private String thirdLevelProductionUnitName;//三级生产单元名称

    @Excel(name = "四级生产单元编码")
    @TableField(value = "forthLevelProductionUnitCode")
    private String forthLevelProductionUnitCode;//四级生产单元编码

    @Excel(name = "四级生产单元名称")
    @TableField(value = "forthLevelProductionUnitName")
    private String forthLevelProductionUnitName;//四级生产单元名称
}

设计生产单元的树状结构模型为:

public class TreeBusinessNode {

    private String label;
    private String value;
    private List<TreeBusinessNode> children;

    public TreeBusinessNode(String label, String value) {
        this.label = label;
        this.value = value;
        this.children = new ArrayList<>();
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public List<TreeBusinessNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeBusinessNode> children) {
        this.children = children;
    }
}

现有的数据为:

核心代码编写:

/**
     * 构建生产单元树
     *
     * @return
     */
    private List<TreeBusinessNode> buildTree(List<ProduceUnitInfo> produceUnitInfos) {
        // 首先将一级科目提取出来
        return produceUnitInfos.stream()
                .collect(Collectors.groupingBy(ProduceUnitInfo::getFirstLevelProductionUnitCode))
                .entrySet().stream()
                .map(entry -> {
                    String firstLevelCode = entry.getKey();
                    ProduceUnitInfo firstLevelSetting = entry.getValue().get(0);
                    TreeBusinessNode firstLevelNode = new TreeBusinessNode(firstLevelSetting.getFirstLevelProductionUnitName(), firstLevelCode);

                    // 二级科目
                    List<TreeBusinessNode> secondLevelNodes = entry.getValue().stream()
                            .collect(Collectors.groupingBy(ProduceUnitInfo::getSecondLevelProductionUnitCode))
                            .entrySet().stream()
                            .map(secondEntry -> {
                                String secondLevelCode = secondEntry.getKey();
                                ProduceUnitInfo secondLevelSetting = secondEntry.getValue().get(0);
                                TreeBusinessNode secondLevelNode = new TreeBusinessNode(secondLevelSetting.getSecondLevelProductionUnitName(), secondLevelCode);

                                //三级科目
                                List<TreeBusinessNode> thirdLevelNodes = entry.getValue().stream()
                                        .filter(thirdSetting -> thirdSetting.getThirdLevelProductionUnitCode() != null)
                                        .collect(Collectors.groupingBy(ProduceUnitInfo::getThirdLevelProductionUnitCode))
                                        .entrySet().stream()
                                        .map(thirdEntry -> {
                                            String thirdLevelCode = thirdEntry.getKey();
                                            ProduceUnitInfo thirdLevelSetting = thirdEntry.getValue().get(0);
                                            TreeBusinessNode thirdLevelNode = new TreeBusinessNode(thirdLevelSetting.getThirdLevelProductionUnitName(), thirdLevelCode);

                                            // 四级科目
                                            List<TreeBusinessNode> fourthLevelNodes = thirdEntry.getValue().stream()
                                                    .filter(fourthSetting -> fourthSetting.getForthLevelProductionUnitCode() != null)
                                                    .map(fourthSetting -> new TreeBusinessNode(fourthSetting.getForthLevelProductionUnitName(), fourthSetting.getForthLevelProductionUnitCode()))
                                                    .collect(Collectors.toList());
                                            thirdLevelNode.setChildren(fourthLevelNodes);
                                            return thirdLevelNode;
                                        })
                                        .collect(Collectors.toList());

                                secondLevelNode.setChildren(thirdLevelNodes);
                                return secondLevelNode;
                            })
                            .collect(Collectors.toList());

                    firstLevelNode.setChildren(secondLevelNodes);
                    return firstLevelNode;
                })
                .collect(Collectors.toList());
    }

转化之后的结果为:

{
  "errcode": 200,
  "errmsg": "调用成功!!",
  "data": [
    {
      "label": "22",
      "value": "11",
      "children": [
        {
          "label": "44",
          "value": "33",
          "children": [
            {
              "label": "66",
              "value": "55",
              "children": [
                {
                  "label": "88",
                  "value": "77",
                  "children": []
                },
                {
                  "label": "88",
                  "value": "77",
                  "children": []
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "label": "2",
      "value": "1",
      "children": [
        {
          "label": "4",
          "value": "3",
          "children": [
            {
              "label": "6",
              "value": "5",
              "children": [
                {
                  "label": "8",
                  "value": "7",
                  "children": []
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "label": "截止阀事业部",
      "value": "26",
      "children": [
        {
          "label": "铜管车间",
          "value": "260110",
          "children": []
        },
        {
          "label": "截止阀焊接车间",
          "value": "260102",
          "children": []
        },
        {
          "label": "截止阀金加工车间",
          "value": "260101",
          "children": []
        },
        {
          "label": "方体阀金加工车间",
          "value": "260104",
          "children": []
        },
        {
          "label": "截止阀装配车间",
          "value": "260103",
          "children": []
        },
        {
          "label": "方体阀装配车间",
          "value": "260106",
          "children": []
        },
        {
          "label": "方体阀焊接车间",
          "value": "260105",
          "children": []
        },
        {
          "label": "#装配车间",
          "value": "200003",
          "children": []
        }
      ]
    }
  ]
}

简单明了,大家一键三连!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值