java 将查询出来的list 转为tree结构的工具类

2022-08-16 08:17:38 浏览数 (1)

目录

  • 1 代码

1 代码

代码语言:javascript复制
public class ConvertUtils {

    public static List<DateSourceType> departmentTransferVO2(List<DateSourceType> list) {
        if (ObjectUtils.isEmpty(list)) {
            return null;
        }
        Map<String, DateSourceType> map = list.stream().collect(Collectors.toMap(DateSourceType::getId, v -> v, (a, b) -> a));
        return getDepartmentChildVO2(list, map);
    }

    private static List<DateSourceType> getDepartmentChildVO2(List<DateSourceType> list, Map<String, DateSourceType> map) {
        List<DateSourceType> res = new ArrayList<>();
        for (DateSourceType departmentVo : list) {
            DateSourceType departmentVo1 = map.get(departmentVo.getParentId());
            if (departmentVo1 != null) {
                List<DateSourceType> childes = departmentVo1.getChildDepartments();
                if (childes != null) {
                    childes.add(departmentVo);
                } else {
                    departmentVo1.setChildDepartments(Lists.newArrayList(departmentVo));
                }
            } else {
                res.add(departmentVo);
            }
        }
        return res;
    }


//    public static void main(String[] args) {
//
//
//    }

}

0 人点赞