Java“菜单“树递归-合并子树

一、问题描述

如下面截图,最顶级是“核心指标”,即树干,接下来是子树分支“空间品质”,“城镇人均住房面积(m2)”就是最后一级的子树。我们接下来要做的合并就是树干下面有分支,如果分支有已经存在相同的分支则比较下一级,一直到下一级不存在该分支则将其合并到该分支的子树下面。

二、树结构JSON文件

1、需要合并的JSON文件

{"catalog":0,"children":[{"catalog":0,"children":[{"catalog":0,"children":[],"iconCls":"icon-indicator","id":"d5bd3ecd-6596-49b5-b952-ec07be30b2ac","load":false,"node":true,"parentId":"2b4d8426-d712-42c6-b698-f33dee39dd31","sort":0.0,"text":"城镇人均住房面积(m2)"}],"iconCls":"icon-indicatorset","id":"e0d6e7a3-2d07-4285-b352-cf9b9a3eb7eb","load":false,"node":true,"parentId":"43b3b6fb-9677-4d13-a65d-46bbfdb2beee","sort":0.0,"text":"空间品质"}],"iconCls":"icon-indicatorset","id":"43b3b6fb-9677-4d13-a65d-46bbfdb2beee","load":false,"node":true,"parentId":"#","sort":0.0,"text":"核心指标"}
{"catalog":0,"children":[{"catalog":0,"children":[{"catalog":0,"children":[],"iconCls":"icon-indicator","id":"e96498ce-8f91-4f1f-9dd0-4c5269010116","load":false,"node":true,"parentId":"d5ee65d1-126d-4bfe-991b-aa062af9fd93","sort":0.0,"text":"新能源和可再生能源比例(%)"}],"iconCls":"icon-indicatorset","id":"a5a588d1-4c8a-45f2-837a-734d154d9903","load":false,"node":true,"parentId":"43b3b6fb-9677-4d13-a65d-46bbfdb2beee","sort":0.0,"text":"空间底线"}],"iconCls":"icon-indicatorset","id":"43b3b6fb-9677-4d13-a65d-46bbfdb2beee","load":false,"node":true,"parentId":"#","sort":0.0,"text":"核心指标"}

2、两个JSON文件的截图如下:

三、合并方法实现

public static TreeEntity appendTreeEntity(TreeEntity distEntity, TreeEntity sourEntity){

        if (distEntity.getId().equals(sourEntity.getId())){
            if (sourEntity.getChildren().size() <=0){
                return distEntity;
            }
            TreeEntity first = sourEntity.getChildren().first();
            List<TreeEntity> collect = distEntity.getChildren().stream().filter(item -> first.getId().equals(item.getId())).collect(Collectors.toList());
            if (collect.size() <= 0) { //不存在就拼接
                distEntity.getChildren().add(first);
            }else {
                TreeEntity sourEntityChild = sourEntity.getChildren().first();
                TreeEntity distEntityChild = distEntity.getChildren().stream().filter(item -> first.getId().equals(item.getId())).collect(Collectors.toList()).get(0);
                appendTreeEntity(distEntityChild,sourEntityChild);
            }
        }
        return distEntity;
    }

 四、测试类主方法

 public static void main(String[] args) {
        String msg = "{\"catalog\":0,\"children\":[{\"catalog\":0,\"children\":[{\"catalog\":0,\"children\":[],\"iconCls\":\"icon-indicator\",\"id\":\"d5bd3ecd-6596-49b5-b952-ec07be30b2ac\",\"load\":false,\"node\":true,\"parentId\":\"2b4d8426-d712-42c6-b698-f33dee39dd31\",\"sort\":0.0,\"text\":\"城镇人均住房面积(m2)\"}],\"iconCls\":\"icon-indicatorset\",\"id\":\"e0d6e7a3-2d07-4285-b352-cf9b9a3eb7eb\",\"load\":false,\"node\":true,\"parentId\":\"43b3b6fb-9677-4d13-a65d-46bbfdb2beee\",\"sort\":0.0,\"text\":\"空间品质\"}],\"iconCls\":\"icon-indicatorset\",\"id\":\"43b3b6fb-9677-4d13-a65d-46bbfdb2beee\",\"load\":false,\"node\":true,\"parentId\":\"#\",\"sort\":0.0,\"text\":\"核心指标\"}";
        String msg2 = "{\"catalog\":0,\"children\":[{\"catalog\":0,\"children\":[{\"catalog\":0,\"children\":[],\"iconCls\":\"icon-indicator\",\"id\":\"e96498ce-8f91-4f1f-9dd0-4c5269010116\",\"load\":false,\"node\":true,\"parentId\":\"d5ee65d1-126d-4bfe-991b-aa062af9fd93\",\"sort\":0.0,\"text\":\"新能源和可再生能源比例(%)\"}],\"iconCls\":\"icon-indicatorset\",\"id\":\"a5a588d1-4c8a-45f2-837a-734d154d9903\",\"load\":false,\"node\":true,\"parentId\":\"43b3b6fb-9677-4d13-a65d-46bbfdb2beee\",\"sort\":0.0,\"text\":\"空间底线\"}],\"iconCls\":\"icon-indicatorset\",\"id\":\"43b3b6fb-9677-4d13-a65d-46bbfdb2beee\",\"load\":false,\"node\":true,\"parentId\":\"#\",\"sort\":0.0,\"text\":\"核心指标\"}";

        TreeEntity distEntity = JSONObject.parseObject(msg,TreeEntity.class);
        TreeEntity sourEntity = JSONObject.parseObject(msg2,TreeEntity.class);
        TreeEntity treeEntity = appendTreeEntity(distEntity, sourEntity);
        System.out.println("合并结果:" +JSONObject.toJSONString(treeEntity));


    }

