本文实例为大家分享了java转树形结构工具类的具体代码,供大家参考,具体内容如下
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
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;
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/l707268743/article/details/106642018
相关文章
猜你喜欢
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 71
-
2025-05-29 29
-
2025-05-29 89
-
2025-06-04 78
-
2025-05-25 54
热门评论