java 转树形结构工具类
import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.ToString;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
import java.util.*;
public class TreeUtil {
public static <T> List<T> convert(List<T> origList, String idFieldName,
String parentIdFieldName, String childrenFieldName) throws Exception {
Map<String, T> idMaps = new HashMap<>();
List<T> tempList = new ArrayList<>();
List<T> result = new ArrayList<>();
for (T entity : origList) {
String id = Objects.toString(getFieldValue(entity, idFieldName), "");
String parentId = Objects.toString(getFieldValue(entity, parentIdFieldName), "");
if (StringUtils.isEmpty(id)) {
throw new Exception("存在id为空的资料");
}
idMaps.put(id, entity);
if (StringUtils.isEmpty(parentId)) {
result.add(entity);
} else {
T parentEntity = idMaps.get(parentId);
if (parentEntity == null) {
tempList.add(entity);
} else {
setChildrenValue(childrenFieldName, entity, parentEntity);
}
}
}
for (T entity : tempList) {
String parentId = Objects.toString(getFieldValue(entity, parentIdFieldName), "");
T parentEntity = idMaps.get(parentId);
if (parentEntity == null) {
throw new Exception("存在孤立的子节点");
} else {
setChildrenValue(childrenFieldName, entity, parentEntity);
}
}
return result;
}
private static <T> void setChildrenValue(String childrenFieldName, T entity, T parentEntity) throws Exception {
Object children = getFieldValue(parentEntity, childrenFieldName);
List<T> childrenList;
if (children == null) {
childrenList = new ArrayList<>();
childrenList.add(entity);
setFieldValue(parentEntity, childrenFieldName, childrenList);
} else {
List<T> childrenReal = (List<T>) children;
childrenReal.add(entity);
}
}
private static <T> Object getFieldValue(T entity, String fieldName) throws Exception {
Field field = ReflectionUtils.findField(entity.getClass(), fieldName);
if (field == null) {
throw new Exception(String.format("字段名称[%s]不存在", fieldName));
}
boolean accessible = field.isAccessible();
field.setAccessible(true);
Object result = ReflectionUtils.getField(field, entity);
field.setAccessible(accessible);
return result;
}
private static <T> void setFieldValue(T entity, String fieldName, Object value) throws Exception {
Field field = ReflectionUtils.findField(entity.getClass(), fieldName);
if (field == null) {
throw new Exception(String.format("字段名称[%s]不存在", fieldName));
}
boolean accessible = field.isAccessible();
field.setAccessible(true);
ReflectionUtils.setField(field, entity, value);
field.setAccessible(accessible);
}
public static void main(String[] args) throws Exception {
List<Demo> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Demo demo = new Demo(i, "一级节点" + i);
list.add(demo);
}
for (int i = 5; i < 15; i++) {
Demo demo = new Demo(i, i % 5, "二级节点" + i);
list.add(demo);
}
for (int i = 15; i < 100; i++) {
Demo demo = new Demo(i, i % 10 + 5, "三级节点" + i);
list.add(demo);
}
Demo demo = new Demo(100, 102, "非法节点");
list.add(demo);
List<Demo> convert = TreeUtil.convert(list, "id", "pid", "children");
String s = JSON.toJSONString(convert);
System.out.println(s);
}
}
@Data
@ToString
class Demo {
private Integer id;
private Integer pid;
private String name;
private List<Demo> children;
public Demo(Integer id, Integer pid, String name) {
this.id = id;
this.pid = pid;
this.name = name;
}
public Demo(Integer id, String name) {
this.id = id;
this.name = name;
}
}