实体类
public class DictTreeselectVO {
private String value;
private String label;
private String parentId;
private List<DictTreeselectVO> children = new ArrayList<DictTreeselectVO>();
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public List<DictTreeselectVO> getChildren() {
return children;
}
public void setChildren(List<DictTreeselectVO> children) {
this.children = children;
}
}
返回结果
public class TreeSelectT implements Serializable
{
private static final long serialVersionUID = 1L;
private String id;
private String label;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<TreeSelectT> children;
public TreeSelectT()
{
}
public TreeSelectT(DictTreeselectVO dictTreeselectVO)
{
this.id = dictTreeselectVO.getValue();
this.label = dictTreeselectVO.getLabel();
this.children = dictTreeselectVO.getChildren().stream().map(TreeSelectT::new).collect(Collectors.toList());
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public List<TreeSelectT> getChildren() {
return children;
}
public void setChildren(List<TreeSelectT> children) {
this.children = children;
}
}
工具类
public static List<TreeSelectT> buildTreeSelect(List<DictTreeselectVO> treeselectList){
List<DictTreeselectVO> treeselectVOList = buildDictTreeselect(treeselectList);
return treeselectVOList.stream().map(TreeSelectT::new).collect(Collectors.toList());
}
private static List<DictTreeselectVO> buildDictTreeselect(List<DictTreeselectVO> treeselectList){
List<DictTreeselectVO> returnList = new ArrayList<DictTreeselectVO>();
List<String> tempList = treeselectList.stream().map(DictTreeselectVO::getValue).collect(Collectors.toList());
for (DictTreeselectVO dictTreeselectVO : treeselectList) {
if (!tempList.contains(dictTreeselectVO.getParentId())) {
recursionFn(treeselectList, dictTreeselectVO);
returnList.add(dictTreeselectVO);
}
}
if (returnList.isEmpty()){
returnList = treeselectList;
}
return returnList;
}
private static void recursionFn (List<DictTreeselectVO> list, DictTreeselectVO t){
List<DictTreeselectVO> childList = getChildList(list, t);
t.setChildren(childList);
for (DictTreeselectVO tChild : childList) {
if (hasChild(list, tChild)){
recursionFn(list, tChild);
}
}
}
private static List<DictTreeselectVO> getChildList(List<DictTreeselectVO> list, DictTreeselectVO t)
{
List<DictTreeselectVO> tlist = new ArrayList<DictTreeselectVO>();
Iterator<DictTreeselectVO> it = list.iterator();
while (it.hasNext())
{
DictTreeselectVO n = (DictTreeselectVO) it.next();
if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(t.getValue()))
{
tlist.add(n);
}
}
return tlist;
}
private static boolean hasChild(List<DictTreeselectVO> list, DictTreeselectVO t){
return getChildList(list, t).size() > 0;
}