五、结果

1、合并结果:

{"catalog":0,"children":[{"catalog":0,"children":[{"catalog":0,"children":[],"iconCls":"icon-indicator","id":"d5bd3ecd-6596-49b5-b952-ec07be30b2ac","load":false,"node":true,"parentId":"2b4d8426-d712-42c6-b698-f33dee39dd31","sort":0.0,"text":"城镇人均住房面积(m2)"}],"iconCls":"icon-indicatorset","id":"e0d6e7a3-2d07-4285-b352-cf9b9a3eb7eb","load":false,"node":true,"parentId":"43b3b6fb-9677-4d13-a65d-46bbfdb2beee","sort":0.0,"text":"空间品质"},{"catalog":0,"children":[{"catalog":0,"children":[],"iconCls":"icon-indicator","id":"e96498ce-8f91-4f1f-9dd0-4c5269010116","load":false,"node":true,"parentId":"d5ee65d1-126d-4bfe-991b-aa062af9fd93","sort":0.0,"text":"新能源和可再生能源比例(%)"}],"iconCls":"icon-indicatorset","id":"a5a588d1-4c8a-45f2-837a-734d154d9903","load":false,"node":true,"parentId":"43b3b6fb-9677-4d13-a65d-46bbfdb2beee","sort":0.0,"text":"空间底线"}],"iconCls":"icon-indicatorset","id":"43b3b6fb-9677-4d13-a65d-46bbfdb2beee","load":false,"node":true,"parentId":"#","sort":0.0,"text":"核心指标"}

 2、结果截图:

六、封装类TreeEntity 

package com.xxx.smartdata.nri16n.imapnr.funcs.ssjd.provider.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;
import java.util.TreeSet;


public class TreeEntity implements Comparable<TreeEntity>, Serializable {
    private static final long serialVersionUID = -8367908181559979913L;
    private String id;
    private String text;
    private String iconCls;
    private String state;
    private String parentId;
    private int catalog;
    private float sort;
    @JsonIgnore
    private boolean load;
    private Object attributes;
    private boolean node;
    private TreeSet<TreeEntity> children;
    public TreeEntity() {
        this.node = true;
        this.children = new TreeSet();
    }

    public boolean getNode() {
        return this.node;
    }

    public void setNode(boolean node) {
        this.node = node;
    }

    public TreeEntity(String sId, String sText, String sParentId, float iSort, String sIcon) {
        this();
        this.id = sId;
        this.text = sText;
        this.parentId = sParentId;
        this.sort = iSort;
        if (sIcon != null) {
            if (sIcon.isEmpty()) {
                sIcon = "icon-blank";
            }

            this.iconCls = sIcon;
        }

    }

    public int compareTo(TreeEntity o) {
        if (o == null) {
            return 1;
        } else if (this == o) {
            return 0;
        } else {
            int ir = Integer.compare(this.catalog, o.catalog);
            if (ir == 0) {
                ir = Float.compare(this.sort, o.sort);
            }

            String stext;
            if (ir == 0) {
                stext = this.text;
                if (stext == null) {
                    stext = "";
                }

                ir = stext.compareTo(o.text == null ? "" : o.text);
            }

            if (ir == 0) {
                stext = this.id;
                if (stext == null) {
                    stext = "";
                }

                ir = stext.compareTo(o.id == null ? "" : o.id);
            }

            return ir == 0 ? 1 : ir;
        }
    }

