基于spring boot Java的数组转树状结构类
基于spring boot的数组转树状结构类
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class Array{
private <E, V> V createViewObject(E entity, Class<V> viewObjectClass) throws Exception {
V viewObject = viewObjectClass.getDeclaredConstructor().newInstance();
for (Method method : viewObjectClass.getMethods()) {
if (method.getName().startsWith("set")) {
String propertyName = method.getName().substring(3);
Method getter = findGetter(entity.getClass(), propertyName);
if (getter != null) {
Object value = getter.invoke(entity);
method.invoke(viewObject, value);
}
}
}
return viewObject;
}
private Method findGetter(Class<?> clazz, String propertyName) {
try {
return clazz.getMethod("get" + propertyName);
} catch (NoSuchMethodException e) {
return null;
}
}
private <E, V> List<V> childrenMain(List<E> data, String fid, Function<E, V> ucode, Class<E> entityClass, Class<V> viewObjectClass) throws Exception {
List<V> result = new ArrayList<>();
for (E entity : data) {
// Assuming fid is a property in entity class
String entityId = (String) entityClass.getMethod("getId").invoke(entity);
String entityFid = (String) entityClass.getMethod("getFid").invoke(entity);
if (entityFid.equals(fid)) {
List<V> children = childrenMain(data, entityId, ucode, entityClass, viewObjectClass);
V viewObject;
if(ucode!=null){
viewObject = ucode.apply(entity);
}else {
viewObject=createViewObject(entity, viewObjectClass);
}
// Assuming there's a setter for children in viewObject class
viewObjectClass.getMethod("setChildren", List.class).invoke(viewObject, children);
if (viewObject != null) {
result.add(viewObject);
}
}
}
return result;
}
/**
* 将具有层级关系的二维数组转换为树状数组
* @param data 数组
* @param fid 顶级ID
* @param entityClass 数据库实体的类
* @param viewObjectClass 转换后的视图对象类
* @return 转换后的视图对象类
*/
public <E, V> List<V> children(List<E> data, String fid,Class<E> entityClass, Class<V> viewObjectClass) throws Exception {
return this.childrenMain(data,fid,null,entityClass,viewObjectClass);
}
/**
* 将具有层级关系的二维数组转换为树状数组
* @param data 数组
* @param fid 顶级ID
* @param ucode 闭包函数
* @param entityClass 数据库实体的类
* @param viewObjectClass 转换后的视图对象类
* @return 转换后的视图对象类
*/
public <E, V> List<V> children(List<E> data, String fid,Function<E, V> ucode,Class<E> entityClass, Class<V> viewObjectClass) throws Exception {
return this.childrenMain(data,fid,ucode,entityClass,viewObjectClass);
}
public static <E, V> List<V> childrenUtil(List<E> data, String fid,Function<E, V> ucode,Class<E> entityClass, Class<V> viewObjectClass) throws Exception {
Array array=new Array();
return array.childrenMain(data,fid,ucode,entityClass,viewObjectClass);
}
public static <E, V> List<V> childrenUtil(List<E> data, String fid,Class<E> entityClass, Class<V> viewObjectClass) throws Exception {
Array array=new Array();
return array.childrenMain(data,fid,null,entityClass,viewObjectClass);
}
}
use
List<PermissionsEntity> permissionsEntities = permissionsMapper.selectList(null);
List<AccountPermissionsVo> res=Array.childrenUtil(permissionsEntities,"0",PermissionsEntity.class,AccountPermissionsVo.class);
return res;
other
VO
@Data
public class AccountPermissionsVo {
private String id;
private String fid;
private String name;
private String rid;
private List<AccountPermissionsVo> children;
}
本文来自博客园,作者:小枫同学,除网络转载的部分,其他版权属于作者和博客园所有,未经作者或博客园许可,禁止转载、复制、重新发布完整或者部分文字、代码、图片等信息,否则将保留追究法律责任的权利(如博客侵权了您的作品,本人再次表示抱歉,请将原创地址发送至下文邮箱,核实后立刻删除。)。查阅文章的同学,由于网络爬虫严重,有些代码并不会完整贴出来或者存在bug,不过你可以发送邮件到xfstune@126.com获取新代码,记得附上文章链接