树形结构
java基于Map实现集合组装树形结构
public class TestMain {
@Data
// @Builder
// @NoArgsConstructor
@AllArgsConstructor
public static class DeptEntity {
private Long deptId;
private Long parentId;
private String deptName;
private List<DeptEntity> children = new ArrayList<>();
public DeptEntity(Long deptId, Long parentId, String deptName) {
this.deptId = deptId;
this.parentId = parentId;
this.deptName = deptName;
}
public void addChild(DeptEntity child) {
this.children.add(child);
}
}
public static List<DeptEntity> getData() {
List<DeptEntity> list = new ArrayList<>();
// list.add(new DeptEntity(100L, 0L, "若依科技"));
list.add(new DeptEntity(101L, 100L, "深圳总公司"));
list.add(new DeptEntity(102L, 100L, "长沙分公司"));
list.add(new DeptEntity(103L, 101L, "研发部门"));
list.add(new DeptEntity(104L, 101L, "市场部门"));
list.add(new DeptEntity(105L, 102L, "运维部门"));
list.add(new DeptEntity(106L, 102L, "财务部门"));
return list;
}
public static void mapBuildTree() {
// 获取假数据
List<DeptEntity> data = getData();
List<DeptEntity> roots = new ArrayList<>();
Map<Long, DeptEntity> childMap = new HashMap<>();
// 1.将所有节点存入Map中
for (DeptEntity deptEntity : data) {
childMap.put(deptEntity.getDeptId(), deptEntity);
}
// 2.构建树结构
for (DeptEntity deptEntity : data) {
// 根节点
if (deptEntity.getParentId() == null || deptEntity.getParentId() == 0 || !childMap.containsKey(deptEntity.getParentId())) {
roots.add(deptEntity);
} else {
// 非根节点,加入到父节点的children列表
DeptEntity parent = childMap.get(deptEntity.getParentId());
if (parent != null) {
parent.addChild(deptEntity);
}
}
}
System.out.println(JSONObject.toJSONString(roots));
}
public static void main(String[] args) {
mapBuildTree();
}
}
引用 https://blog.csdn.net/zhangfuping123456789/article/details/146275943
引用2:https://www.jb51.net/program/339751h5u.htm
java基于Stream实现集合组装树形结构
public class TestMain {
@Data
// @Builder
// @NoArgsConstructor
@AllArgsConstructor
public static class DeptEntity {
private Long deptId;
private Long parentId;
private String deptName;
private List<DeptEntity> children = new ArrayList<>();
/**
* 排序
*/
private Integer sort;
public DeptEntity(Long deptId, Long parentId, String deptName) {
this.deptId = deptId;
this.parentId = parentId;
this.deptName = deptName;
}
public void addChild(DeptEntity child) {
this.children.add(child);
}
}
public static List<DeptEntity> getData() {
List<DeptEntity> list = new ArrayList<>();
// list.add(new DeptEntity(100L, 0L, "若依科技"));
list.add(new DeptEntity(101L, 100L, "深圳总公司"));
list.add(new DeptEntity(102L, 100L, "长沙分公司"));
list.add(new DeptEntity(103L, 101L, "研发部门"));
list.add(new DeptEntity(104L, 101L, "市场部门"));
list.add(new DeptEntity(105L, 102L, "运维部门"));
list.add(new DeptEntity(106L, 102L, "财务部门"));
return list;
}
public static void main(String[] args) {
List<DeptEntity> treedata = listWithTree();
System.out.println(JSONObject.toJSONString(treedata));
}
// 构建树形结构的分类列表
public static List<DeptEntity> listWithTree() {
// 从某个静态源(可能是数据库或配置)获取所有分类的列表
List<DeptEntity> categories = getData();
Map<Long, DeptEntity> childMap = new HashMap<>();
// 1.将所有节点存入Map中
for (DeptEntity deptEntity : categories) {
childMap.put(deptEntity.getDeptId(), deptEntity);
}
// 过滤出所有一级分类(即父分类ID为null的分类),并为每个一级分类设置子分类
List<DeptEntity> collect = categories
.stream() // 将列表转换为流
.filter(m -> !childMap.containsKey(m.getParentId())) // 过滤出一级分类
.map((m) -> { // 对每个一级分类进行处理
m.setChildren(getChildrenList(m, categories)); // 设置子分类
return m; // 返回处理后的分类
})
.collect(Collectors.toList()); // 收集结果到新的列表中
return collect; // 返回构建好的树形结构列表
}
/**
* 获取子节点列表
* @param tree 父节点
* @param list 分类列表
* @return 子节点列表
*/
public static List<DeptEntity> getChildrenList(DeptEntity tree, List<DeptEntity> list) {
// 从列表中过滤出所有父分类ID等于给定父分类ID的分类
List<DeptEntity> children = list
.stream()
.filter(item -> Objects.equals(item.getParentId(), tree.getDeptId()))
.map((item) -> { // 对每个子分类进行处理
item.setChildren(getChildrenList(item, list)); // 递归设置子分类的子分类
return item; // 返回处理后的子分类
})
.collect(Collectors.toList()); // 收集结果到新的列表中
return children; // 返回构建好的子分类列表
}
}