java实现树形结构数据

  1. 树形结构的数据必须要有一个父id,创建一个树形结构的实体类,里面要有List childNode,用来存放子节点
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class RoleTree extends BaseEntity {
	private Long id;
    /**
     * 角色名称
     */
    private String roleName;
    /**
     * 角色编码
     */
    private String roleCode;
    /**
     * 路径
     */
    private String rolePath;
    /**
     * 父级id
     */
    private Long parentId;
    /**
     * 是否启用
     */
    private Boolean isEnabled;
    /**
     * 子角色
     */
    private List<RoleTree> childNode;
	// 这里的构造函数主要是为了把数据库的实体对应的数据赋值给我写得这个树形结构的实体
    public RoleTree(Role role){
        this.Id = role.getId();
        this.roleName = role.getRoleName();
        this.roleCode = role.getRoleCode();
        this.rolePath = role.getRolePath();
        this.parentId = role.getParentId();;
        this.isEnabled = role.getIsEnabled();
        this.childNode = new ArrayList<>();
    }
}

  1. 重点,业务逻辑
    public List<RoleTree> getRoleTree() {
        // 查询全部角色
        List<Role> roles = baseMapper.selectList(Wrappers.emptyWrapper());
        // 用来存放非跟节点 Map<父id,List<RoleTree>>,同一个父的RoleTree保存在一个List<RoleTree>集合里面
        Map<Long, List<RoleTree>> childMap = new HashMap<>();
        // 用来存放返回的结果集
        List<RoleTree> resultList = new ArrayList<>();
        // 遍历查询的全部角色
        for (Role role : roles) {
            // 通过构造函数,把role的数据赋值给RoleTree
            RoleTree roleTree = new RoleTree(role);
            // 如果父节点为0则表示为根节点,否则为非根节点
            if (role.getParentId() == 0 || role.getParentId() == null){
                resultList.add(roleTree);
            }else {
                // 如果childMap中的数据有同一个父id的,则直接添加到父id的集合中
                if (childMap.containsKey(role.getParentId())){
                    childMap.get(role.getParentId()).add(roleTree);
                }else {
                    // 否则 Map添加一条数据,父id作为map的key
                    List<RoleTree> list = new ArrayList<>();
                    list.add(roleTree);
                    childMap.put(role.getParentId(), list);
                }
            }
        }
        //遍历所有的根节点,通过根节点和非根节点集合,找到这个根节点的所有子节点
        for (RoleTree root : resultList) {
            // Arrays.asList() 把对象转成list集合,
            getChild(childMap, Arrays.asList(root));
        }
        return resultList;
    }

    /**
     * 静态方法,改变resultList,递归算法
     * @param childMap
     * @param rootList
     */
    private static void getChild(Map<Long, List<RoleTree>> childMap, List<RoleTree> rootList){
        for (RoleTree roleTree : rootList){
            // 如果该根节点有子节点,则执行
            if (childMap.containsKey(roleTree.getId())){
                // 全部添加
                roleTree.getChildNode().addAll(childMap.get(roleTree.getId()));
                // 递归
                getChild(childMap, childMap.get(roleTree.getId()));
            }
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值