Java封装树形结构数据
业务场景
进行业务开发时,有时前端需要一个树形导航菜单,或者分类菜单,这样就需要后端把数据封装成一个树形结构。比较常见的像导航菜单、省市区级联等。
在此模拟一个类似场景,查询部门信息,并且存在上下级。
实体类
实体类中使用parentCode
来实现数据关联。
/**
* @author Yoko
* @date 2021/10/28
*/
@Data
@Builder
public class Department {
/**
* 公司编码
*/
private Integer companyCode;
/**
* 部门名称
*/
private String name;
/**
* 部门编码
*/
private Integer code;
/**
* 上级部门编码
*/
private Integer parentCode;
/**
* 部门下所管辖部门
*/
private List<Department> children;
}
业务实现
public static void main(String[] args) {
// 查询公司下所有部门信息
List<com.hqins.ishare.user.demo.Department> departmentList = departmentListByCompanyCode();
// 使用filter()过滤,获取最上层部门
List<com.hqins.ishare.user.demo.Department> result = departmentList.stream().filter(depart -> 0 == depart.getParentCode())
// 使用peek() 为上级部门添加下级部门
.peek(depart -> depart.setChildren(createChildList(depart, departmentList)))
// 排序
.sorted(Comparator.comparing(com.hqins.ishare.user.demo.Department:: getCode))
// 收集数据至list
.collect(Collectors.toList());
System.out.println(JSONObject.toJSONString(result));
}
/**
* 递归获取
* 根据当前部门,获取其子部门
*/
private static List<com.hqins.ishare.user.demo.Department> createChildList(com.hqins.ishare.user.demo.Department father, List<com.hqins.ishare.user.demo.Department> list) {
return list.stream().filter(model -> father.getCode().equals(model.getParentCode()))
.peek(model -> model.setChildren(createChildList(model, list)))
.sorted(Comparator.comparing(com.hqins.ishare.user.demo.Department:: getCode))
.collect(Collectors.toList());
}
/**
* 模拟数据库查询
* 根据公司编码获取公司所有部门信息
*/
private static List<com.hqins.ishare.user.demo.Department> departmentListByCompanyCode() {
List<com.hqins.ishare.user.demo.Department> result = new ArrayList<>();
com.hqins.ishare.user.demo.Department model1 = com.hqins.ishare.user.demo.Department.builder().companyCode(10086).name("技术部").code(10000).parentCode(0).build();
com.hqins.ishare.user.demo.Department model2 = com.hqins.ishare.user.demo.Department.builder().companyCode(10086).name("java开发部").code(10001).parentCode(10000).build();
com.hqins.ishare.user.demo.Department model3 = com.hqins.ishare.user.demo.Department.builder().companyCode(10086).name("前端技术部").code(10002).parentCode(10000).build();
com.hqins.ishare.user.demo.Department model4 = com.hqins.ishare.user.demo.Department.builder().companyCode(10086).name("人事部").code(10003).parentCode(0).build();
com.hqins.ishare.user.demo.Department model5 = com.hqins.ishare.user.demo.Department.builder().companyCode(10086).name("人事一部").code(10004).parentCode(10003).build();
com.hqins.ishare.user.demo.Department model6 = com.hqins.ishare.user.demo.Department.builder().companyCode(10086).name("人事二部").code(10005).parentCode(10003).build();
result.add(model1);
result.add(model2);
result.add(model3);
result.add(model4);
result.add(model5);
result.add(model6);
return result;
}
数据结果
[{
"children": [{
"children": [],
"code": 10001,
"companyCode": 10086,
"name": "java开发部",
"parentCode": 10000
}, {
"children": [],
"code": 10002,
"companyCode": 10086,
"name": "前端技术部",
"parentCode": 10000
}],
"code": 10000,
"companyCode": 10086,
"name": "技术部",
"parentCode": 0
}, {
"children": [{
"children": [],
"code": 10004,
"companyCode": 10086,
"name": "人事一部",
"parentCode": 10003
}, {
"children": [],
"code": 10005,
"companyCode": 10086,
"name": "人事二部",
"parentCode": 10003
}],
"code": 10003,
"companyCode": 10086,
"name": "人事部",
"parentCode": 0
}]
学习是一个循序渐进的过程