java实现多级目录树(递归实现)

一.应用场景

有时候需要我们后台给前台传树结构的数据,要怎么查询? 怎么返回数据呢?

二.数据库表设计以及数据内容(以部门举例)

id            主键
parent_id     父级部门id
depart_name   部门名词
sort          部门排序

在这里插入图片描述

三.实体类

@Data
public class KunKun implements Serializable {
    private static final long serialVersionUID = 1L;

	@ApiModelProperty("部门id")
	private String id;
	
	@ApiModelProperty("父级部门id")
	private String parentId;
	
	@ApiModelProperty("机构/部门名称")
	private String departName;
	
	@ApiModelProperty("子级部门的集合")
	private List<KunKun> children;
}

四.代码实现

public void selectKunTree() {
	 //查询部门信息,获取全部部门信息
	List<KunKun> kunList = xxxxMapper.selectList() ;
	
	//组装成树形结构
	//找到所有的一级分类(也就是树的根节点)
	List<KunKun> ikuns = kunList.stream().filter(ikun ->
		ikun.getParentId() == 0
		).map((menu) -> {
			menu.setChildren(getChildrens(menu,kunList));
			return menu;
		}).sorted((menu1,menu2)-> {//如果你查询的时候就排好序了就不用写这里
			return menu1.getSort() - menu2.getSort();
		}).collect(Collectors.tolist());
		
	//返回树结构的list
	return ikuns;
 }


//递归方法
private List<KunKun> getChildrens(KunKun root,List<KunKun> all){
	List<KunKun> children = all.stream().filter(ikun -> { 
		return ikun.getParentId() == root.getId();
	}).map(ikun  -> { //找子部门
		ikun.setChildren(getChildrens(ikun ,all));
		return categoryEntity;
	}).sorted( (menu1 ,menu2)
		return menu1.getSort - menu2.getSort() 
	}).collect(Collectors .tolist());
	
	return children;
}

=========================

多个一级id场景

在这里插入图片描述

实现思路

获取所有的分类。
获取所有分类的id集合。使用stream()实现,stream使用教程
获取一级分类信息。同样使用stream()实现。
循环一级分类,在循环中将一级分类添加子分类,并且将一级分类加入返回的树结构中(备注:不加入返回的树结构中也行,直接返回步骤3的分类信息一样)。
重点是步骤4中将一级分类添加子分类,并且子分类在添加子子分类,子子分类再添加子子子分类··········等等,实现过程使用递归即可。
步骤5的实现过程:写一个递归方法,往当前节点添加子节点,首先获取当前节点的字节点集合,然后把这个集合放入到当前节点子节点属性中,接着再次调用当前递归的方法,把刚获取到的子节点当成新当前节点,获取新当前节点的新子节点,注意再次调用当前递归的方法,把刚获取到的子节点当成新当前节点之前首先判断新当前节点有没有子节点(判断方法:获取当前节点的字节点数组,根据数组的size()>0?判断是否有子节点),如果没有就不用递归。
总结:1-4是数据准备,5-6是实现递归(当前节点添加子节点的递归)。

代码展示

  1. 主要思路代码
