List全部数据组合成树形结构
实体类:
package com.test.vo;import lombok.Data;import java.util.List;/**
* 国民经济行业分类树形返回参数
*
* @author lmp
* @date 2021-04-19
*/@Datapublic class IndustryVo {
private static final long serialVersionUID=1L;/**ID*/
private String IndustryId;/** 字典值 */
private Stringvalue;/** 选项名称 */
private String label;/** 选项名称 */
private String parentId;/** 下级节点 */
private List<IndustryVo> children;
}
Controller类
@GetMapping("/getAllData")@ResponseBody@ApiOperation("查询所有行业数据-接口")public AjaxResultgetAllData(){return AjaxResult.success(getChildPerms(industryClient.getAllIndustry(), null));}/**
* 根据父节点的ID获取所有子节点
*
* @param list 分类表
* @param parentId 传入的父节点ID
* @return String
*/public List<IndustryVo>getChildPerms(List<IndustryVo> list, String parentId){
List<IndustryVo> returnList=newArrayList<IndustryVo>();for(Iterator<IndustryVo> iterator= list.iterator(); iterator.hasNext();){
IndustryVo t=(IndustryVo) iterator.next();// 一、根据传入的某个父节点ID,遍历该父节点的所有子节点if(t.getParentId()== parentId){recursionFn(list, t);
returnList.add(t);}}return returnList;}/**
* 递归列表
*
* @param list
* @param t
*/privatevoidrecursionFn(List<IndustryVo> list, IndustryVo t){// 得到子节点列表
List<IndustryVo> childList=getChildList(list, t);
t.setChildren(childList);for(IndustryVo tChild: childList){if(hasChild(list, tChild)){// 判断是否有子节点
Iterator<IndustryVo> it= childList.iterator();while(it.hasNext()){
IndustryVo n=(IndustryVo) it.next();recursionFn(list, n);}}}}/**
* 得到子节点列表
*/private List<IndustryVo>getChildList(List<IndustryVo> list, IndustryVo t){
List<IndustryVo> tlist=newArrayList<IndustryVo>();
Iterator<IndustryVo> it= list.iterator();while(it.hasNext()){
IndustryVo n=(IndustryVo) it.next();if(n.getParentId()!=null&& n.getParentId().equals(t.getIndustryId())){
tlist.add(n);}}return tlist;}/**
* 判断是否有子节点
*/privatebooleanhasChild(List<IndustryVo> list, IndustryVo t){returngetChildList(list, t).size()>0?true:false;}