Java集合转成树的三种方法(listToTree)
-
以stream的方式进行转化
-
转成json处理再转成list
实体:
package com.utils;
import java.util.List;
/**
* @Description:
* @Author:
* @Date: 2021/12/16 9:35
*/
public class TestEntity {
private String name;
private String code;
private String parentCode;
private Integer age;
private List<TestEntity> children;
public List<TestEntity> getChildren() {
return children;
}
public void setChildren(List<TestEntity> children) {
this.children = children;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getParentCode() {
return parentCode;
}
public void setParentCode(String parentCode) {
this.parentCode = parentCode;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}