当前位置: 首页 > 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.mrgr.cn/news/2820.html

相关文章:

  • 零售业务产品系统应用架构设计(三)
  • 手持气象站的工作原理
  • Hadoop如何搭建计算和存储节点分离
  • Redis常用数据结构常用命令总览
  • 停车场管理系统设计与实现(源码+lw+部署文档+讲解等)
  • 秋招力扣Hot100刷题总结——回溯
  • pandas习题 040:初始值按40计算变化百分比
  • 工程建设现场管理智慧建造综合管理平台,智慧工地云平台源码
  • 【Rust光年纪】深入探索Rust语言的计算机视觉库:功能、特点与应用场景
  • 案例分析:常用的Java代码优化法则
  • 单例模式详细
  • IOS 11 通用Base控制器封装
  • Ruby简介
  • 本地项目git同步到线上
  • 【STM32 HAL库】寻迹小车 开环控制 状态机 TB6612+TCRT5000+HC-05
  • 关于白鳝存储过程技术话题
  • Java---二维数组
  • MongoDB 在 Java 中的使用教程
  • windows vs2022 MFC使用webview2嵌入网页
  • GDB的基本使用