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;
}