2种方法将集合数据List构建成树形结构


在这里插入图片描述

递归循环构建树结构

先查最外层树节点数据,再递归遍历每一层子节点数据

public ApiResultDto<List<LocationDto>> getTreeByParams(LocationSearchDto searchDto, SecurityUser user) {
		// 最外层的树结构【此最外层是BUILDING,也就是BUILDING的parentId为null】,不是获取所有数据,与下不同
        searchDto.setProjectId(user.getProjectId());
        searchDto.setType(LocationType.BUILDING);
        List<LocationEntity> locationEntities = locationDao.findByParams(searchDto);
        List<LocationDto> locationDtos = locationEntities.stream().map(t -> {
            LocationDto dto = new LocationDto();
            dto.setId(t.getId());
            dto.setName(t.getName());
            dto.setType(t.getType());
            dto.setSort(t.getSort());
            dto.setSerialNum(t.getSerialNum());
            dto.setCode(t.getCode());
            dto.setParentId(t.getParentId());
           // 循环递归获取子节点
            iterChild(dto, user.getProjectId());
            return dto;
        }).collect(Collectors.toList());
        return ApiResultDto.success(locationDtos);
    }

 	/**
     * 遍历子节点.
     *
     * @param dto 空间dto
     */
    private void iterChild(LocationDto dto, UUID projectId) {
//  <select id="findChildById"
//        parameterType="map" resultType="common.po.location.LocationEntity">
//        select *
//        from location
//        where model_status = 1
//        and parent_id = #{id}
//    </select>
        List<LocationEntity> locations = locationDao.findChildById(dto.getId());
        if (CollectionUtils.isEmpty(locations)) {
            return;
        }
        List<LocationDto> locationDtos = locations.stream().map(t -> {
            LocationDto childDto = new LocationDto();
            childDto.setId(t.getId());
            childDto.setName(t.getName());
            childDto.setType(t.getType());
            childDto.setSort(t.getSort());
            childDto.setSerialNum(t.getSerialNum());
            childDto.setCode(t.getCode());
            childDto.setParentId(t.getParentId());
            // 递归遍历子节点
            iterChild(childDto, projectId);
            return childDto;
        }).collect(Collectors.toList());
        dto.setChildren(locationDtos);
    }

hutool.TreeUtil.build构建树结构

   public ApiResultDto<List<Tree<UUID>>> getTreeByParams(LocationSearchDto searchDto, SecurityUser user) {
   		// 查询到所有数据
        searchDto.setProjectId(user.getProjectId());
        List<LocationEntity> entities = locationDao.findByParams(searchDto);
        // 获取非第一层"BUILDING"的数据的父级信息【查询到子节点,要带出对应的所有父节点,如果不需要可以删除】
        List<LocationEntity> locationEntities = new ArrayList<>();
        locationEntities.addAll(entities);
        for (LocationEntity entity : entities) {
            addParentEntities(entity, locationEntities);
        }
        locationEntities = locationEntities.stream().distinct().collect(Collectors.toList());
        // 构建树,主要数据在treeNode中,其余数据在extra字段中
        List<TreeNode<UUID>> nodeList = new ArrayList<>();
        for (LocationEntity entity : locationEntities) {
        // 此时最后一个权重参数weight为null,默认都是0,也就是无顺序
        // 如果想要有序,需要指定该权重值,一般会提前在list表中设置好即可
        // TreeNode<UUID> treeNode = new TreeNode<>(entity.getId(), entity.getParentId(), entity.getName(), null);
             TreeNode<UUID> treeNode = new TreeNode<>(entity.getId(), entity.getParentId(), entity.getName(), entity.getSort());
            nodeList.add(treeNode);
            Map<String, Object> extra = new HashMap<>();
            extra.put("name", entity.getName());
            extra.put("serialNum", entity.getSerialNum());
            extra.put("code", entity.getCode());
            extra.put("type", entity.getType());
            extra.put("sort", entity.getSort());
            if (MapUtils.isNotEmpty(extra)) {
                treeNode.setExtra(extra);
            }
        }
        List<Tree<UUID>> result = TreeUtil.build(nodeList, null);
        return ApiResultDto.success(result);
    }

	/**
     * 获取当前空间实体额所有上级实体.
     *
     * @param entity 当前空间实体
     * @param res    结果
     */
    private void addParentEntities(LocationEntity entity, List<LocationEntity> res) {
        if (Objects.nonNull(entity) && Objects.nonNull(entity.getParentId())) {
            LocationEntity parentEntity = locationDao.selectById(entity.getParentId());
            if (Objects.nonNull(parentEntity)) {
                res.add(parentEntity);
                addParentEntities(parentEntity, res);
            }
        }
    }
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr朱墨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值