JAVA获取树形结构的两种方式

在日常开发或工作需求中,我们需要构建菜单、树形结构,在数据库中使用父id进行标识。使得逻辑结构更加清晰。

前置准备:构建实体类

​
public class CategoryEntity implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * 分类id
	 */
	@TableId
	private Long catId;
	/**
	 * 分类名称
	 */
	private String name;
	/**
	 * 父分类id
	 */
	private Long parentCid;
	/**
	 * 层级
	 */
	private Integer catLevel;
	/**
	 * 是否显示[0-不显示,1显示]
	 */
	private Integer showStatus;
	/**
	 * 排序
	 */
	private Integer sort;
	/**
	 * 图标地址
	 */
	private String icon;
	/**
	 * 计量单位
	 */
	private String productUnit;
	/**
	 * 商品数量
	 */
	private Integer productCount;

​

stream流递归

使用stream流递归方式:在实体类上加入子菜单

/**
 * 子菜单
 */
@TableField(exist = false)
private List<CategoryEntity> categoryChilds;

还可以自定义排序等其他功能,可以自行完善

    @Override
    public List<CategoryEntity> listByTree2() {
        //先查询出所有
        List<CategoryEntity> alls = categoryDao.selectList(null);
        //查询出所有的父级菜单
        List<CategoryEntity> collect = alls.stream().filter(all -> all.getParentCid()==0)
                .map(categoryParent -> {
                    categoryParent.setCategoryChilds(getChilds(categoryParent, alls));
                    return categoryParent;
                }).collect(Collectors.toList());
        return collect;
    }

    private List<CategoryEntity> getChilds(CategoryEntity root, List<CategoryEntity> alls) {
        List<CategoryEntity> collect = alls.stream().filter(all -> {
            return all.getParentCid().equals(root.getCatId());
        }).map(categoryList -> {
            categoryList.setCategoryChilds(getChilds(categoryList, alls));
            return categoryList;
        }).collect(Collectors.toList());
        return collect;

    }

hutool工具包

使用Hutool工具包效率更高,但是需要掌握Stream流递归

 导入依赖

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.22</version>
        </dependency>

 

public List<Tree<Integer>> listByTree() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));
        System.out.println(System.currentTimeMillis());
        List<CategoryEntity> entityList = categoryDao.selectList(null);
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("cid");
        treeNodeConfig.setNameKey("name");
        treeNodeConfig.setParentIdKey("parent_cid");
        treeNodeConfig.setDeep(3);
        List<Tree<Integer>> treeList = TreeUtil.build(entityList, 0, treeNodeConfig, (treeNode, tree) -> {
            tree.setId(treeNode.getCatId().intValue());
            tree.setName(treeNode.getName());
            tree.setParentId(treeNode.getParentCid().intValue());
            tree.putExtra("level", treeNode.getCatLevel());

        });
        Date date1 = new Date();
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat1.format(date1));
        System.out.println(System.currentTimeMillis());
        return treeList;
    }

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值