java转树形结构工具类详解
作者:方阙 发布时间:2021-07-26 04:00:08
标签:java,树形结构
本文实例为大家分享了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


猜你喜欢
- C#事件实例详解C#和JAVA有许多相似的地方,设计思想差不多,语法及其相像,均传承自面向对象设计思想,灵感来自C++并取其精华去其“糟粕(
- 说明这里只以 servlet 为例,没有涉及到框架,但其实路径的基本原理和框架的关系不大,所以学了框架的同学如果对路径有疑惑的也可以阅读此文
- /* * 使用 C# 动态编译代码和执行 * 作者: yaob */ static void Main(string[] args) { /
- 系统原来用的是BOSCH_BMA222的gsensor, 现在要求换成使用MMA7660,我们来看一下怎样增加驱动和调试过程。 1. 修改M
- 引言这里实现一个简单的图片上传功能,主要是熟悉这个文件上传的交互流程。关于更复杂的文件上传,如大文件的切片上传、断点续传等,这里不做过多介绍
- 之前一篇文章研究完横向二级菜单,发现其中使用了SparseArray去替换HashMap的使用.于是乎自己查了一些相关资料,自己同时对性能进
- 一、前言之前介绍了JMeter engine启动原理,但是里面涉及到HashTree这个类结构没有给大家详细介绍,这边文章就详细介绍JMet
- SpringBoot访问html和js等静态资源配置把静态资源放到resources/static下,这是springboot静态资源默认访
- Kotlin 是一种在 Java 虚拟机上运行的静态类型编程语言,被称之为 Android 世界的Swift,由 JetBrains 设计开
- 在许多游戏中当我们因为一些问题无法接着进行游玩,我们都会选择保存,以便后面有空时,接着游玩。接下来,我们会学习一些Unity有关的存储方法。
- 问题我们发现,之所以我们现在离不开 xml 配置文件,是因为我们有一句很关键的配置:<!-- 告知spring框架在,读取配置文件,创
- maven 打包 动态启动脚本介绍如何通过maven的环境变量动态打包, 并动态改变启动脚本中的环境参数之前都是每个环境一个启动脚本, 其实
- 在任何Java面试当中多线程和并发方面的问题都是必不可少的一部分。如果你想获得任何股票投资银行的前台资讯职位,那么你应该准备很多关于多线程的
- 基本介绍BottomSheetDialog是底部操作控件,可在屏幕底部创建一个支持滑动关闭视图。目前依赖使用如下:implementatio
- 本文实例为大家分享了ImageView阴影和图层效果的实现代码,供大家参考,具体内容如下import android.app.Activit
- 本文实例为大家分享了java实现登录验证码功能的具体代码,供大家参考,具体内容如下登录验证码登录验证是大多数登录系统都会用到的一个功能,它的
- 前段时间由于VSS上的一个项目要给2个公司开发使用,而2个公司的需求不同 就把该项目复制到VSS上的另外一个目录,结果在别人的电脑取出并打开
- 我们都知道取消标题栏有两种方式,一种是在Java代码中取消,另一种通过设置styles.xml文件中的Theme即可;如下图:第一种:第二种
- 本文实例讲述了C#逐行读取文件的方法。分享给大家供大家参考。具体如下:这里使用C#逐行读取文件,对于大文件的读取非常有用。StreamRea
- 本文实例讲述了Java获取UTC时间的方法。分享给大家供大家参考,具体如下:取得本地时间:java.util.Calendar cal =