public class TreeNode<T> {
@Schema(description = "id")
private String id;
@Schema(description = "名称")
private String name;
@Schema(description = "父级id")
private String parentId;
@Schema(description = "排序")
private Integer sort;
@Schema(description = "类型")
private String type;
@Schema(description = "别名")
private String alias;
@Schema(description = "子集")
private List<TreeNode<T>> children;
@Schema(description = "扩展字段")
private T ext;
@Schema(description = "扩展数据集")
private Object extra;
}
public class TreeBuild<T> {
private final String rootParentId = "0";
private List<TreeNode<T>> nodeList = new ArrayList<>();
public TreeBuild(List<TreeNode<T>> nodeList) {
this.nodeList = nodeList;
}
private TreeBuild() {
}
/**
* 构建
* @return /
*/
public List<TreeNode<T>> build() {
List<TreeNode<T>> treeNodes = new ArrayList<>();
getRootNode().forEach(root -> {
treeNodes.add(buildChildTree(root));
});
return treeNodes;
}
/**
* 获取根节点
* @return /
*/
private List<TreeNode<T>> getRootNode() {
List<TreeNode<T>> rootNodeList = new ArrayList<>();
nodeList.forEach(item -> {
if (rootParentId.equals(item.getParentId())) {
rootNodeList.add(item);
}
});
return rootNodeList;
}
/**
* 处理子集
* @param parentNode /
* @return /
*/
private TreeNode<T> buildChildTree(TreeNode<T> parentNode) {
List<TreeNode<T>> childTree = new ArrayList<>();
nodeList.forEach(item -> {
// 如果相等便进入 处理子集的数据,子集如果还有就处理子集的子集,由里向外设置数据
if (parentNode.getId().equals(item.getParentId())) {
childTree.add(buildChildTree(item));
}
});
parentNode.setChildren(childTree);
return parentNode;
}
}
优化后
在原来的基础上增加了顶层父级id、排序
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
* 递归树构建
* @author qb
* @version 1.0
* @since 2023/2/22 9:40
*/
public class TreeBuild<T> {
private final String rootParentId;
private final Boolean isSort;
private final List<? extends TreeNodeAbstract<T>> nodeList;
public TreeBuild(List<? extends TreeNodeAbstract<T>> nodeList) {
this.nodeList = nodeList;
this.rootParentId = "0";
this.isSort = false;
}
public TreeBuild(List<? extends TreeNodeAbstract<T>> nodeList, Boolean isSort) {
this.nodeList = nodeList;
this.rootParentId = "0";
this.isSort = isSort;
}
private TreeBuild(List<? extends TreeNodeAbstract<T>> nodeList,String rootParentId) {
this.nodeList = nodeList;
this.rootParentId = rootParentId;
this.isSort = false;
}
private TreeBuild(List<? extends TreeNodeAbstract<T>> nodeList,String rootParentId, Boolean isSort) {
this.nodeList = nodeList;
this.rootParentId = rootParentId;
this.isSort = isSort;
}
public List<? extends TreeNodeAbstract<T>> build() {
List<TreeNodeAbstract<T>> treeNodes = new ArrayList<>();
getRootNode(isSort).forEach(root -> treeNodes.add(buildChildTree(root)));
//if(isSort){
// return treeNodes.stream().peek(item -> item.setChildren(sort(item.getChildren()))).collect(Collectors.toList());
// }
return treeNodes;
}
/**
* 获取根节点
* @return /
*/
private List<TreeNodeAbstract<T>> getRootNode(Boolean isSort) {
List<TreeNodeAbstract<T>> rootNodeList = new ArrayList<>();
nodeList.forEach(item -> {
if (rootParentId.equals(item.getParentId())) {
rootNodeList.add(item);
}
});
if(isSort){
return sort(rootNodeList);
}
return rootNodeList;
}
private List<TreeNodeAbstract<T>> sort(List<TreeNodeAbstract<T>> rootNodeList){
return rootNodeList.stream().sorted(Comparator.comparing(TreeNodeAbstract::getSort,Comparator.nullsLast(Comparator.naturalOrder()))).collect(Collectors.toList());
}
/**
* 处理子集
* @param parentNode /
* @return /
*/
private TreeNodeAbstract<T> buildChildTree(TreeNodeAbstract<T> parentNode) {
List<TreeNodeAbstract<T>> childTree = new ArrayList<>();
nodeList.forEach(item -> {
// 如果相等便进入 处理子集的数据,子集如果还有就处理子集的子集,由里向外设置数据
if (parentNode.getId().equals(item.getParentId())) {
childTree.add(buildChildTree(item));
}
});
if(isSort){
parentNode.setChildren(sort(childTree));
}else {
parentNode.setChildren(childTree);
}
return parentNode;
}
}
import io.swagger.annotations.ApiModelProperty;
import java.util.List;
/**
* @author qb
* @version 1.0
* @since 2023/2/22 9:37
*/
public class TreeNodeAbstract<T> {
@ApiModelProperty(value = "id")
private String id;
@ApiModelProperty(value = "名称")
private String name;
@ApiModelProperty(value = "父级id")
private String parentId;
@ApiModelProperty(value = "排序")
private Integer sort;
@ApiModelProperty(value = "类型")
private String type;
@ApiModelProperty(value = "别名")
private String alias;
@ApiModelProperty(value = "子集")
private List<TreeNodeAbstract<T>> children;
@ApiModelProperty(value = "扩展字段")
private T ext;
@ApiModelProperty(value = "扩展数据集")
private Object extra;
public TreeNodeAbstract() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAlias() {
return alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
public List<TreeNodeAbstract<T>> getChildren() {
return children;
}
public void setChildren(List<TreeNodeAbstract<T>> children) {
this.children = children;
}
public T getExt() {
return ext;
}
public void setExt(T ext) {
this.ext = ext;
}
public Object getExtra() {
return extra;
}
public void setExtra(Object extra) {
this.extra = extra;
}
}
使用
List<TreeNode<String>> funAuthResults = baseMapper.funAuthList(hosId, areaId);
TreeBuild<String> treeBuild = new TreeBuild<>(funAuthResults);
List<TreeNode<String>> treeNodes = treeBuild.build();
结果
{
"code": 0,
"msg": "成功",
"data": [
{
"children": [
{
"children": [
{
"children": [],
"dictType": "test",
"dictCode": "test1-1-2",
"dictValue": "1-1-2"
},
{
"children": [
{
"children": [],
"dictType": "test",
"dictCode": "test-1-1-1",
"dictValue": "1-1-1-1"
}
],
"dictType": "test",
"dictCode": "test1-1-1",
"dictValue": "1-1-1"
}
],
"dictType": "test",
"dictCode": "test1-1",
"dictValue": "1-1"
},
{
"children": [
{
"children": [],
"dictType": "test",
"dictCode": "test-1-2-1",
"dictValue": "1-2-1"
}
],
"dictType": "test",
"dictCode": "test1-2",
"dictValue": "1-2"
}
],
"dictType": "test",
"dictCode": "test1",
"dictValue": "1"
},
{
"children": [
{
"children": [],
"dictType": "test",
"dictCode": "test2",
"dictValue": "2-1"
}
],
"dictType": "test",
"dictCode": "test2",
"dictValue": "2"
}
],
"ok": true
}
536

被折叠的 条评论
为什么被折叠?



