原文地址 :
一、描述
public class TreeVO {
private String sqNum;
private Long id;
private Long parentId;
private List<TreeVO> childList;
}
用上面树形结构生成下面这样的序号
1-1
1-1.1
1-1.2
1-2
1-2.1
1-2.1
二、代码实现
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));
}
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;
}
}
最终结果:
[{
"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"
}]