Java 树形结构生成序号/编号

问题描述:

给一组树形结构生成以下编号

1-1
1-1.1
1-1.2
1-2
1-2.1
1-2.1

废话不多说,直接看代码例子

model 结构:

public class TreeVO {

    private String sqNum;

    private Long id;

    private Long parentId;

    private List<TreeVO> childList;
}

例子全部代码 :

需要导入:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.18</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.66</version>
</dependency>
import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

public class TreeSequenceNumber {

    public static void main(String[] args) {
        TreeSequenceNumber treeSequenceNumber = new TreeSequenceNumber();
        treeSequenceNumber.createSeqNum();
    }

    /**
     * 给树结构创建编号
     */
    void createSeqNum() {
        //组装数据
        List<TreeVO> treeList = createData();

        //根节点生成序号
        for (int i = 0; i < treeList.size(); i++) {
            TreeVO treeVO = treeList.get(i);
            String parentSeqNum = i + 1 + "";
            treeVO.setSqNum(parentSeqNum);
            //给每个根底下的子结构也生成编号
            recursiveLoopCreateSeqNum(treeVO.getChildList(), parentSeqNum);
        }

        //打印最后结果
        System.out.println(JSON.toJSONString(treeList));
    }

    /**
     * 递归给根节点底下节点生成编号
     * @param treeList
     * @param parentSeqNum
     */
    void recursiveLoopCreateSeqNum(List<TreeVO> treeList, String parentSeqNum) {
        if (null == treeList || treeList.size() < 1) {
            return;
        }
        for (int i = 0; i < treeList.size(); i++) {
            TreeVO treeVO = treeList.get(i);
            String childSeqNum = parentSeqNum + "-" + (i + 1);
            treeVO.setSqNum(childSeqNum);
            //递归
            recursiveLoopCreateSeqNum(treeVO.getChildList(), childSeqNum);
        }
    }

    /**
     * 创建数据
     */
    List<TreeVO> createData() {
        //树形结构数据列表
        List<TreeVO> treeVOList = new ArrayList<>();

        //巴拉巴拉一堆造数据
        TreeVO treeVO1 = new TreeVO();
        treeVO1.setParentId(5L);
        treeVO1.setId(1L);

        TreeVO treeVO2 = new TreeVO();
        treeVO2.setParentId(5L);
        treeVO2.setId(2L);

        TreeVO treeVO3 = new TreeVO();
        treeVO3.setParentId(6L);
        treeVO3.setId(3L);

        TreeVO treeVO4 = new TreeVO();
        treeVO3.setParentId(6L);
        treeVO4.setId(4L);


        List<TreeVO> treeVO5ChildList = new ArrayList<>();
        treeVO5ChildList.add(treeVO1);
        treeVO5ChildList.add(treeVO2);

        TreeVO treeVO5 = new TreeVO();
        treeVO5.setId(5L);
        treeVO5.setParentId(0L);
        treeVO5.setChildList(treeVO5ChildList);

        List<TreeVO> treeVO6ChildList = new ArrayList<>();
        treeVO6ChildList.add(treeVO3);
        treeVO6ChildList.add(treeVO4);

        TreeVO treeVO6 = new TreeVO();
        treeVO6.setId(6L);
        treeVO6.setParentId(0L);
        treeVO6.setChildList(treeVO6ChildList);

        treeVOList.add(treeVO5);
        treeVOList.add(treeVO6);

        return treeVOList;
    }

    /**
     * 树形结构 model 类
     */
    @Data
    public  static class TreeVO {

        //序列号
        private String sqNum;

        //记录id
        private Long id;

        //父节点id
        private Long parentId;

        //子数据
        private List<TreeVO> childList;
    }
}

用该例子最终得到结果:

[{
	"childList": [{
		"id": 1,
		"parentId": 5,
		"sqNum": "1-1"
	}, {
		"id": 2,
		"parentId": 5,
		"sqNum": "1-2"
	}],
	"id": 5,
	"parentId": 0,
	"sqNum": "1"
}, {
	"childList": [{
		"id": 3,
		"parentId": 6,
		"sqNum": "2-1"
	}, {
		"id": 4,
		"sqNum": "2-2"
	}],
	"id": 6,
	"parentId": 0,
	"sqNum": "2"
}]

本次分享语句:
肉体是拿来用的,而不是拿来伺候的

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赖伟春

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值