    public static String getStateClosed() {
        return "closed";
    }

    public static String getStateOpen() {
        return "open";
    }

    public static String getNullIcon() {
        return "icon-blank";
    }

    public static String getFolderIcon() {
        return "tree-folder";
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getText() {
        return this.text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getIconCls() {
        return this.iconCls;
    }

    public void setIconCls(String iconCls) {
        this.iconCls = iconCls;
    }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getParentId() {
        return this.parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public float getSort() {
        return this.sort;
    }

    public void setSort(float sort) {
        this.sort = sort;
    }

    public boolean isLoad() {
        return this.load;
    }

    public void setLoad(boolean load) {
        this.load = load;
    }

    public Object getAttributes() {
        return this.attributes;
    }

    public void setAttributes(Object attributes) {
        this.attributes = attributes;
    }

    public TreeSet<TreeEntity> getChildren() {
        return this.children;
    }

    public void setChildren(TreeSet<TreeEntity> children) {
        if (children == null) {
            this.children.clear();
        } else {
            this.children = children;
        }

    }

    public int getCatalog() {
        return this.catalog;
    }

    public void setCatalog(int catalog) {
        this.catalog = catalog;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof TreeEntity)) {
            return false;
        } else {
            TreeEntity other = (TreeEntity)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label111: {
                    Object this$id = this.getId();
                    Object other$id = other.getId();
                    if (this$id == null) {
                        if (other$id == null) {
                            break label111;
                        }
                    } else if (this$id.equals(other$id)) {
                        break label111;
                    }

                    return false;
                }

                Object this$text = this.getText();
                Object other$text = other.getText();
                if (this$text == null) {
                    if (other$text != null) {
                        return false;
                    }
                } else if (!this$text.equals(other$text)) {
                    return false;
                }

                Object this$iconCls = this.getIconCls();
                Object other$iconCls = other.getIconCls();
                if (this$iconCls == null) {
                    if (other$iconCls != null) {
                        return false;
                    }
                } else if (!this$iconCls.equals(other$iconCls)) {
                    return false;
                }

                label90: {
                    Object this$state = this.getState();
                    Object other$state = other.getState();
                    if (this$state == null) {
                        if (other$state == null) {
                            break label90;
                        }
                    } else if (this$state.equals(other$state)) {
                        break label90;
                    }

                    return false;
                }

                label83: {
                    Object this$parentId = this.getParentId();
                    Object other$parentId = other.getParentId();
                    if (this$parentId == null) {
                        if (other$parentId == null) {
                            break label83;
                        }
                    } else if (this$parentId.equals(other$parentId)) {
                        break label83;
                    }

                    return false;
                }

                if (this.getCatalog() != other.getCatalog()) {
                    return false;
                } else if (Float.compare(this.getSort(), other.getSort()) != 0) {
                    return false;
                } else if (this.isLoad() != other.isLoad()) {
                    return false;
                } else {
                    label72: {
                        Object this$attributes = this.getAttributes();
                        Object other$attributes = other.getAttributes();
                        if (this$attributes == null) {
                            if (other$attributes == null) {
                                break label72;
                            }
                        } else if (this$attributes.equals(other$attributes)) {
                            break label72;
                        }

                        return false;
                    }

                    if (this.getNode() != other.getNode()) {
                        return false;
                    } else {
                        Object this$children = this.getChildren();
                        Object other$children = other.getChildren();
                        if (this$children == null) {
                            if (other$children != null) {
                                return false;
                            }
                        } else if (!this$children.equals(other$children)) {
                            return false;
                        }

                        return true;
                    }
                }
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof TreeEntity;
    }



    public String toString() {
        return "TreeEntity(id=" + this.getId() + ", text=" + this.getText() + ", iconCls=" + this.getIconCls() + ", state=" + this.getState() + ", parentId=" + this.getParentId() + ", catalog=" + this.getCatalog() + ", sort=" + this.getSort() + ", load=" + this.isLoad() + ", attributes=" + this.getAttributes() + ", node=" + this.getNode() + ", children=" + this.getChildren() + ")";
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值