列表转换为树形结构的列表

List<StyleDto> styleDtos2 = TreeBuilder.buildByLoopV2(styleDtos);将List<StyleDto> styleDtos列表转换为数据列表

与数据库对应的对象Style


@Data
@Entity
@TableName("style")
public class Style extends AuditableIdModel {

    @Schema(description = "款式名称")
    private String styleName;

    @Schema(description = "款式编码")
    private String styleCode;

    @Schema(description = "款式性别(0女、1男)")
    private Boolean styleGender;

    @Schema(description = "父级id(0代表当前是父级)")
    private Long parentId;

    @Schema(description = "号型系列")
    private String sizeSeries;

    @Schema(description = "款式单耗")
    private String styleUnitConsumption;

    @Schema(description = "产品类型")
    private String productType;

    @Schema(description = "产品性别(0女、1男)")
    private Boolean productGender;

    @Schema(description = "上下装(0上装、1下装)")
    private Boolean separates;

    @Schema(description = "使用版型")
    private String pattern;

    @Schema(description = "状态(0待审核、1已通过、2已驳回)")
    private Integer state;

    @Schema(description = "是否启用(0禁用、1启用)")
    private Boolean disabled;

    @Schema(description = "胸围放松量,单位cm")
    private String bustEase;

    @Schema(description = "腰围放松量,单位cm")
    private String waistlineEase;

    @Schema(description = "臀围放松量,单位cm")
    private String hipEase;

    @Schema(description = "外形特征")
    private String appearanceCharacteristics;

    @Schema(description = "外形图")
    private String appearanceImg;

}

列表输出时的对象StyleDto,继承了TreeNode



@Data
public class StyleDto  extends TreeNode {

    @Schema(description = "id")
    private Long id;

    @Schema(description = "款式名称")
    private String styleName;

    @Schema(description = "款式编码")
    private String styleCode;

    @Schema(description = "款式性别(0女、1男)")
    private Boolean styleGender;

    @Schema(description = "父级id(0代表当前是父级)")
    private Long parentId;

    @Schema(description = "号型系列")
    private String sizeSeries;

    @Schema(description = "款式单耗")
    private String styleUnitConsumption;

    @Schema(description = "产品类型")
    private String productType;

    @Schema(description = "产品性别(0女、1男)")
    private Boolean productGender;

    @Schema(description = "上下装(0上装、1下装)")
    private Boolean separates;

    @Schema(description = "使用版型")
    private String pattern;

    @Schema(description = "状态(0待审核、1已通过、2已驳回)")
    private Integer state;

    @Schema(description = "是否启用(0禁用、1启用)")
    private Boolean disabled;

    @Schema(description = "胸围放松量,单位cm")
    private String bustEase;

    @Schema(description = "腰围放松量,单位cm")
    private String waistlineEase;

    @Schema(description = "臀围放松量,单位cm")
    private String hipEase;

    @Schema(description = "外形特征")
    private String appearanceCharacteristics;

    @Schema(description = "外形图")
    private String appearanceImg;

    @Schema(description = "创建人")
    private Long createdById;

    @Schema(description = "创建人名称")
    private String createdByName;

    @Schema(description = "创建时间")
    private LocalDateTime createdTime;

    @Schema(description = "更新人")
    private Long updatedById;

    @Schema(description = "更新人名称")
    private String updatedByName;

    @Schema(description = "更新时间")
    private LocalDateTime updatedTime;

}
List<Style> styleList = styleMapper.selectStyleList(styleName, styleCode, productType);
List<StyleDto> styleDtos = convertToDtoList(styleList);
List<StyleDto> styleDtos2 = TreeBuilder.buildByLoopV2(styleDtos);

 TreeBuilder.buildByLoopV2() 


public final class TreeBuilder {
    public static <T extends TreeNode> List<T> buildByLoop(List<T> treeNodes, @NotNull Serializable root) {
        List<T> trees = new ArrayList();
        Iterator var3 = treeNodes.iterator();

        while(var3.hasNext()) {
            T treeNode = (TreeNode)var3.next();
            if (root.equals(treeNode.getParentId())) {
                trees.add(treeNode);
            }

            Iterator var5 = treeNodes.iterator();

            while(var5.hasNext()) {
                T it = (TreeNode)var5.next();
                if (it.getParentId().equals(treeNode.getId())) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList());
                    }

                    treeNode.add(it);
                }
            }
        }

        return trees;
    }

    public static <T extends TreeNode> List<T> buildByLoopV2(List<T> BaseTrees) {
        List<T> trees = new ArrayList();
        List<Serializable> collect = (List)BaseTrees.stream().map(TreeNode::getId).collect(Collectors.toList());
        Iterator var3 = BaseTrees.iterator();

        while(var3.hasNext()) {
            T BaseTree = (TreeNode)var3.next();
            Serializable parentId = BaseTree.getParentId();
            if (!collect.contains(parentId)) {
                trees.add(BaseTree);
            }

            Iterator var6 = BaseTrees.iterator();

            while(var6.hasNext()) {
                T it = (TreeNode)var6.next();
                if (it.getParentId().equals(BaseTree.getId())) {
                    if (BaseTree.getChildren() == null) {
                        BaseTree.setChildren(new ArrayList());
                    }

                    BaseTree.add(it);
                }
            }
        }

        return trees;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值