java转树形结构工具类详解
作者:方阙 时间:2021-07-26 04:00:08
本文实例为大家分享了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.*;
/**
* @author : liyk
* @version 1.0
* @date : 2020/6/9
*/
public class TreeUtil {
/**
* 将 List 转为树形结构
*
* @param origList : 要转换的 List
* @param idFieldName : id字段名
* @param parentIdFieldName : parentId 字段名
* @param childrenFieldName : children 字段名
* @param <T> : 拥有父子结构的 Entity
* @return : 树形结果
* @throws Exception .
*/
public static <T> List<T> convert(List<T> origList, String idFieldName,
String parentIdFieldName, String childrenFieldName) throws Exception {
// 用于保存当前 id 索引的实体类
Map<String, T> idMaps = new HashMap<>();
// 暂存区, 用于保存没有找到父 id 的控件
List<T> tempList = new ArrayList<>();
List<T> result = new ArrayList<>();
for (T entity : origList) {
// 获取 id, parentId, children
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)) {
// 如果父 id 为空, 则实体类为第一层
result.add(entity);
} else {
// 根据父 id 获取实体类
T parentEntity = idMaps.get(parentId);
if (parentEntity == null) {
// 没找到先放入暂存区
tempList.add(entity);
} else {
// 父组件判断是否存在 children, 不存在新增, 存在则直接假如
setChildrenValue(childrenFieldName, entity, parentEntity);
}
}
}
// 处理暂存区, 暂存区的一定不为根节点, 所以它只要父节点存在, 那么此轮查询一定能找到父节点(上一轮已经将全部节点放入 idMaps)
for (T entity : tempList) {
// 获取 parentId
String parentId = Objects.toString(getFieldValue(entity, parentIdFieldName), "");
// 根据父id获取实体类
T parentEntity = idMaps.get(parentId);
if (parentEntity == null) {
throw new Exception("存在孤立的子节点");
} else {
// 父组件判断是否存在children, 不存在新增, 存在则直接假如
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;
}
}
来源:https://blog.csdn.net/l707268743/article/details/106642018
标签:java,树形结构
0
投稿
猜你喜欢
Android SharedPreferences数据存储详解
2023-08-05 13:32:47
C语言实现自定义扫雷游戏(递归版)
2023-11-03 02:12:36
Java 栈与队列超详细分析讲解
2023-08-15 01:09:07
java的泛型你真的了解吗
2022-07-25 09:40:06
Spring的@Autowired加到接口上但获取的是实现类的问题
2023-08-23 21:32:21
springboot嵌套子类使用方式—前端与后台开发的注意事项
2023-09-16 12:37:22
Java IO流相关知识代码解析
2023-08-05 05:48:04
详解APP微信支付(java后台_统一下单和回调)
2023-11-10 17:26:42
Java文件操作之按行读取文件和遍历目录的方法
2023-11-24 16:58:21
Java实现验证码具体代码(图片、汉字)
2023-03-25 11:14:31
JFinal实现伪静态的方法
2023-07-17 12:11:37
C语言文件操作之fread函数详解
2023-07-06 18:24:15
svn 清理失败 (cleanup 失败) 的快速解决方法
2022-10-25 11:22:40
java,android,MD5加密算法的实现代码(16位,32位)
2022-07-12 20:40:10
Servlet实现文件的上传与下载
2023-08-08 06:13:43
Maven学习----Maven安装与环境变量配置教程
2021-12-04 08:20:25
Java对象类型的判断详解
2023-07-26 09:55:07
微信小程序获取手机号,后端JAVA解密流程代码
2023-11-29 07:57:26
SparkSQl简介及运行原理
2023-09-17 05:18:19
JVM教程之内存管理和垃圾回收(三)
2023-11-10 15:49:54