java接口如何返回Tree树状结构数据

在实际的开发中,我们数据库表经常是会有层级关系的数据,比如一级二级三级菜单,通过parentId关联,而前端又需要后端一次性返回所有的数据及层级关系,即树状结构,后端如何吃查询并正确返回呢

/**
 * @Author: chuxia0811
 * @Date: 2022/6/20 22:37
 * @Description :如何封装返回一个树桩结构的数据
 */
public class ResponseTreeDemo {
    public static void main(String[] args) {
        Group group1 = new Group(1, "分组1", null, 1);
        Group group2 = new Group(2, "分组2", null, 1);
        Group group3 = new Group(3, "分组3", 2, 2);
        Group group4 = new Group(4, "分组4", 3, 3);
        List<Group> groups = new ArrayList<>();
        // 假设从数据库中查询出所有的group分组,现在需要根据level和parentGroupId 返回树状结构
        // 假设group1 group2是一级,group3挂在group2下面,为2级,group4又挂在group3下面,为3级
        groups.add(group1);
        groups.add(group2);
        groups.add(group3);
        groups.add(group4);

        Map<Integer, Group> groupMap = groups.stream().map(Group::new).collect(Collectors.toMap(Group::getGroupId, Function.identity()));
        Set<Integer> hasGroupId = new HashSet<>();
        for (Map.Entry<Integer, Group> groupTree : groupMap.entrySet()) {
            Group mapvalue = groupTree.getValue();
            Group groupEntity = groupMap.get(mapvalue.getParentGroupId());
            if (groupEntity != null) {
                hasGroupId.add(mapvalue.getGroupId());
                groupEntity.addChild(mapvalue);
            }
        }
        for (Integer remove : hasGroupId) {
            groupMap.remove(remove);
        }
        System.out.println("返回的树状结构如下:");
        System.out.println(groupMap.toString());


    }
}
class Group {
    private Integer groupId;

    private String groupName;

    private Integer parentGroupId;

    private Integer level;

    private List<Group> child;

    public void addChild(Group value) {
        if (child == null) {
            child = new ArrayList<>();
        }
        child.add(value);
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    public Integer getGroupId() {
        return groupId;
    }

    public void setGroupId(Integer groupId) {
        this.groupId = groupId;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public Integer getParentGroupId() {
        return parentGroupId;
    }

    public void setParentGroupId(Integer parentGroupId) {
        this.parentGroupId = parentGroupId;
    }

    public Group(Group value) {
        this.groupId = value.groupId;
        this.groupName = value.groupName;
        this.parentGroupId = value.parentGroupId;
        this.level = value.level;
    }

    public Group(Integer groupId, String groupName, Integer parentGroupId, Integer level) {
        this.groupId = groupId;
        this.groupName = groupName;
        this.parentGroupId = parentGroupId;
        this.level = level;
    }

    @Override
    public String toString() {
        return "Group{" +
                "groupId=" + groupId +
                ", groupName='" + groupName + '\'' +
                ", parentGroupId=" + parentGroupId +
                ", level=" + level +
                ", child=" + child +
                '}';
    }
}

运行结果如下:

{1=Group{groupId=1, groupName='分组1', parentGroupId=null, level=1, child=null}, 
2=Group{groupId=2, groupName='分组2', parentGroupId=null, level=1, 
child=[Group{groupId=3, groupName='分组3', parentGroupId=2, level=2, 
child=[Group{groupId=4, groupName='分组4', parentGroupId=3, level=3, child=null}]}]}}

你可能感兴趣的:(java笔记,java,树,Tree)