public List<DeptTree> getParent(List<DeptTree> deptTrees) {
List<DeptTree> collect = deptTrees
.stream().filter(m -> StrUtil.isNotBlank(m.getPid()) && m.getLevel() != null && m.getLevel() == 1).map((m) -> {
m.setChildren(getChildren(m, deptTrees));
return m;
}
).collect(toList());
if (collect.isEmpty()) {
return deptTrees;
}
return collect;
}
private List<DeptTree> getChildren(DeptTree root, List<DeptTree> all) {
return all.stream().filter(m -> Objects.equals(m.getPid(), root.getDeptId())).map(
m -> {
m.setChildren(getChildren(m, all));
return m;
}
).collect(toList());
}
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.Hidden;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.List;
@Getter
@Setter
@Accessors(chain = true)
public class DeptTree implements Serializable {
private String pid;
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<DeptTree> children;
private String deptId;
private String deptName;
@Hidden
private Integer level;
}