一、问题描述

如下面截图,最顶级是“核心指标”,即树干,接下来是子树分支“空间品质”,“城镇人均住房面积(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;@JsonIgnoreprivate 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() + ")";}}

Java“菜单“树递归-合并子树相关推荐

  1. java递归把list菜单列表转为菜单树

    java递归把列表转为菜单树 菜单实体类 package com.utils.menu;import java.util.List;public class Menu {private String ...

  2. 常见数据结构和算法实现(排序/查找/数组/链表/栈/队列/树/递归/海量数据处理/图/位图/Java版数据结构)

    常见数据结构和算法实现(排序/查找/数组/链表/栈/队列/树/递归/海量数据处理/图/位图/Java版数据结构) 数据结构和算法作为程序员的基本功,一定得稳扎稳打的学习,我们常见的框架底层就是各类数据 ...

  3. 树形结构:二叉树,分治,合并子树,递归

    我们学习树的时候,一些地方用到了递归,但是可能没意识到这里面都是分治的思想 ============================================================== ...

  4. 菜单转换Java_java递归菜单树转换成pojo对象

    package com.cjonline.foundation.authority.pojo; import java.util.ArrayList; import java.util.Collect ...

  5. Mybatis通过colliection属性递归获取菜单树

    1.现有商品分类数据表category结构如下,三个字段都为varchar类型 2.创建商品分类对应的数据Bean /*** */ package com.xdw.dao;import java.ut ...

  6. Java多级菜单树转为面包屑

    菜单树json格式 {"metricsCategoryId": null,"metricsCategoryCode": null,"metricsCa ...

  7. Bootstrap系列之treeview实现菜单树

    本博客,介绍通过Bootstrap的treeview插件实现菜单树的功能. treeview链接:http://www.htmleaf.com/Demo/201502141380.html ORM框架 ...

  8. Java开发 - 树(二叉树,二叉排序树,红黑树)

    目录 前言 你好,认识一下,我是树 二叉树与二叉排序树 二叉排序树特点 为什么说二叉排序树查询效率要高于链表呢? 元素的类型 比较器 手写二叉排序树 定义一棵二叉树 增加元素 查询元素 修改元素 删除 ...

  9. Tunnel Warfare(HDU1540+线段树+区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目: 题意:总共有n个村庄,有q次操作,每次操作分为摧毁一座村庄,修复一座村庄,和查询与询问的 ...

最新文章

  1. C++ 和C 语言混合代码导致的问题
  2. docker环境下solr6.0配置(中文分词+拼音)
  3. php mysql增改删_PHP分享:如何实现MySQL的增加删除修改查看
  4. 深入理解python.md_linux-深入理解python.pdf
  5. opencv1-安装及资料
  6. 从IBM和SUN分析当前SOA公司现状
  7. 简单实用的Android ORM框架TigerDB
  8. ofo(小黄车)项目分析
  9. wmware虚拟网卡 VMnet8 VMnet1未识别网络解决方法
  10. keil软件下载安装与新建工程
  11. ActionScript快速对照表
  12. 尚硅谷 硅谷外卖_关于多样性,硅谷未能想到与众不同
  13. css控制图片拉伸不变形,css+background实现 图片宽高自适应,拉伸裁剪不变形
  14. 在kafka中,可以这么理解topic,partition,broker
  15. 机器学习为什么重要_机器学习:它是如何工作的; 更重要的是,为什么它起作用?...
  16. 【51nod 1326】遥远的旅途【最短路】
  17. 二十四、冷战和消费主义
  18. 什么样的男生值得交往一生
  19. 继电器为什么要并联二极管
  20. PAT 乙级 1047 团体编程赛 python

热门文章

  1. 【CG物理模拟系列】开篇:介绍(下)
  2. [笔记] 关于通过鼠标滚轮设置缩放的技巧
  3. http返回码301、302、307、305含义和区别
  4. docker常见面试题
  5. 【小程序源码】同名在线查询系统
  6. PPT基础(三十)图片的特殊效果
  7. 【Nodejs】448- 深入学习 Node.js Buffer
  8. 生鲜配送APP软件开发快速制作
  9. 概率DP——BZOJ4008 [HNOI2015]亚瑟王
  10. mac上优秀的键盘改键神器:Karabiner Elements完美支持m1芯片详细教程解析