当前位置: 首页 > news >正文

Java:封装树结构

实体类

public class DictTreeselectVO {private String value;private String label;/*** 节点*/private String parentId;private List<DictTreeselectVO> children = new ArrayList<DictTreeselectVO>();public String getValue() {return value;}public void setValue(String value) {this.value = value;}public String getLabel() {return label;}public void setLabel(String label) {this.label = label;}public String getParentId() {return parentId;}public void setParentId(String parentId) {this.parentId = parentId;}public List<DictTreeselectVO> getChildren() {return children;}public void setChildren(List<DictTreeselectVO> children) {this.children = children;}
}

返回结果

public class TreeSelectT implements Serializable
{private static final long serialVersionUID = 1L;/** 节点ID */private String id;/** 节点名称 */private String label;/** 子节点 */@JsonInclude(JsonInclude.Include.NON_EMPTY)private List<TreeSelectT> children;public TreeSelectT(){}public TreeSelectT(DictTreeselectVO dictTreeselectVO){this.id = dictTreeselectVO.getValue();this.label = dictTreeselectVO.getLabel();this.children = dictTreeselectVO.getChildren().stream().map(TreeSelectT::new).collect(Collectors.toList());}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getLabel() {return label;}public void setLabel(String label) {this.label = label;}public List<TreeSelectT> getChildren() {return children;}public void setChildren(List<TreeSelectT> children) {this.children = children;}
}

工具类

/**
* 创建树结构
* @param treeselectList
* @return
*/
public static List<TreeSelectT> buildTreeSelect(List<DictTreeselectVO> treeselectList){List<DictTreeselectVO> treeselectVOList = buildDictTreeselect(treeselectList);return treeselectVOList.stream().map(TreeSelectT::new).collect(Collectors.toList());
}/**
* 构建前端所需要树结构
* @param treeselectList
* @return
*/
private static List<DictTreeselectVO> buildDictTreeselect(List<DictTreeselectVO> treeselectList){List<DictTreeselectVO> returnList = new ArrayList<DictTreeselectVO>();List<String> tempList = treeselectList.stream().map(DictTreeselectVO::getValue).collect(Collectors.toList());for (DictTreeselectVO dictTreeselectVO : treeselectList) {// 如果是顶级节点, 遍历该父节点的所有子节点if (!tempList.contains(dictTreeselectVO.getParentId())) {recursionFn(treeselectList, dictTreeselectVO);returnList.add(dictTreeselectVO);}}if (returnList.isEmpty()){returnList = treeselectList;}return returnList;
}/*** 递归列表* @param list* @param t*/
private static void recursionFn (List<DictTreeselectVO> list, DictTreeselectVO t){// 得到子节点列表List<DictTreeselectVO> childList = getChildList(list, t);t.setChildren(childList);for (DictTreeselectVO tChild : childList) {if (hasChild(list, tChild)){recursionFn(list, tChild);}}
}/*** 得到子节点列表* @param list* @param t* @return*/
private static List<DictTreeselectVO> getChildList(List<DictTreeselectVO> list, DictTreeselectVO t)
{List<DictTreeselectVO> tlist = new ArrayList<DictTreeselectVO>();Iterator<DictTreeselectVO> it = list.iterator();while (it.hasNext()){DictTreeselectVO n = (DictTreeselectVO) it.next();if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(t.getValue())){tlist.add(n);}}return tlist;
}/*** 判断是否有子节点*/
private static boolean hasChild(List<DictTreeselectVO> list, DictTreeselectVO t){return getChildList(list, t).size() > 0;
}
http://www.lryc.cn/news/428599.html

相关文章:

  • linux内核 pintrl子系统
  • 网络通信要素
  • day03_作业
  • pyinstaller程序打包,资源嵌入exe
  • 如何使用 OCR 和 GPT-4o mini 轻松提取收据信息
  • go 事务
  • C,数据结构,多进程线程,网络编程面试题总结
  • 【Cesium学习】着色器详解【待进一步总结】
  • 【3】静态路由(Static routing)
  • 阿里声音项目Qwen2-Audio的部署安装,在服务器Ubuntu22.04系统——点动科技
  • RAG(检索增强生成)
  • AcWing848有向图的拓扑排序
  • 猫咪掉毛很严重,家中猫毛该如何清理?快来看资深铲屎官经验分享
  • Midjourney进阶-反推与优化提示词(案例实操)
  • 大公报发表欧科云链署名文章:发行港元稳定币,建Web3.0新生态
  • Mybatis的一些常用知识点(面试)
  • stm32—ADC
  • 【微信小程序】吐槽生态之云开发服务端能力不足
  • AnimateDiff论文解读
  • C/C++控制台贪吃蛇游戏的实现
  • Linux 升级安装 Weblogic-补丁!
  • 苍鹰来啦!快来看呀!NGO-BiTCN-BiGRU-Attention北方苍鹰算法优化多重双向深度学习回归预测
  • 关于WebSocket必知必会的知识点
  • Go 1.19.4 Sort排序进阶-Day 12
  • python-求距离(赛氪OJ)
  • 《第二十一章 传感器与定位 - 传感器应用》
  • Windows系统命令
  • C语言函数递归
  • 【python数据分析11】——Pandas统计分析(分组聚合进行组内计算)
  • 高性能web服务器