基于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;
}
posted @ 2024-05-11 11:04  小枫同学  阅读(11)  评论(0编辑  收藏  举报