树状结构的json组装

package com.example;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.assertj.core.util.Lists;
import org.springframework.util.CollectionUtils;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class LableDto {

    private Long pid;
    private Long id;
    private String lable;
    private Integer depth;
    private List<LableDto> children;


    public Map<Long, LableDto> Transtion(List<LableDto> list) {
        HashMap<Long, LableDto> map = new HashMap<>();
        for (LableDto lableDto : list) {
            map.put(lableDto.getId(), lableDto);
        }
        return map;
    }


    public static List<Lable> convert(List<LableDto> lableDtos, ArrayList<Lable> list) {
        for (LableDto lableDto : lableDtos) {
            Lable lable = new Lable();
            lable.setDepth(lableDto.getDepth());
            lable.setId(lableDto.getId());
            if (!CollectionUtils.isEmpty(lableDto.getChildren())) {
                lable.setChildren(convert(lableDto.getChildren(), list));
                list.add(lable);
            } else {
                lable.setChildren(null);
                list.add(lable);
            }

        }
        return list;
    }

    public Lable convertLable(LableDto lableDto) {
        Lable lable = new Lable();
        lable.setDepth(lableDto.getDepth());
        lable.setId(lableDto.getId());
        List<LableDto> children = lableDto.getChildren();
        if (!CollectionUtils.isEmpty(children)) {
            ArrayList<Lable> list = Lists.newArrayList();
            for (LableDto child : children) {
                Lable lable1 = new Lable();
                lable1.setDepth(child.getDepth());
                lable1.setId(child.getId());
            }
        } else {
            lable.setChildren(null);
        }
        return lable;
    }

    public static Map<Long, Lable> build(List<LableDto> lableDtos, Map<Long, Lable> map) {
        for (LableDto lableDto : lableDtos) {
            Lable lable = new Lable();
            lable.setChildren(null);
            lable.setId(lableDto.getId());
            lable.setDepth(lableDto.getDepth());
            lable.setPid(lableDto.getPid());
            lable.setLable(lableDto.getLable());
            map.put(lableDto.getId(), lable);
            List<LableDto> children = lableDto.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                build(children, map);
            }
        }
        return map;
    }


    public static Map<Long, LableDto> Assemrt(List<LableDto> lableDtos) {
        HashMap<Long, LableDto> map = new HashMap<>();
        List<LableDto> collect = lableDtos.stream().sorted(Comparator.comparing(LableDto::getDepth))
                .collect(Collectors.toList());
        for (LableDto lableDto : collect) {
            Long pid = lableDto.getPid();
            if (pid != null) {
                LableDto parVo = map.get(pid);
                if (null != parVo) {
                    List<LableDto> children = parVo.getChildren();
                    if (null == children) {
                        children = new ArrayList<>();
                    }
                    children.add(lableDto);
                    parVo.setChildren(children);
                }
            }
            map.put(lableDto.getId(), lableDto);
        }
        return map;
    }


    public static void main(String[] args) {
        ArrayList<LableDto> lableDtos = Lists
                .newArrayList(new LableDto(1L, 2L, "hh", 1, Lists.newArrayList(new LableDto(2L, 4L, "h56h", 2, null),
                        new LableDto(2L, 5L, "5555", 2, null))));
        //List<Lable> convert = LableDto.convert(lableDtos, Lists.newArrayList());
        ArrayList<LableDto> lableDtos1 = Lists
                .newArrayList(new LableDto(1L, 3L, "hh", 2, null), new LableDto(1L, 2L, "hh", 2, null),
                        new LableDto(null, 1L, "hh", 1, null));

        //    Map<Long, Lable> build = build(lableDtos, new HashMap<>());
        Map<Long, LableDto> assemrt = Assemrt(lableDtos1);
        System.out.println();


    }

}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Lable {

    private Long pid;
    private Long id;
    private String lable;
    private Integer depth;
    private List<Lable> children;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值