@GetMapping("/list")
    public Result list() {
        //所有的分类
        List<Category> categoryList = categoryService.list();
        //所有分类id集合
        List<String> idList = categoryList.stream().map(Category::getId).collect(Collectors.toList());
        //返回的树分类结果
        List<Category> treeCategory = new ArrayList<>();
        //一级分类目录
        List<Category> categories = categoryList.stream().filter(category -> !idList.contains(category.getParentId())).collect(Collectors.toList());
        //循环当前一级分类目录
        for (Category category : categories) {
            //给当前分类节点 添加 子分类节点
            addChild(categoryList,category);
            //当前分类添加完子节点分类之后,添加到返回的树结构中
            treeCategory.add(category);
        }
        //把返回的树结构返回
        return Result.success(categories);
    }

    /**
     * 给当前分类节点 添加 子分类节点
     * @param categoryList 所有的分类
     * @param category 当前分类节点
     */
    public void addChild( List<Category> categoryList,Category category){
        //循环所有的分类,获取当前节点的所有子节点
        List<Category> categoryListChild = categoryList.stream().filter(category1 -> category1.getParentId().equals(category.getId())).collect(Collectors.toList());
        //把当前分类的子节点添加到当前分类
        category.setChildList(categoryListChild);
        //再次调用本方法,把当前节点的子节点当成当前节点,继续添加子节点,备注:这样会造成一直循环
        categoryListChild.forEach(category1 -> {
            //添加一步,判断当前节点是否有子节点,没有就不循环递归
            if (haveChild(categoryList,category1)){
            	addChild(categoryList,category1);
 			}
        });
    }

    /**
     * 判断当前节点 是否存在 子节点
     * @param categoryList 所有的分类
     * @param category 当前节点
     */
    public boolean haveChild( List<Category> categoryList,Category category){
        //获取当前节点的子节点
        List<Category> categoryListChild = categoryList.stream().filter(category1 -> category1.getParentId().equals(category.getId())).collect(Collectors.toList());
        //子节点大于0则存在,否则不存在
        return categoryListChild==null&&categoryListChild.size()>0;

  1. 实体类代码展示
    备注:实体类代码中一定要有一个子节点数组。实体类对用最上面的实体类图片
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class Category implements Serializable {

    private static final long serialVersionUID = 1L;
    @TableId(value = "id", type = IdType.ID_WORKER_STR)
    private String id;

    /**
     * 类别名称
     */
    private String name;

    /**
     * 上级id
     */
    private String parentId;

    /**
     * 分类级别
     */
    private Integer type;
    /**
     * 子节点数组
     */
    @TableField(exist = false)
    private List<Category> childList;
}

##吧数据处理返回:

{
    "code": 200,
    "msg": "操作成功",
    "data": [
        {
            "id": "1",
            "name": "电子产品",
            "parentId": "0",
            "type": 1,
            "childList": [
                {
                    "id": "5",
                    "name": "笔记本电脑",
                    "parentId": "1",
                    "type": 2,
                    "childList": [
                        {
                            "id": "21",
                            "name": "联想笔记本",
                            "parentId": "5",
                            "type": 3,
                            "childList": []
                        },
                        {
                            "id": "22",
                            "name": "外星人笔记本",
                            "parentId": "5",
                            "type": 3,
                            "childList": []
                        },
                        {
                            "id": "23",
                            "name": "戴尔笔记本",
                            "parentId": "5",
                            "type": 3,
                            "childList": []
                        }
                    ]
                },
                {
                    "id": "6",
                    "name": "手机",
                    "parentId": "1",
                    "type": 2,
                    "childList": [
                        {
                            "id": "24",
                            "name": "苹果手机",
                            "parentId": "6",
                            "type": 3,
                            "childList": []
                        },
                        {
                            "id": "25",
                            "name": "菠萝手机",
                            "parentId": "6",
                            "type": 3,
                            "childList": []
                        }
                    ]
                },
                {
                    "id": "7",
                    "name": "耳机",
                    "parentId": "1",
                    "type": 2,
                    "childList": []
                },
                {
                    "id": "8",
                    "name": "电子烟",
                    "parentId": "1",
                    "type": 2,
                    "childList": []
                }
            ]
        },
        {
            "id": "2",
            "name": "生活用品",
            "parentId": "0",
            "type": 1,
            "childList": [
                {
                    "id": "10",
                    "name": "椅子",
                    "parentId": "2",
                    "type": 2,
                    "childList": []
                },
                {
                    "id": "11",
                    "name": "床",
                    "parentId": "2",
                    "type": 2,
                    "childList": []
                },
                {
                    "id": "19",
                    "name": "牙膏",
                    "parentId": "2",
                    "type": 2,
                    "childList": []
                },
                {
                    "id": "20",
                    "name": "牙刷",
                    "parentId": "2",
                    "type": 2,
                    "childList": []
                },
                {
                    "id": "9",
                    "name": "桌子",
                    "parentId": "2",
                    "type": 2,
                    "childList": []
                }
            ]
        },
        {
            "id": "3",
            "name": "卫生用品",
            "parentId": "0",
            "type": 1,
            "childList": [
                {
                    "id": "12",
                    "name": "卫生纸",
                    "parentId": "3",
                    "type": 2,
                    "childList": []
                },
                {
                    "id": "13",
                    "name": "湿巾",
                    "parentId": "3",
                    "type": 2,
                    "childList": []
                }
            ]
        },
        {
            "id": "4",
            "name": "学习用品",
            "parentId": "0",
            "type": 1,
            "childList": [
                {
                    "id": "14",
                    "name": "电子书",
                    "parentId": "4",
                    "type": 2,
                    "childList": []
                },
                {
                    "id": "15",
                    "name": "听力光盘",
                    "parentId": "4",
                    "type": 2,
                    "childList": []
                },
                {
                    "id": "16",
                    "name": "实体书",
                    "parentId": "4",
                    "type": 2,
                    "childList": []
                },
                {
                    "id": "17",
                    "name": "钢笔",
                    "parentId": "4",
                    "type": 2,
                    "childList": []
                },
                {
                    "id": "18",
                    "name": "笔记本子",
                    "parentId": "4",
                    "type": 2,
                    "childList": []
                }
            ]
        }
    ]
}

其他方式

@Override
	public List<AmlEquityRelVO> queryChildList(String custName) {
		List<AmlEquityRelVO> list = dao.queryChildList(custName);
		return getChildTreeList(list,custName);
	}

/**
	 * 把列表转换为树结构
	 *
	 * @param originalList 原始list数据
	 * @param keyName 作为唯一标示的字段名称
	 * @return 组装后的集合
	 */
	public  List<AmlEquityRelVO> getChildTreeList(List<AmlEquityRelVO> originalList,String custName) {
	    // 获取根节点,即找出父节点为空的对象
	    List<AmlEquityRelVO> topList = new ArrayList<>();
	    for (int i = 0; i < originalList.size(); i++) {
	       AmlEquityRelVO t = originalList.get(i);
	        String parentName = t.getParentName();
	        if (custName.equals(parentName)) {
	            topList.add(t);
	        }
	    }

	    // 将根节点从原始list移除,减少下次处理数据
	    originalList.removeAll(topList);

	    // 递归封装树
	    fillChildTree(topList, originalList);

	    return topList;
	}

/**
	 * 封装树
	 *
	 * @param parentList 要封装为树的父对象集合
	 * @param originalList 原始list数据
	 * @param keyName 作为唯一标示的字段名称
	 * @param parentFieldName 模型中作为parent字段名称
	 * @param childrenFieldName 模型中作为children的字段名称
	 */
	private void fillChildTree(List<AmlEquityRelVO> parentList, List<AmlEquityRelVO> originalList) {
	    for (int i = 0; i < parentList.size(); i++) {
	        List<AmlEquityRelVO> children = fillChildChildren(parentList.get(i), originalList);
	        if (children.isEmpty()) {
	            continue;
	        }
	        originalList.removeAll(children);
	        fillChildTree(children, originalList);
	    }
	}


/**
	 * 封装子对象
	 *
	 * @param parent 父对象
	 * @param originalList 待处理对象集合
	 * @param keyName 作为唯一标示的字段名称
	 * @param parentFieldName 模型中作为parent字段名称
	 * @param childrenFieldName 模型中作为children的字段名称
	 */
	private List<AmlEquityRelVO> fillChildChildren(AmlEquityRelVO parent, List<AmlEquityRelVO> originalList) {
	    List<AmlEquityRelVO> childList = new ArrayList<>();
	    //String parentName = parent.getChildName();
	    String parentId = parent.getId();
	    for (int i = 0; i < originalList.size(); i++) {
	    	AmlEquityRelVO t = originalList.get(i);
	        //String childParentName = t.getParentName();
	        String childParentId = t.getParentId();
	        if (parentId.equals(childParentId)) {
	            childList.add(t);
	        }
	    }
	    if (!childList.isEmpty()) {
	    	parent.setChildren(childList);
	    }
	    return childList;
	}















=======
public class AVO extends A {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7276047523099274236L;
	private String id;
	private String childId;
	private String childName;
	private String type;
	private String parentId;
	private String parentName;
	private BigDecimal ratio;
	private BigDecimal amount;
	private String name;
	
	 private List<A> children = new ArrayList<A>() ;
}

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值